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