summaryrefslogtreecommitdiffstats
path: root/dexlib/src/main/java/org/jf/dexlib/TypeListItem.java
blob: 50262ea17f27017ea5c100c939af5bcc815a1fbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * [The "BSD licence"]
 * Copyright (c) 2010 Ben Gruver (JesusFreke)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.jf.dexlib;

import org.jf.dexlib.Util.AnnotatedOutput;
import org.jf.dexlib.Util.Input;
import org.jf.dexlib.Util.ReadOnlyArrayList;

import java.util.List;

public class TypeListItem extends Item<TypeListItem> {
    private int hashCode = 0;

    private TypeIdItem[] typeList;

    /**
     * Creates a new uninitialized <code>TypeListItem</code>
     * @param dexFile The <code>DexFile</code> that this item belongs to
     */
    protected TypeListItem(DexFile dexFile) {
        super(dexFile);
    }

    /**
     * Creates a new <code>TypeListItem</code> for the given string
     * @param dexFile The <code>DexFile</code> that this item belongs to
     * @param typeList A list of the types that this <code>TypeListItem</code> represents
     */
    private TypeListItem(DexFile dexFile, TypeIdItem[] typeList) {
        super(dexFile);

        this.typeList = typeList;
    }

    /**
     * Returns a <code>TypeListItem</code> for the given values, and that has been interned into
     * the given <code>DexFile</code>
     * @param dexFile The <code>DexFile</code> that this item belongs to
     * @param typeList A list of the types that this <code>TypeListItem</code> represents
     * @return a <code>TypeListItem</code> for the given values, and that has been interned into
     * the given <code>DexFile</code>
     */
    public static TypeListItem internTypeListItem(DexFile dexFile, List<TypeIdItem> typeList) {
        TypeIdItem[] typeArray = new TypeIdItem[typeList.size()];
        typeList.toArray(typeArray);
        TypeListItem typeListItem = new TypeListItem(dexFile, typeArray);
        return dexFile.TypeListsSection.intern(typeListItem);
    }

    /**
     * Looks up the <code>TypeListItem</code> from the given <code>DexFile</code> for the given
     * list of types
     * @param dexFile the <code>Dexfile</code> to find the type in
     * @param typeList A list of the types that the <code>TypeListItem</code> represents
     * @return a <code>TypeListItem</code> from the given <code>DexFile</code> for the given
     * list of types, or null if it doesn't exist
     */
    public static TypeListItem lookupTypeListItem(DexFile dexFile, List<TypeIdItem> typeList) {
        TypeIdItem[] typeArray = new TypeIdItem[typeList.size()];
        typeList.toArray(typeArray);
        TypeListItem typeListItem = new TypeListItem(dexFile, typeArray);
        return dexFile.TypeListsSection.getInternedItem(typeListItem);
    }

    /** {@inheritDoc} */
    protected void readItem(Input in, ReadContext readContext) {
        int size = in.readInt();
        typeList = new TypeIdItem[size];
        for (int i=0; i<size; i++) {
            int typeIndex = in.readShort();
            typeList[i] = dexFile.TypeIdsSection.getItemByIndex(typeIndex);
        }
    }

    /** {@inheritDoc} */
    protected int placeItem(int offset) {
        return offset + 4 + typeList.length * 2;
    }

    /** {@inheritDoc} */
    protected void writeItem(AnnotatedOutput out) {
        //yes, the code to write the item is duplicated. This eliminates the need to iterate over the list twice

        if (out.annotates()) {
            out.annotate(4, "size: 0x" + Integer.toHexString(typeList.length) + " (" + typeList.length +")");

            for (TypeIdItem typeIdItem: typeList) {
                out.annotate(2, "type_id_item: " + typeIdItem.getTypeDescriptor());
            }
        }
        out.writeInt(typeList.length);
        for (TypeIdItem typeIdItem: typeList) {
            int typeIndex = typeIdItem.getIndex();
            if (typeIndex > 0xffff) {
                throw new RuntimeException(String.format("Error writing type_list entry. The type index of " +
                    "type %s is too large", typeIdItem.getTypeDescriptor()));
            }
            out.writeShort(typeIndex);
        }
    }

    /** {@inheritDoc} */
    public ItemType getItemType() {
        return ItemType.TYPE_TYPE_LIST;
    }

