MethodIdItem.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 org.jf.dexlib.Util.Input;
32import org.jf.dexlib.Util.AnnotatedOutput;
33
34public class MethodIdItem extends Item<MethodIdItem> {
35    private int hashCode = 0;
36
37    private TypeIdItem classType;
38    private ProtoIdItem methodPrototype;
39    private StringIdItem methodName;
40
41    /**
42     * Creates a new uninitialized <code>MethodIdItem</code>
43     * @param dexFile The <code>DexFile</code> that this item belongs to
44     */
45    protected MethodIdItem(DexFile dexFile) {
46        super(dexFile);
47    }
48
49    /**
50     * Creates a new <code>MethodIdItem</code> for the given class, type and name
51     * @param dexFile The <code>DexFile</code> that this item belongs to
52     * @param classType the class that the method is a member of
53     * @param methodPrototype the type of the method
54     * @param methodName the name of the method
55     */
56    private MethodIdItem(DexFile dexFile, TypeIdItem classType, ProtoIdItem methodPrototype, StringIdItem methodName) {
57        this(dexFile);
58        this.classType = classType;
59        this.methodPrototype = methodPrototype;
60        this.methodName = methodName;
61    }
62
63    /**
64     * Returns a <code>MethodIdItem</code> for the given values, and that has been interned into
65     * the given <code>DexFile</code>
66     * @param dexFile The <code>DexFile</code> that this item belongs to
67     * @param classType the class that the method is a member of
68     * @param methodPrototype the type of the method
69     * @param methodName the name of the method
70     * @return a <code>MethodIdItem</code> for the given values, and that has been interned into
71     * the given <code>DexFile</code>
72     */
73    public static MethodIdItem getInternedMethodIdItem(DexFile dexFile, TypeIdItem classType,
74                                                       ProtoIdItem methodPrototype, StringIdItem methodName) {
75        MethodIdItem methodIdItem = new MethodIdItem(dexFile, classType, methodPrototype, methodName);
76        return dexFile.MethodIdsSection.intern(methodIdItem);
77    }
78
79    /** {@inheritDoc} */
80    protected void readItem(Input in, ReadContext readContext) {
81        classType = dexFile.TypeIdsSection.getItemByIndex(in.readShort());
82        methodPrototype = dexFile.ProtoIdsSection.getItemByIndex(in.readShort());
83        methodName = dexFile.StringIdsSection.getItemByIndex(in.readInt());
84    }
85
86    /** {@inheritDoc} */
87    protected int placeItem(int offset) {
88        return offset + 8;
89    }
90
91    /** {@inheritDoc} */
92    protected void writeItem(AnnotatedOutput out) {
93        if (out.annotates()) {
94            out.annotate(2, classType.getConciseIdentity());
95            out.annotate(2, methodPrototype.getConciseIdentity());
96            out.annotate(4, methodName.getConciseIdentity());
97        }
98
99        out.writeShort(classType.getIndex());
100        out.writeShort(methodPrototype.getIndex());
101        out.writeInt(methodName.getIndex());
102    }
103
104    /** {@inheritDoc} */
105    public ItemType getItemType() {
106        return ItemType.TYPE_METHOD_ID_ITEM;
107    }
108
109    /** {@inheritDoc} */
110    public String getConciseIdentity() {
111        return "method_id_item: " + getMethodString();
112    }
113
114    /** {@inheritDoc} */
115    public int compareTo(MethodIdItem o) {
116        int result = classType.compareTo(o.classType);
117        if (result != 0) {
118            return result;
119        }
120
121        result = methodName.compareTo(o.methodName);
122        if (result != 0) {
123            return result;
124        }
125
126        return methodPrototype.compareTo(o.methodPrototype);
127    }
128
129    private String cachedMethodString = null;
130    /**
131     * @return a string formatted like LclassName;->methodName(TTTT..)R
132     */
133    public String getMethodString() {
134        if (cachedMethodString == null) {
135            cachedMethodString = classType.getTypeDescriptor() + "->" + methodName.getStringValue() +
136                    methodPrototype.getPrototypeString();
137        }
138        return cachedMethodString;
139    }
140
141    /**
142     * @return the method prototype
143     */
144    public ProtoIdItem getPrototype() {
145        return methodPrototype;
146    }
147
148    /**
149     * @return the name of the method
150     */
151    public StringIdItem getMethodName() {
152        return methodName;
153    }
154
155    /**
156     * @return the class this method is a member of
157     */
158    public TypeIdItem getContainingClass() {
159        return classType;
160    }
161
162    /**
163     * calculate and cache the hashcode
164     */
165    private void calcHashCode() {
166        hashCode = classType.hashCode();
167        hashCode = 31 * hashCode + methodPrototype.hashCode();
168        hashCode = 31 * hashCode + methodName.hashCode();
169    }
170
171    @Override
172    public int hashCode() {
173        //there's a small possibility that the actual hash code will be 0. If so, we'll
174        //just end up recalculating it each time
175        if (hashCode == 0)
176            calcHashCode();
177        return hashCode;
178    }
179
180    @Override
181    public boolean equals(Object o) {
182        if (this==o) {
183            return true;
184        }
185        if (o==null || !this.getClass().equals(o.getClass())) {
186            return false;
187        }
188
189        //This assumes that the referenced items have been interned in both objects.
190        //This is a valid assumption because all outside code must use the static
191        //"getInterned..." style methods to make new items, and any item created
192        //internally is guaranteed to be interned
193        MethodIdItem other = (MethodIdItem)o;
194        return (classType == other.classType &&
195                methodPrototype == other.methodPrototype &&
196                methodName == other.methodName);
197    }
198}
199