• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 package org.jf.dexlib2.dexbacked.raw;
33 
34 import org.jf.dexlib2.dexbacked.raw.util.DexAnnotator;
35 import org.jf.dexlib2.util.AnnotatedBytes;
36 
37 import javax.annotation.Nonnull;
38 import javax.annotation.Nullable;
39 
40 public class AnnotationDirectoryItem {
41     public static final int CLASS_ANNOTATIONS_OFFSET  = 0;
42     public static final int FIELD_SIZE_OFFSET = 4;
43     public static final int ANNOTATED_METHOD_SIZE_OFFSET = 8;
44     public static final int ANNOTATED_PARAMETERS_SIZE = 12;
45 
46     @Nonnull
makeAnnotator(@onnull DexAnnotator annotator, @Nonnull MapItem mapItem)47     public static SectionAnnotator makeAnnotator(@Nonnull DexAnnotator annotator, @Nonnull MapItem mapItem) {
48         return new SectionAnnotator(annotator, mapItem) {
49             @Nonnull @Override public String getItemName() {
50                 return "annotation_directory_item";
51             }
52 
53             @Override public int getItemAlignment() {
54                 return 4;
55             }
56 
57             @Override
58             protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
59                 int classAnnotationsOffset = dexFile.readSmallUint(out.getCursor());
60                 out.annotate(4, "class_annotations_off = %s",
61                         AnnotationSetItem.getReferenceAnnotation(dexFile, classAnnotationsOffset));
62 
63                 int fieldsSize = dexFile.readSmallUint(out.getCursor());
64                 out.annotate(4, "fields_size = %d", fieldsSize);
65 
66                 int annotatedMethodsSize = dexFile.readSmallUint(out.getCursor());
67                 out.annotate(4, "annotated_methods_size = %d", annotatedMethodsSize);
68 
69                 int annotatedParameterSize = dexFile.readSmallUint(out.getCursor());
70                 out.annotate(4, "annotated_parameters_size = %d", annotatedParameterSize);
71 
72                 if (fieldsSize > 0) {
73                     out.annotate(0, "field_annotations:");
74                     out.indent();
75                     for (int i=0; i<fieldsSize; i++) {
76                         out.annotate(0, "field_annotation[%d]", i);
77                         out.indent();
78                         int fieldIndex = dexFile.readSmallUint(out.getCursor());
79                         out.annotate(4, "%s", FieldIdItem.getReferenceAnnotation(dexFile, fieldIndex));
80                         int annotationOffset = dexFile.readSmallUint(out.getCursor());
81                         out.annotate(4, "%s", AnnotationSetItem.getReferenceAnnotation(dexFile, annotationOffset));
82                         out.deindent();
83                     }
84                     out.deindent();
85                 }
86 
87                 if (annotatedMethodsSize > 0) {
88                     out.annotate(0, "method_annotations:");
89                     out.indent();
90                     for (int i=0; i<annotatedMethodsSize; i++) {
91                         out.annotate(0, "method_annotation[%d]", i);
92                         out.indent();
93                         int methodIndex = dexFile.readSmallUint(out.getCursor());
94                         out.annotate(4, "%s", MethodIdItem.getReferenceAnnotation(dexFile, methodIndex));
95                         int annotationOffset = dexFile.readSmallUint(out.getCursor());
96                         out.annotate(4, "%s", AnnotationSetItem.getReferenceAnnotation(dexFile, annotationOffset));
97                         out.deindent();
98                     }
99                     out.deindent();
100                 }
101 
102                 if (annotatedParameterSize > 0) {
103                     out.annotate(0, "parameter_annotations:");
104                     out.indent();
105                     for (int i=0; i<annotatedParameterSize; i++) {
106                         out.annotate(0, "parameter_annotation[%d]", i);
107                         out.indent();
108                         int methodIndex = dexFile.readSmallUint(out.getCursor());
109                         out.annotate(4, "%s", MethodIdItem.getReferenceAnnotation(dexFile, methodIndex));
110                         int annotationOffset = dexFile.readSmallUint(out.getCursor());
111                         out.annotate(4, "%s", AnnotationSetRefList.getReferenceAnnotation(dexFile, annotationOffset));
112                         out.deindent();
113                     }
114                     out.deindent();
115                 }
116             }
117         };
118     }
119 }
120