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.Iterator;
23 import java.util.stream.Stream;
24
25 import org.apache.bcel.Const;
26
27
28
29
30
31
32 public abstract class Annotations extends Attribute implements Iterable<AnnotationEntry> {
33
34 private AnnotationEntry[] annotationTable;
35 private final boolean isRuntimeVisible;
36
37
38
39
40
41
42
43
44
45
46
47 public Annotations(final byte annotationType, final int nameIndex, final int length, final AnnotationEntry[] annotationTable,
48 final ConstantPool constantPool, final boolean isRuntimeVisible) {
49 super(annotationType, nameIndex, length, constantPool);
50 setAnnotationTable(annotationTable);
51 this.isRuntimeVisible = isRuntimeVisible;
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65 Annotations(final byte annotationType, final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool,
66 final boolean isRuntimeVisible) throws IOException {
67 this(annotationType, nameIndex, length, (AnnotationEntry[]) null, constantPool, isRuntimeVisible);
68 final int annotationTableLength = input.readUnsignedShort();
69 annotationTable = new AnnotationEntry[annotationTableLength];
70 for (int i = 0; i < annotationTableLength; i++) {
71 annotationTable[i] = AnnotationEntry.read(input, constantPool, isRuntimeVisible);
72 }
73 }
74
75
76
77
78
79
80
81
82 @Override
83 public void accept(final Visitor v) {
84 v.visitAnnotation(this);
85 }
86
87 @Override
88 public Attribute copy(final ConstantPool constantPool) {
89
90 return null;
91 }
92
93
94
95
96 public AnnotationEntry[] getAnnotationEntries() {
97 return annotationTable;
98 }
99
100
101
102
103
104
105 public final int getNumAnnotations() {
106 return annotationTable.length;
107 }
108
109 public boolean isRuntimeVisible() {
110 return isRuntimeVisible;
111 }
112
113 @Override
114 public Iterator<AnnotationEntry> iterator() {
115 return Stream.of(annotationTable).iterator();
116 }
117
118
119
120
121
122
123 public final void setAnnotationTable(final AnnotationEntry[] annotationTable) {
124 this.annotationTable = annotationTable != null ? annotationTable : AnnotationEntry.EMPTY_ARRAY;
125 }
126
127
128
129
130
131
132 @Override
133 public final String toString() {
134 final StringBuilder buf = new StringBuilder(Const.getAttributeName(getTag()));
135 buf.append(":\n");
136 for (int i = 0; i < annotationTable.length; i++) {
137 buf.append(" ").append(annotationTable[i]);
138 if (i < annotationTable.length - 1) {
139 buf.append('\n');
140 }
141 }
142 return buf.toString();
143 }
144
145 protected void writeAnnotations(final DataOutputStream dos) throws IOException {
146 dos.writeShort(annotationTable.length);
147 for (final AnnotationEntry element : annotationTable) {
148 element.dump(dos);
149 }
150 }
151
152 }