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 this.annotationTable = 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 if (annotationTable == null) {
107 return 0;
108 }
109 return annotationTable.length;
110 }
111
112 public boolean isRuntimeVisible() {
113 return isRuntimeVisible;
114 }
115
116 @Override
117 public Iterator<AnnotationEntry> iterator() {
118 return Stream.of(annotationTable).iterator();
119 }
120
121
122
123
124
125
126 public final void setAnnotationTable(final AnnotationEntry[] annotationTable) {
127 this.annotationTable = annotationTable;
128 }
129
130
131
132
133
134
135 @Override
136 public final String toString() {
137 final StringBuilder buf = new StringBuilder(Const.getAttributeName(getTag()));
138 buf.append(":\n");
139 for (int i = 0; i < annotationTable.length; i++) {
140 buf.append(" ").append(annotationTable[i]);
141 if (i < annotationTable.length - 1) {
142 buf.append('\n');
143 }
144 }
145 return buf.toString();
146 }
147
148 protected void writeAnnotations(final DataOutputStream dos) throws IOException {
149 if (annotationTable == null) {
150 return;
151 }
152 dos.writeShort(annotationTable.length);
153 for (final AnnotationEntry element : annotationTable) {
154 element.dump(dos);
155 }
156 }
157
158 }