AnnotationEncodedSubValue.java revision 4d68e05fb5e3262c58bc9896befe910698daa6a8
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.EncodedValue;
30
31import org.jf.dexlib.TypeIdItem;
32import org.jf.dexlib.StringIdItem;
33import org.jf.dexlib.DexFile;
34import org.jf.dexlib.Util.Input;
35import org.jf.dexlib.Util.AnnotatedOutput;
36import org.jf.dexlib.Util.Leb128Utils;
37
38/**
39 * An <code>AnnotationEncodedSubValue</code> is identical to an <code>AnnotationEncodedValue</code>, except that it
40 * doesn't have the initial valueType/valueArg byte. This is used in the <code>AnnotationItem</code> object
41 */
42public class AnnotationEncodedSubValue extends EncodedValue {
43    private int hashCode = 0;
44
45    public final TypeIdItem annotationType;
46    public final StringIdItem[] names;
47    public final EncodedValue[] values;
48
49    /**
50    * Constructs a new <code>AnnotationEncodedSubValue</code> by reading the value from the given <code>Input</code>
51    * object.
52    * @param dexFile The <code>DexFile</code> that is being read in
53    * @param in The <code>Input</code> object to read from
54    */
55    public AnnotationEncodedSubValue(DexFile dexFile, Input in) {
56        annotationType = dexFile.TypeIdsSection.getItemByIndex(in.readUnsignedLeb128());
57        names = new StringIdItem[in.readUnsignedLeb128()];
58        values = new EncodedValue[names.length];
59
60        for (int i=0; i<names.length; i++) {
61            names[i] = dexFile.StringIdsSection.getItemByIndex(in.readUnsignedLeb128());
62            values[i] = EncodedValue.readEncodedValue(dexFile, in);
63        }
64    }
65
66    /**
67     * Constructs a new <code>AnnotationEncodedValue</code> with the given values. names and values must be the same
68     * length, and must be sorted according to the name
69     * @param annotationType The type of the annotation
70     * @param names An array of the names of the elements of the annotation
71     * @param values An array of the values of the elements on the annotation
72     */
73    public AnnotationEncodedSubValue(TypeIdItem annotationType, StringIdItem[] names, EncodedValue[] values) {
74        this.annotationType = annotationType;
75        if (names.length != values.length) {
76            throw new RuntimeException("The names and values parameters must be the same length");
77        }
78        this.names = names;
79        this.values = values;
80    }
81
82    /** {@inheritDoc} */
83    public void writeValue(AnnotatedOutput out) {
84        out.annotate("annotation_type: " + annotationType.getTypeDescriptor());
85        out.writeUnsignedLeb128(annotationType.getIndex());
86        out.annotate("element_count: 0x" + Integer.toHexString(names.length) + " (" + names.length + ")");
87        out.writeUnsignedLeb128(names.length);
88
89        for (int i=0; i<names.length; i++) {
90            out.annotate(0, "[" + i + "] annotation_element");
91            out.indent();
92            out.annotate("element_name: " + names[i].getStringValue());
93            out.writeUnsignedLeb128(names[i].getIndex());
94            out.annotate(0, "element_value:");
95            out.indent();
96            values[i].writeValue(out);
97            out.deindent();
98            out.deindent();
99        }
100    }
101
102    /** {@inheritDoc} */
103    public int placeValue(int offset) {
104        offset = offset + Leb128Utils.unsignedLeb128Size(annotationType.getIndex());
105        offset = offset + Leb128Utils.unsignedLeb128Size(names.length);
106
107        for (int i=0; i<names.length; i++) {
108            offset = offset + Leb128Utils.unsignedLeb128Size(names[i].getIndex());
109            offset = values[i].placeValue(offset);
110        }
111
112        return offset;
113    }
114
115    /** {@inheritDoc} */
116    protected int compareValue(EncodedValue o) {
117        AnnotationEncodedSubValue other = (AnnotationEncodedSubValue)o;
118
119        int comp = annotationType.compareTo(other.annotationType);
120        if (comp != 0) {
121            return comp;
122        }
123
124        comp = names.length - other.names.length;
125        if (comp != 0) {
126            return comp;
127        }
128
129        for (int i=0; i<names.length; i++) {
130            comp = names[i].compareTo(other.names[i]);
131            if (comp != 0) {
132                return comp;
133            }
134
135            comp = values[i].compareTo(other.values[i]);
136            if (comp != 0) {
137                return comp;
138            }
139        }
140
141        return comp;
142    }
143
144    /** {@inheritDoc} */
145    public ValueType getValueType() {
146        return ValueType.VALUE_ANNOTATION;
147    }
148
149    /**
150     * calculate and cache the hashcode
151     */
152    private void calcHashCode() {
153        hashCode = annotationType.hashCode();
154
155        for (int i=0; i<names.length; i++) {
156            hashCode = 31 * hashCode + names[i].hashCode();
157            hashCode = 31 * hashCode + values[i].hashCode();
158        }
159    }
160
161    @Override
162    public int hashCode() {
163        //there's a small possibility that the actual hash code will be 0. If so, we'll
164        //just end up recalculating it each time
165        if (hashCode == 0)
166            calcHashCode();
167        return hashCode;
168    }
169}
170