    /** {@inheritDoc} */
    public String getConciseIdentity() {
        return "type_list: " + getTypeListString("");
    }

    /** {@inheritDoc} */
    public int compareTo(TypeListItem o) {
        if (o == null) {
            return 1;
        }

        int thisSize = typeList.length;
        int otherSize = o.typeList.length;
        int size = Math.min(thisSize, otherSize);

        for (int i = 0; i < size; i++) {
            int result = typeList[i].compareTo(o.typeList[i]);
            if (result != 0) {
                return result;
            }
        }

        if (thisSize < otherSize) {
            return -1;
        } else if (thisSize > otherSize) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * @return the number of registers required for this <code>TypeListItem</code>
     */
    public int getRegisterCount() {
        int wordCount = 0;
        for (TypeIdItem typeIdItem: typeList) {
            wordCount += typeIdItem.getRegisterCount();
        }
        return wordCount;
    }

    /**
     * @return a string consisting of the type descriptors in this <code>TypeListItem</code>
     * that are separated by the given separator
     * @param separator the separator between each type
     */
    public String getTypeListString(String separator) {
        int size = 0;
        for (TypeIdItem typeIdItem: typeList) {
            size += typeIdItem.getTypeDescriptor().length();
            size += separator.length();
        }

        StringBuilder sb = new StringBuilder(size);
        for (TypeIdItem typeIdItem: typeList) {
            sb.append(typeIdItem.getTypeDescriptor());
            sb.append(separator);
        }
        if (typeList.length > 0) {
            sb.delete(sb.length() - separator.length(), sb.length());
        }
        return sb.toString();
    }

    /**
     * @return a string consisting of the shorty form of the type descriptors in this
     * <code>TypeListItem</code> that are directly concatenated together
     */
    public String getShortyString() {
        StringBuilder sb = new StringBuilder();
        for (TypeIdItem typeIdItem: typeList) {
            sb.append(typeIdItem.toShorty());
        }
        return sb.toString();
    }

    /**
     * @param index the index of the <code>TypeIdItem</code> to get
     * @return the <code>TypeIdItem</code> at the given index
     */
    public TypeIdItem getTypeIdItem(int index) {
        return typeList[index];
    }

    /**
     * @return the number of types in this <code>TypeListItem</code>
     */
    public int getTypeCount() {
        return typeList.length;
    }

    /**
     * @return an array of the <code>TypeIdItems</code> in this <code>TypeListItem</code>
     */
    public List<TypeIdItem> getTypes() {
        return new ReadOnlyArrayList<TypeIdItem>(typeList);
    }

    /**
     * Helper method to allow easier "inline" retrieval of of the list of TypeIdItems
     * @param typeListItem the typeListItem to return the types of (can be null)
     * @return an array of the <code>TypeIdItems</code> in the specified <code>TypeListItem</code>, or null if the
     * TypeListItem is null
     */
    public static List<TypeIdItem> getTypes(TypeListItem typeListItem) {
        return typeListItem==null?null:typeListItem.getTypes();
    }

    /**
     * calculate and cache the hashcode
     */
    private void calcHashCode() {
        int hashCode = 1;

        for (TypeIdItem typeIdItem: typeList) {
            hashCode = 31 * hashCode + typeIdItem.hashCode();
        }
        this.hashCode = hashCode;
    }

    @Override
    public int hashCode() {
        //there's a small possibility that the actual hash code will be 0. If so, we'll
        //just end up recalculating it each time
        if (hashCode == 0)
            calcHashCode();
        return hashCode;
    }

    @Override
    public boolean equals(Object o) {
        if (this==o) {
            return true;
        }
        if (o==null || !this.getClass().equals(o.getClass())) {
            return false;
        }

        //This assumes that the referenced items have been interned in both objects.
        //This is a valid assumption because all outside code must use the static
        //"getInterned..." style methods to make new items, and any item created
        //internally is guaranteed to be interned
        TypeListItem other = (TypeListItem)o;
        if (typeList.length != other.typeList.length) {
            return false;
        }

        for (int i=0; i<typeList.length; i++) {
            if (typeList[i] != other.typeList[i]) {
                return false;
            }
        }
        return true;
    }
}