DexBackedField.java revision d1662b67fecaf835227aff3a136949a2358ccd4e
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 DexBuffer dexBuf; 45 46 public final int accessFlags; 47 @Nullable public final EncodedValue initialValue; 48 public final int annotationSetOffset; 49 50 public final int fieldIndex; 51 52 private int fieldIdItemOffset; 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 DexReader reader, 59 int previousFieldIndex, 60 @Nonnull StaticInitialValueIterator staticInitialValueIterator, 61 @Nonnull AnnotationsDirectory.AnnotationIterator annotationIterator) { 62 this.dexBuf = reader.getDexBuffer(); 63 64 int fieldIndexDiff = reader.readSmallUleb128(); 65 this.fieldIndex = fieldIndexDiff + previousFieldIndex; 66 this.accessFlags = reader.readSmallUleb128(); 67 68 this.annotationSetOffset = annotationIterator.seekTo(fieldIndex); 69 this.initialValue = staticInitialValueIterator.getNextOrNull(); 70 } 71 72 @Nonnull 73 @Override 74 public String getName() { 75 return dexBuf.getString(dexBuf.readSmallUint(getFieldIdItemOffset() + NAME_OFFSET)); 76 } 77 78 @Nonnull 79 @Override 80 public String getType() { 81 return dexBuf.getType(dexBuf.readUshort(getFieldIdItemOffset() + TYPE_OFFSET)); 82 } 83 84 @Override public int getAccessFlags() { return accessFlags; } 85 @Nullable @Override public EncodedValue getInitialValue() { return initialValue; } 86 87 @Nonnull 88 @Override 89 public List<? extends DexBackedAnnotation> getAnnotations() { 90 return AnnotationsDirectory.getAnnotations(dexBuf, annotationSetOffset); 91 } 92 93 /** 94 * Skips the reader over a single encoded_field structure. 95 * @param reader The {@code DexFileReader} to skip 96 * @param previousFieldIndex The field index of the previous field, or 0 if this is the first 97 * @return The field index of the field that was skipped 98 */ 99 public static int skipEncodedField(@Nonnull DexReader reader, int previousFieldIndex) { 100 int idxDiff = reader.readSmallUleb128(); 101 reader.skipUleb128(); 102 return previousFieldIndex + idxDiff; 103 } 104 105 /** 106 * Skips the reader over the specified number of encoded_field structures 107 * 108 * @param reader The reader to skip 109 * @param count The number of encoded_field structures to skip over 110 */ 111 public static void skipAllFields(@Nonnull DexReader reader, int count) { 112 for (int i=0; i<count; i++) { 113 reader.skipUleb128(); 114 reader.skipUleb128(); 115 } 116 } 117 118 private int getFieldIdItemOffset() { 119 if (fieldIdItemOffset == 0) { 120 fieldIdItemOffset = dexBuf.getFieldIdItemOffset(fieldIndex); 121 } 122 return fieldIdItemOffset; 123 } 124} 125