DexBackedField.java revision dc9e5455bc40510088daa43ec6a3bca77ffc48bc
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.Field; 37import org.jf.dexlib2.iface.value.EncodedValue; 38 39import javax.annotation.Nonnull; 40import javax.annotation.Nullable; 41import java.util.List; 42 43public class DexBackedField implements Field { 44 @Nonnull public final DexFileBuffer dexFile; 45 46 @Nonnull public final String name; 47 @Nonnull public final String type; 48 public final int accessFlags; 49 @Nullable public final EncodedValue initialValue; 50 public final int annotationSetOffset; 51 52 public final int fieldIndex; 53 54 // offsets for field_id_item 55 private static final int TYPE_OFFSET = 2; 56 private static final int NAME_OFFSET = 4; 57 58 public DexBackedField(@Nonnull DexFileReader dexFileReader, 59 int previousFieldIndex, 60 @Nonnull StaticInitialValueIterator staticInitialValueIterator, 61 @Nonnull AnnotationsDirectory.AnnotationIterator annotationIterator) { 62 this.dexFile = dexFileReader.getDexFile(); 63 64 int fieldIndexDiff = dexFileReader.readSmallUleb128(); 65 this.fieldIndex = fieldIndexDiff + previousFieldIndex; 66 this.accessFlags = dexFileReader.readSmallUleb128(); 67 68 this.annotationSetOffset = annotationIterator.seekTo(fieldIndex); 69 this.initialValue = staticInitialValueIterator.getNextOrNull(); 70 71 int fieldIdItemOffset = dexFileReader.getFieldIdItemOffset(fieldIndex); 72 this.type = dexFileReader.getType(dexFileReader.readUshort(fieldIdItemOffset + TYPE_OFFSET)); 73 this.name = dexFileReader.getString(dexFileReader.readSmallUint(fieldIdItemOffset + NAME_OFFSET)); 74 } 75 76 77 @Nonnull @Override public String getName() { return name; } 78 @Nonnull @Override public String getType() { return type; } 79 @Override public int getAccessFlags() { return accessFlags; } 80 @Nullable @Override public EncodedValue getInitialValue() { return initialValue; } 81 82 @Nonnull 83 @Override 84 public List<? extends DexBackedAnnotation> getAnnotations() { 85 return AnnotationsDirectory.getAnnotations(dexFile, annotationSetOffset); 86 } 87 88 /** 89 * Skips the reader over a single encoded_field structure. 90 * @param dexFileReader The {@code DexFileReader} to skip 91 * @param previousFieldIndex The field index of the previous field, or 0 if this is the first 92 * @return The field index of the field that was skipped 93 */ 94 public static int skipEncodedField(@Nonnull DexFileReader dexFileReader, int previousFieldIndex) { 95 int idxDiff = dexFileReader.readSmallUleb128(); 96 dexFileReader.skipUleb128(); 97 return previousFieldIndex + idxDiff; 98 } 99 100 /** 101 * Skips the reader over the specified number of encoded_field structures 102 * 103 * @param dexFileReader The reader to skip 104 * @param count The number of encoded_field structures to skip over 105 */ 106 public static void skipAllFields(@Nonnull DexFileReader dexFileReader, int count) { 107 for (int i=0; i<count; i++) { 108 dexFileReader.skipUleb128(); 109 dexFileReader.skipUleb128(); 110 } 111 } 112} 113