DexBackedField.java revision 25d385a441b95d56987c81f00f19297d799e1b31
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 @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 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 int fieldIdItemOffset = reader.getFieldIdItemOffset(fieldIndex); 72 this.type = reader.getType(reader.readUshort(fieldIdItemOffset + TYPE_OFFSET)); 73 this.name = reader.getString(reader.readSmallUint(fieldIdItemOffset + NAME_OFFSET)); 74 } 75 76 @Nonnull @Override public String getName() { return name; } 77 @Nonnull @Override public String getType() { return type; } 78 @Override public int getAccessFlags() { return accessFlags; } 79 @Nullable @Override public EncodedValue getInitialValue() { return initialValue; } 80 81 @Nonnull 82 @Override 83 public List<? extends DexBackedAnnotation> getAnnotations() { 84 return AnnotationsDirectory.getAnnotations(dexBuf, annotationSetOffset); 85 } 86 87 /** 88 * Skips the reader over a single encoded_field structure. 89 * @param reader The {@code DexFileReader} to skip 90 * @param previousFieldIndex The field index of the previous field, or 0 if this is the first 91 * @return The field index of the field that was skipped 92 */ 93 public static int skipEncodedField(@Nonnull DexReader reader, int previousFieldIndex) { 94 int idxDiff = reader.readSmallUleb128(); 95 reader.skipUleb128(); 96 return previousFieldIndex + idxDiff; 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