DexBackedField.java revision db389aa3a1d898d3a452f3f0b2220b334b23cb4c
1/*
2 * Copyright 2012, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.dexlib2.dexbacked;
33
34import org.jf.dexlib2.dexbacked.util.AnnotationsDirectory;
35import org.jf.dexlib2.dexbacked.util.StaticInitialValueIterator;
36import org.jf.dexlib2.iface.ClassDef;
37import org.jf.dexlib2.iface.Field;
38import org.jf.dexlib2.iface.value.EncodedValue;
39
40import javax.annotation.Nonnull;
41import javax.annotation.Nullable;
42import java.util.List;
43
44public class DexBackedField implements Field {
45    @Nonnull public final DexBuffer dexBuf;
46    @Nonnull public final ClassDef classDef;
47
48    public final int accessFlags;
49    @Nullable public final EncodedValue initialValue;
50    public final int annotationSetOffset;
51
52    public final int fieldIndex;
53
54    private int fieldIdItemOffset;
55
56    // offsets for field_id_item
57    private static final int TYPE_OFFSET = 2;
58    private static final int NAME_OFFSET = 4;
59
60    public DexBackedField(@Nonnull DexReader reader,
61                          @Nonnull DexBackedClassDef classDef,
62                          int previousFieldIndex,
63                          @Nonnull StaticInitialValueIterator staticInitialValueIterator,
64                          @Nonnull AnnotationsDirectory.AnnotationIterator annotationIterator) {
65        this.dexBuf = reader.getDexBuffer();
66        this.classDef = classDef;
67
68        int fieldIndexDiff = reader.readSmallUleb128();
69        this.fieldIndex = fieldIndexDiff + previousFieldIndex;
70        this.accessFlags = reader.readSmallUleb128();
71
72        this.annotationSetOffset = annotationIterator.seekTo(fieldIndex);
73        this.initialValue = staticInitialValueIterator.getNextOrNull();
74    }
75
76    @Nonnull
77    @Override
78    public String getName() {
79        return dexBuf.getString(dexBuf.readSmallUint(getFieldIdItemOffset() + NAME_OFFSET));
80    }
81
82    @Nonnull
83    @Override
84    public String getType() {
85        return dexBuf.getType(dexBuf.readUshort(getFieldIdItemOffset() + TYPE_OFFSET));
86    }
87
88    @Nonnull @Override public String getContainingClass() { return classDef.getType(); }
89    @Override public int getAccessFlags() { return accessFlags; }
90    @Nullable @Override public EncodedValue getInitialValue() { return initialValue; }
91
92    @Nonnull
93    @Override
94    public List<? extends DexBackedAnnotation> getAnnotations() {
95        return AnnotationsDirectory.getAnnotations(dexBuf, annotationSetOffset);
96    }
97
98    /**
99     * Skips the reader over a single encoded_field structure.
100     * @param reader The {@code DexFileReader} to skip
101     * @param previousFieldIndex The field index of the previous field, or 0 if this is the first
102     * @return The field index of the field that was skipped
103     */
104    public static int skipEncodedField(@Nonnull DexReader reader, int previousFieldIndex) {
105        int idxDiff = reader.readSmallUleb128();
106        reader.skipUleb128();
107        return previousFieldIndex + idxDiff;
108    }
109
110    /**
111     * Skips the reader over the specified number of encoded_field structures
112     *
113     * @param reader The reader to skip
114     * @param count The number of encoded_field structures to skip over
115     */
116    public static void skipAllFields(@Nonnull DexReader reader, int count) {
117        for (int i=0; i<count; i++) {
118            reader.skipUleb128();
119            reader.skipUleb128();
120        }
121    }
122
123    private int getFieldIdItemOffset() {
124        if (fieldIdItemOffset == 0) {
125            fieldIdItemOffset = dexBuf.getFieldIdItemOffset(fieldIndex);
126        }
127        return fieldIdItemOffset;
128    }
129}
130