ClassDefItem.java revision a2b3cfe5f2c453ee649417ad7c5fc6072ca92588
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.raw.util.DexAnnotator;
37import org.jf.dexlib2.util.AnnotatedBytes;
38
39import javax.annotation.Nonnull;
40import javax.annotation.Nullable;
41
42public class ClassDefItem {
43    public static final int ITEM_SIZE = 32;
44
45    public static final int CLASS_OFFSET = 0;
46    public static final int ACCESS_FLAGS_OFFSET = 4;
47    public static final int SUPERCLASS_OFFSET = 8;
48    public static final int INTERFACES_OFFSET = 12;
49    public static final int SOURCE_FILE_OFFSET = 16;
50    public static final int ANNOTATIONS_OFFSET = 20;
51    public static final int CLASS_DATA_OFFSET = 24;
52    public static final int STATIC_VALUES_OFFSET = 28;
53
54    @Nonnull
55    public static SectionAnnotator makeAnnotator(@Nonnull DexAnnotator annotator, @Nonnull MapItem mapItem) {
56        return new SectionAnnotator(annotator, mapItem) {
57            private SectionAnnotator classDataAnnotator = null;
58
59            @Override public void annotateSection(@Nonnull AnnotatedBytes out) {
60                classDataAnnotator = annotator.getAnnotator(ItemType.CLASS_DATA_ITEM);
61                super.annotateSection(out);
62            }
63
64            @Nonnull @Override public String getItemName() {
65                return "class_def_item";
66            }
67
68            @Override
69            protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
70                int classIndex = dexFile.readSmallUint(out.getCursor());
71                out.annotate(4, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex));
72
73                int accessFlags = dexFile.readInt(out.getCursor());
74                out.annotate(4, "access_flags = 0x%x: %s", accessFlags,
75                        Joiner.on('|').join(AccessFlags.getAccessFlagsForClass(accessFlags)));
76
77                int superclassIndex = dexFile.readSmallUint(out.getCursor());
78                out.annotate(4, "superclass_idx = %s",
79                        TypeIdItem.getOptionalReferenceAnnotation(dexFile, superclassIndex));
80
81                int interfacesOffset = dexFile.readSmallUint(out.getCursor());
82                out.annotate(4, "interfaces_off = %s", TypeListItem.getReferenceAnnotation(dexFile, interfacesOffset));
83
84                int sourceFileIdx = dexFile.readOptionalUint(out.getCursor());
85                out.annotate(4, "source_file_idx = %s", StringIdItem.getOptionalReferenceAnnotation(dexFile,
86                        sourceFileIdx));
87
88                int annotationsOffset = dexFile.readSmallUint(out.getCursor());
89                if (annotationsOffset == 0) {
90                    out.annotate(4, "annotations_off = annotations_directory_item[NO_OFFSET]");
91                } else {
92                    out.annotate(4, "annotations_off = annotations_directory_item[0x%x]", annotationsOffset);
93                }
94
95                int classDataOffset = dexFile.readSmallUint(out.getCursor());
96                if (classDataOffset == 0) {
97                    out.annotate(4, "class_data_off = class_data_item[NO_OFFSET]");
98                } else {
99                    out.annotate(4, "class_data_off = class_data_item[0x%x]", classDataOffset);
100                    addClassDataIdentity(classDataOffset, dexFile.getType(classIndex));
101                }
102
103                int staticValuesOffset = dexFile.readSmallUint(out.getCursor());
104                if (staticValuesOffset == 0) {
105                    out.annotate(4, "static_values_off = encoded_array_item[NO_OFFSET]");
106                } else {
107                    out.annotate(4, "static_values_off = encoded_array_item[0x%x]", staticValuesOffset);
108                }
109            }
110
111            private void addClassDataIdentity(int classDataOffset, String classType) {
112                if (classDataAnnotator != null) {
113                    classDataAnnotator.setItemIdentity(classDataOffset, classType);
114                }
115            }
116        };
117    }
118}
119