FieldIdItem.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 FieldIdItem extends IndexedItem<FieldIdItem> {
32    private final IndexedItemReference<TypeIdItem> classTypeReferenceField;
33    private final IndexedItemReference<TypeIdItem> fieldTypeReferenceField;
34    private final IndexedItemReference<StringIdItem> fieldNameReferenceField;
35
36    public FieldIdItem(DexFile dexFile, int index) {
37        super(dexFile, index);
38        fields = new Field[] {
39                classTypeReferenceField = new IndexedItemReference<TypeIdItem>(dexFile.TypeIdsSection,
40                        new ShortIntegerField(null), "class_idx"),
41                fieldTypeReferenceField = new IndexedItemReference<TypeIdItem>(dexFile.TypeIdsSection,
42                        new ShortIntegerField(null), "type_idx"),
43                fieldNameReferenceField = new IndexedItemReference<StringIdItem>(dexFile.StringIdsSection,
44                        new IntegerField(null), "name_idx")
45        };
46    }
47
48    public FieldIdItem(DexFile dexFile, TypeIdItem classType, StringIdItem fieldName, TypeIdItem fieldType) {
49        this(dexFile, -1);
50        classTypeReferenceField.setReference(classType);
51        fieldTypeReferenceField.setReference(fieldType);
52        fieldNameReferenceField.setReference(fieldName);
53    }
54
55    public FieldIdItem(DexFile dexFile, TypeIdItem classType, String fieldName, TypeIdItem fieldType) {
56        this(dexFile, classType, new StringIdItem(dexFile, fieldName), fieldType);
57    }
58
59    protected int getAlignment() {
60        return 4;
61    }
62
63    public ItemType getItemType() {
64        return ItemType.TYPE_FIELD_ID_ITEM;
65    }
66
67    public StringIdItem getFieldName() {
68        return fieldNameReferenceField.getReference();
69    }
70
71    public String getConciseIdentity() {
72        String parentClass = classTypeReferenceField.getReference().getTypeDescriptor();
73        //strip off the leading L and trailing ;
74        parentClass = parentClass.substring(1, parentClass.length() - 1);
75
76        return parentClass + "/" + fieldNameReferenceField.getReference().getStringValue() +
77                ":" + fieldTypeReferenceField.getReference().getTypeDescriptor();
78    }
79
80    public int compareTo(FieldIdItem o) {
81        int result = classTypeReferenceField.compareTo(o.classTypeReferenceField);
82        if (result != 0) {
83            return result;
84        }
85
86        result = fieldNameReferenceField.compareTo(o.fieldNameReferenceField);
87        if (result != 0) {
88            return result;
89        }
90
91        return fieldTypeReferenceField.compareTo(o.fieldTypeReferenceField);
92
93    }
94
95    public TypeIdItem getFieldType() {
96        return fieldTypeReferenceField.getReference();
97    }
98
99    public TypeIdItem getContainingClass() {
100        return classTypeReferenceField.getReference();
101    }
102}
103