1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dex.reader;
18 
19 import dex.reader.DexFileReader.FieldIdItem;
20 import dex.structure.DexAnnotation;
21 import dex.structure.DexAnnotationAttribute;
22 
23 import java.util.List;
24 
25 // FIXME provide special type for Signature annotation
26 /* package */final class DexAnnotationImpl implements DexAnnotation {
27 
28     private int offset;
29     private DexBuffer buffer;
30     private int[] typeIds;
31     private String[] stringPool;
32     private Visibility visibility;
33     private DexEncodedAnnotationImpl encodedAnnotation;
34 
35     private TypeFormatter formatter = new TypeFormatter();
36     private final FieldIdItem[] fieldIdItems;
37 
DexAnnotationImpl(DexBuffer buffer, int offset, int[] typeIds, String[] stringPool, FieldIdItem[] fieldIdItems)38     public DexAnnotationImpl(DexBuffer buffer, int offset, int[] typeIds,
39             String[] stringPool, FieldIdItem[] fieldIdItems) {
40         this.buffer = buffer;
41         this.offset = offset;
42         this.typeIds = typeIds;
43         this.stringPool = stringPool;
44         this.fieldIdItems = fieldIdItems;
45         parseAnnotations();
46     }
47 
parseAnnotations()48     private void parseAnnotations() {
49         buffer.setPosition(offset);
50         visibility = Visibility.get(buffer.readUByte());
51         encodedAnnotation = new DexEncodedAnnotationImpl(buffer, this, typeIds,
52                 stringPool, fieldIdItems);
53     }
54 
getAttributes()55     public List<DexAnnotationAttribute> getAttributes() {
56         return encodedAnnotation.getValue();
57     }
58 
getTypeName()59     public String getTypeName() {
60         return encodedAnnotation.getTypeName();
61     }
62 
getVisibility()63     public Visibility getVisibility() {
64         return visibility;
65     }
66 
67     @Override
toString()68     public String toString() {
69         StringBuilder builder = new StringBuilder();
70         builder.append("@");
71         builder.append(formatter.format(encodedAnnotation.getTypeName()));
72         if (!getAttributes().isEmpty()) {
73             builder.append(" (");
74             for (DexAnnotationAttribute value : getAttributes()) {
75                 builder.append(value.toString());
76                 builder.append(" ");
77             }
78             builder.append(")");
79         }
80         return builder.toString();
81     }
82 
83 }
84