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