TypeIdItem.java revision 281b510a9c2b4ae914ab28b9a4f4d622e5861da6
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
31public class TypeIdItem extends IndexedItem<TypeIdItem> {
32    private final IndexedItemReference<StringIdItem> typeDescriptorReferenceField;
33
34    public TypeIdItem(DexFile dexFile, int index) {
35        super(dexFile, index);
36        fields = new Field[] {
37                typeDescriptorReferenceField = new IndexedItemReference<StringIdItem>(dexFile.StringIdsSection,
38                        new IntegerField(null), "descriptor_idx")
39        };
40    }
41
42    public TypeIdItem(DexFile dexFile, StringIdItem stringIdItem) {
43        this(dexFile, -1);
44        typeDescriptorReferenceField.setReference(stringIdItem);
45    }
46
47    public TypeIdItem(DexFile dexFile, String value) {
48        this(dexFile, new StringIdItem(dexFile, value));
49    }
50
51    protected int getAlignment() {
52        return 4;
53    }
54
55    /**
56     * Returns the number of 2-byte registers that an instance of this type requires
57     * @return The number of 2-byte registers that an instance of this type requires
58     */
59    public int getRegisterCount() {
60        String type = this.getTypeDescriptor();
61        /** Only the long and double primitive types are 2 words,
62         * everything else is a single word
63         */
64        if (type.equals("J") || type.equals("D")) {
65            return 2;
66        } else {
67            return 1;
68        }
69    }
70
71    public ItemType getItemType() {
72        return ItemType.TYPE_TYPE_ID_ITEM;
73    }
74
75    public String getConciseIdentity() {
76        return "type_id_item: " + getTypeDescriptor();
77    }
78
79    public String getTypeDescriptor() {
80        return typeDescriptorReferenceField.getReference().getStringValue();
81    }
82
83    public int compareTo(TypeIdItem o) {
84        //sort by the index of the StringIdItem
85        return typeDescriptorReferenceField.compareTo(o.typeDescriptorReferenceField);
86    }
87
88    public String toShorty() {
89        String type = getTypeDescriptor();
90        if (type.length() > 1) {
91            return "L";
92        } else {
93            return type;
94        }
95    }
96}
97