ClassDefItem.java revision ff32d5cc147d4777b19e19692b3b196ddc460b51
1/* 2 * Copyright 2013, 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.raw; 33 34import com.google.common.base.Joiner; 35import org.jf.dexlib2.AccessFlags; 36import org.jf.dexlib2.dexbacked.DexBackedDexFile; 37import org.jf.dexlib2.util.AnnotatedBytes; 38 39import javax.annotation.Nonnull; 40 41public class ClassDefItem { 42 public static final int ITEM_SIZE = 32; 43 44 public static final int CLASS_OFFSET = 0; 45 public static final int ACCESS_FLAGS_OFFSET = 4; 46 public static final int SUPERCLASS_OFFSET = 8; 47 public static final int INTERFACES_OFFSET = 12; 48 public static final int SOURCE_FILE_OFFSET = 16; 49 public static final int ANNOTATIONS_OFFSET = 20; 50 public static final int CLASS_DATA_OFFSET = 24; 51 public static final int STATIC_VALUES_OFFSET = 28; 52 53 @Nonnull 54 public static SectionAnnotator getAnnotator() { 55 return new SectionAnnotator() { 56 @Nonnull @Override public String getItemName() { 57 return "class_def_item"; 58 } 59 60 @Override 61 protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { 62 int classIndex = dexFile.readSmallUint(out.getCursor()); 63 out.annotate(4, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex)); 64 65 int accessFlags = dexFile.readInt(out.getCursor()); 66 out.annotate(4, "access_flags = 0x%x: %s", accessFlags, 67 Joiner.on('|').join(AccessFlags.getAccessFlagsForClass(accessFlags))); 68 69 int superclassIndex = dexFile.readSmallUint(out.getCursor()); 70 out.annotate(4, "superclass_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, superclassIndex)); 71 72 int interfacesOffset = dexFile.readSmallUint(out.getCursor()); 73 out.annotate(4, "interfaces_off = %s", TypeListItem.getReferenceAnnotation(dexFile, interfacesOffset)); 74 75 int sourceFileIdx = dexFile.readOptionalUint(out.getCursor()); 76 if (sourceFileIdx == -1) { 77 out.annotate(4, "source_file_idx = -1"); 78 } else { 79 out.annotate(4, "source_file_idx = %s", StringIdItem.getReferenceAnnotation(dexFile, 80 sourceFileIdx)); 81 } 82 83 int annotationsOffset = dexFile.readSmallUint(out.getCursor()); 84 if (annotationsOffset == 0) { 85 out.annotate(4, "annotations_off = 0"); 86 } else { 87 out.annotate(4, "annotations_off = annotations_directory_item[0x%x]", annotationsOffset); 88 } 89 90 int classDataOffset = dexFile.readSmallUint(out.getCursor()); 91 if (classDataOffset == 0) { 92 out.annotate(4, "class_data_off = 0"); 93 } else { 94 out.annotate(4, "class_data_off = class_data_item[0x%x]", classDataOffset); 95 } 96 97 int staticValuesOffset = dexFile.readSmallUint(out.getCursor()); 98 if (staticValuesOffset == 0) { 99 out.annotate(4, "static_values_off = 0"); 100 } else { 101 out.annotate(4, "static_values_off = encoded_array_item[0x%x]", staticValuesOffset); 102 } 103 } 104 }; 105 } 106} 107