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