ItemType.java revision 83b80f81d311b233188c281059aad4a9f5e8b4e6
1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2009 Ben Gruver
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29package org.jf.dexlib;
30
31import java.util.TreeMap;
32
33/**
34 * Enumeration of all the top-level item types.
35 */
36public enum ItemType {
37    TYPE_HEADER_ITEM(               0x0000, 17, 4, "header_item"),
38    TYPE_STRING_ID_ITEM(            0x0001, 0,  4, "string_id_item"),
39    TYPE_TYPE_ID_ITEM(              0x0002, 1,  4, "type_id_item"),
40    TYPE_PROTO_ID_ITEM(             0x0003, 2,  4, "proto_id_item"),
41    TYPE_FIELD_ID_ITEM(             0x0004, 3,  4, "field_id_item"),
42    TYPE_METHOD_ID_ITEM(            0x0005, 4,  4, "method_id_item"),
43    TYPE_CLASS_DEF_ITEM(            0x0006, 5,  4, "class_def_item"),
44    TYPE_MAP_LIST(                  0x1000, 16, 4, "map_list"),
45    TYPE_TYPE_LIST(                 0x1001, 6,  4, "type_list"),
46    TYPE_ANNOTATION_SET_REF_LIST(   0x1002, 7,  4, "annotation_set_ref_list"),
47    TYPE_ANNOTATION_SET_ITEM(       0x1003, 8,  4, "annotation_set_item"),
48    TYPE_CLASS_DATA_ITEM(           0x2000, 9,  1, "class_data_item"),
49    TYPE_CODE_ITEM(                 0x2001, 10, 4, "code_item"),
50    TYPE_STRING_DATA_ITEM(          0x2002, 11, 1, "string_data_item"),
51    TYPE_DEBUG_INFO_ITEM(           0x2003, 12, 1, "debug_info_item"),
52    TYPE_ANNOTATION_ITEM(           0x2004, 13, 1, "annotation_item"),
53    TYPE_ENCODED_ARRAY_ITEM(        0x2005, 14, 1, "encoded_array_item"),
54    TYPE_ANNOTATIONS_DIRECTORY_ITEM(0x2006, 15, 4, "annotations_directory_item");
55
56    /** A map to facilitate looking up an <code>ItemType</code> by ordinal */
57    private final static TreeMap<Integer, ItemType> itemTypeIntegerMap;
58
59    /** builds the <code>itemTypeIntegerMap</code> object */
60    static {
61	    itemTypeIntegerMap = new TreeMap<Integer, ItemType>();
62
63        for (ItemType itemType: ItemType.values()) {
64            itemTypeIntegerMap.put(itemType.MapValue, itemType);
65        }
66    }
67
68
69
70    /**
71     * value when represented in a MapItem
72     */
73    public final int MapValue;
74
75    /**
76     * name of the type
77     */
78    public final String TypeName;
79
80    /**
81     * index for this item's section
82     */
83    public final int SectionIndex;
84
85    /**
86     * the alignment for this item type
87     */
88    public final int ItemAlignment;
89    /**
90     * Constructs an instance.
91     *
92     * @param mapValue value when represented in a MapItem
93     * @param sectionIndex index for this item's section
94     * @param itemAlignment the byte alignment required by this item
95     * @param typeName non-null; name of the type
96     */
97    private ItemType(int mapValue, int sectionIndex, int itemAlignment, String typeName) {
98        this.MapValue = mapValue;
99        this.SectionIndex = sectionIndex;
100        this.ItemAlignment = itemAlignment;
101        this.TypeName = typeName;
102    }
103
104    /**
105     * Converts an int value to the corresponding ItemType enum value,
106     * or null if the value isn't a valid ItemType value
107     *
108     * @param itemType the int value to convert to an ItemType
109     * @return the ItemType enum value corresponding to itemType, or null
110     * if not a valid ItemType value
111     */
112    public static ItemType fromInt(int itemType) {
113        return itemTypeIntegerMap.get(itemType);
114    }
115
116    /**
117     * Returns true if this is an indexed item, or false if its an offsetted item
118     * @return true if this is an indexed item, or false if its an offsetted item
119     */
120    public boolean isIndexedItem() {
121        return MapValue <= 0x1000;
122    }
123}