1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.bcel.classfile;
19
20 import java.io.DataInputStream;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.bcel.Constants;
28
29
30
31
32
33
34
35
36 public class AnnotationEntry implements Node, Constants, Serializable {
37
38 private static final long serialVersionUID = 1L;
39
40 private final int type_index;
41 private final ConstantPool constant_pool;
42 private final boolean isRuntimeVisible;
43
44 private List<ElementValuePair> element_value_pairs;
45
46
47
48
49
50
51
52
53
54
55 public static AnnotationEntry read(DataInputStream file, ConstantPool constant_pool, boolean isRuntimeVisible) throws IOException {
56
57 final AnnotationEntry annotationEntry = new AnnotationEntry(file.readUnsignedShort(), constant_pool, isRuntimeVisible);
58 final int num_element_value_pairs = (file.readUnsignedShort());
59 annotationEntry.element_value_pairs = new ArrayList<ElementValuePair>();
60 for (int i = 0; i < num_element_value_pairs; i++) {
61 annotationEntry.element_value_pairs.add(new ElementValuePair(file.readUnsignedShort(), ElementValue.readElementValue(file, constant_pool),
62 constant_pool));
63 }
64 return annotationEntry;
65 }
66
67 public AnnotationEntry(int type_index, ConstantPool constant_pool, boolean isRuntimeVisible) {
68 this.type_index = type_index;
69 this.constant_pool = constant_pool;
70 this.isRuntimeVisible = isRuntimeVisible;
71 }
72
73 public int getTypeIndex() {
74 return type_index;
75 }
76
77 public ConstantPool getConstantPool() {
78 return constant_pool;
79 }
80
81 public boolean isRuntimeVisible() {
82 return isRuntimeVisible;
83 }
84
85
86
87
88
89
90
91 public void accept(Visitor v) {
92
93 }
94
95
96
97
98 public String getAnnotationType() {
99 final ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(type_index, CONSTANT_Utf8);
100 return c.getBytes();
101 }
102
103
104
105
106 public int getAnnotationTypeIndex() {
107 return type_index;
108 }
109
110
111
112
113 public final int getNumElementValuePairs() {
114 return element_value_pairs.size();
115 }
116
117
118
119
120 public ElementValuePair[] getElementValuePairs() {
121
122 return element_value_pairs.toArray(new ElementValuePair[element_value_pairs.size()]);
123 }
124
125 public void dump(DataOutputStream dos) throws IOException {
126 dos.writeShort(type_index);
127 dos.writeShort(element_value_pairs.size());
128
129 for (int i = 0; i < element_value_pairs.size(); i++) {
130 final ElementValuePair envp = element_value_pairs.get(i);
131 envp.dump(dos);
132 }
133 }
134
135 public void addElementNameValuePair(ElementValuePair elementNameValuePair) {
136 element_value_pairs.add(elementNameValuePair);
137 }
138
139 public String toShortString() {
140 final StringBuilder result = new StringBuilder();
141 result.append("@");
142 result.append(getAnnotationType());
143 if (getElementValuePairs().length > 0) {
144 result.append("(");
145 for (int i = 0; i < getElementValuePairs().length; i++) {
146 final ElementValuePair element = getElementValuePairs()[i];
147 result.append(element.toShortString());
148 }
149 result.append(")");
150 }
151 return result.toString();
152 }
153 }