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.DataInputStream;
23 import java.io.DataOutputStream;
24 import java.io.IOException;
25 import java.util.Arrays;
26
27
28
29
30 public abstract class FieldOrMethod extends AccessFlags implements Cloneable, Node {
31
32
33
34
35 @java.lang.Deprecated
36 protected int name_index;
37
38
39
40
41 @java.lang.Deprecated
42 protected int signature_index;
43
44
45
46
47 @java.lang.Deprecated
48 protected Attribute[] attributes;
49
50
51
52
53 @java.lang.Deprecated
54 protected int attributes_count;
55
56
57 private AnnotationEntry[] annotationEntries;
58
59
60
61
62 @java.lang.Deprecated
63 protected ConstantPool constant_pool;
64
65 private String signatureAttributeString;
66 private boolean searchedForSignatureAttribute;
67
68 FieldOrMethod() {
69 }
70
71
72
73
74
75
76
77 protected FieldOrMethod(final DataInput file, final ConstantPool constantPool) throws IOException {
78 this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), null, constantPool);
79 final int attributesCount = file.readUnsignedShort();
80 attributes = new Attribute[attributesCount];
81 for (int i = 0; i < attributesCount; i++) {
82 attributes[i] = Attribute.readAttribute(file, constantPool);
83 }
84 this.attributes_count = attributesCount;
85 }
86
87
88
89
90
91
92
93
94 @java.lang.Deprecated
95 protected FieldOrMethod(final DataInputStream file, final ConstantPool constantPool) throws IOException {
96 this((DataInput) file, constantPool);
97 }
98
99
100
101
102
103
104
105 protected FieldOrMethod(final FieldOrMethod c) {
106 this(c.getAccessFlags(), c.getNameIndex(), c.getSignatureIndex(), c.getAttributes(), c.getConstantPool());
107 }
108
109
110
111
112
113
114
115
116 protected FieldOrMethod(final int accessFlags, final int nameIndex, final int signatureIndex, final Attribute[] attributes,
117 final ConstantPool constantPool) {
118 super(accessFlags);
119 this.name_index = nameIndex;
120 this.signature_index = signatureIndex;
121 this.constant_pool = constantPool;
122 setAttributes(attributes);
123 }
124
125
126
127
128 protected FieldOrMethod copy_(final ConstantPool constantPool) {
129 try {
130 final FieldOrMethod c = (FieldOrMethod) clone();
131 c.constant_pool = constantPool;
132 c.attributes = new Attribute[attributes.length];
133 c.attributes_count = attributes_count;
134 Arrays.setAll(c.attributes, i -> attributes[i].copy(constantPool));
135 return c;
136 } catch (final CloneNotSupportedException e) {
137 throw new UnsupportedOperationException(e);
138 }
139 }
140
141
142
143
144
145
146
147 public final void dump(final DataOutputStream file) throws IOException {
148 file.writeShort(super.getAccessFlags());
149 file.writeShort(name_index);
150 file.writeShort(signature_index);
151 file.writeShort(attributes_count);
152 for (final Attribute attribute : attributes) {
153 attribute.dump(file);
154 }
155 }
156
157
158
159
160
161 public AnnotationEntry[] getAnnotationEntries() {
162 if (annotationEntries == null) {
163 annotationEntries = AnnotationEntry.createAnnotationEntries(getAttributes());
164 }
165
166 return annotationEntries;
167 }
168
169
170
171
172
173
174
175 @SuppressWarnings("unchecked")
176 public final <T extends Attribute> T getAttribute(final byte tag) {
177 for (final Attribute attribute : getAttributes()) {
178 if (attribute.getTag() == tag) {
179 return (T) attribute;
180 }
181 }
182 return null;
183 }
184
185
186
187
188 public final Attribute[] getAttributes() {
189 return attributes;
190 }
191
192
193
194
195 public final ConstantPool getConstantPool() {
196 return constant_pool;
197 }
198
199
200
201
202
203
204
205
206 public final String getGenericSignature() {
207 if (!searchedForSignatureAttribute) {
208 boolean found = false;
209 for (int i = 0; !found && i < attributes.length; i++) {
210 if (attributes[i] instanceof Signature) {
211 signatureAttributeString = ((Signature) attributes[i]).getSignature();
212 found = true;
213 }
214 }
215 searchedForSignatureAttribute = true;
216 }
217 return signatureAttributeString;
218 }
219
220
221
222
223 public final String getName() {
224 return constant_pool.getConstantUtf8(name_index).getBytes();
225 }
226
227
228
229
230 public final int getNameIndex() {
231 return name_index;
232 }
233
234
235
236
237 public final String getSignature() {
238 return constant_pool.getConstantUtf8(signature_index).getBytes();
239 }
240
241
242
243
244 public final int getSignatureIndex() {
245 return signature_index;
246 }
247
248
249
250
251 public final void setAttributes(final Attribute[] attributes) {
252 this.attributes = attributes != null ? attributes : Attribute.EMPTY_ARRAY;
253 this.attributes_count = this.attributes.length;
254 }
255
256
257
258
259 public final void setConstantPool(final ConstantPool constantPool) {
260 this.constant_pool = constantPool;
261 }
262
263
264
265
266 public final void setNameIndex(final int nameIndex) {
267 this.name_index = nameIndex;
268 }
269
270
271
272
273 public final void setSignatureIndex(final int signatureIndex) {
274 this.signature_index = signatureIndex;
275 }
276 }