DexBackedField.java revision 12b970ed4dfad768002335503e49c348ea0ed69b
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.base.reference.BaseFieldReference; 35import org.jf.dexlib2.dexbacked.util.AnnotationsDirectory; 36import org.jf.dexlib2.dexbacked.util.StaticInitialValueIterator; 37import org.jf.dexlib2.iface.ClassDef; 38import org.jf.dexlib2.iface.Field; 39import org.jf.dexlib2.iface.value.EncodedValue; 40 41import javax.annotation.Nonnull; 42import javax.annotation.Nullable; 43import java.util.List; 44 45public class DexBackedField extends BaseFieldReference implements Field { 46 @Nonnull public final DexBuffer dexBuf; 47 @Nonnull public final ClassDef classDef; 48 49 public final int accessFlags; 50 @Nullable public final EncodedValue initialValue; 51 public final int annotationSetOffset; 52 53 public final int fieldIndex; 54 55 private int fieldIdItemOffset; 56 57 // offsets for field_id_item 58 private static final int TYPE_OFFSET = 2; 59 private static final int NAME_OFFSET = 4; 60 61 public DexBackedField(@Nonnull DexReader reader, 62 @Nonnull DexBackedClassDef classDef, 63 int previousFieldIndex, 64 @Nonnull StaticInitialValueIterator staticInitialValueIterator, 65 @Nonnull AnnotationsDirectory.AnnotationIterator annotationIterator) { 66 this.dexBuf = reader.getDexBuffer(); 67 this.classDef = classDef; 68 69 int fieldIndexDiff = reader.readSmallUleb128(); 70 this.fieldIndex = fieldIndexDiff + previousFieldIndex; 71 this.accessFlags = reader.readSmallUleb128(); 72 73 this.annotationSetOffset = annotationIterator.seekTo(fieldIndex); 74 this.initialValue = staticInitialValueIterator.getNextOrNull(); 75 } 76 77 @Nonnull 78 @Override 79 public String getName() { 80 return dexBuf.getString(dexBuf.readSmallUint(getFieldIdItemOffset() + NAME_OFFSET)); 81 } 82 83 @Nonnull 84 @Override 85 public String getType() { 86 return dexBuf.getType(dexBuf.readUshort(getFieldIdItemOffset() + TYPE_OFFSET)); 87 } 88 89 @Nonnull @Override public String getContainingClass() { return classDef.getType(); } 90 @Override public int getAccessFlags() { return accessFlags; } 91 @Nullable @Override public EncodedValue getInitialValue() { return initialValue; } 92 93 @Nonnull 94 @Override 95 public List<? extends DexBackedAnnotation> getAnnotations() { 96 return AnnotationsDirectory.getAnnotations(dexBuf, annotationSetOffset); 97 } 98 99 /** 100 * Skips the reader over the specified number of encoded_field structures 101 * 102 * @param reader The reader to skip 103 * @param count The number of encoded_field structures to skip over 104 */ 105 public static void skipAllFields(@Nonnull DexReader reader, int count) { 106 for (int i=0; i<count; i++) { 107 reader.skipUleb128(); 108 reader.skipUleb128(); 109 } 110 } 111 112 private int getFieldIdItemOffset() { 113 if (fieldIdItemOffset == 0) { 114 fieldIdItemOffset = dexBuf.getFieldIdItemOffset(fieldIndex); 115 } 116 return fieldIdItemOffset; 117 } 118} 119