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.IOException;
23 import java.util.Objects;
24
25 import org.apache.bcel.generic.Type;
26 import org.apache.bcel.util.BCELComparator;
27
28
29
30
31
32 public final class Method extends FieldOrMethod {
33
34
35
36
37
38
39 public static final Method[] EMPTY_ARRAY = {};
40
41 private static BCELComparator<Method> bcelComparator = new BCELComparator<Method>() {
42
43 @Override
44 public boolean equals(final Method a, final Method b) {
45 return a == b || a != null && b != null && Objects.equals(a.getName(), b.getName()) && Objects.equals(a.getSignature(), b.getSignature());
46 }
47
48 @Override
49 public int hashCode(final Method o) {
50 return o != null ? Objects.hash(o.getSignature(), o.getName()) : 0;
51 }
52 };
53
54
55
56
57 public static BCELComparator<Method> getComparator() {
58 return bcelComparator;
59 }
60
61
62
63
64 public static void setComparator(final BCELComparator<Method> comparator) {
65 bcelComparator = comparator;
66 }
67
68
69 private ParameterAnnotationEntry[] parameterAnnotationEntries;
70
71
72
73
74 public Method() {
75 }
76
77
78
79
80
81
82
83
84 Method(final DataInput file, final ConstantPool constantPool) throws IOException, ClassFormatException {
85 super(file, constantPool);
86 }
87
88
89
90
91
92
93
94
95 public Method(final int accessFlags, final int nameIndex, final int signatureIndex, final Attribute[] attributes, final ConstantPool constantPool) {
96 super(accessFlags, nameIndex, signatureIndex, attributes, constantPool);
97 }
98
99
100
101
102
103
104
105 public Method(final Method c) {
106 super(c);
107 }
108
109
110
111
112
113
114
115 @Override
116 public void accept(final Visitor v) {
117 v.visitMethod(this);
118 }
119
120
121
122
123 public Method copy(final ConstantPool constantPool) {
124 return (Method) copy_(constantPool);
125 }
126
127
128
129
130
131
132
133 @Override
134 public boolean equals(final Object obj) {
135 return obj instanceof Method && bcelComparator.equals(this, (Method) obj);
136 }
137
138
139
140
141 public Type[] getArgumentTypes() {
142 return Type.getArgumentTypes(getSignature());
143 }
144
145
146
147
148 public Code getCode() {
149 for (final Attribute attribute : super.getAttributes()) {
150 if (attribute instanceof Code) {
151 return (Code) attribute;
152 }
153 }
154 return null;
155 }
156
157
158
159
160
161 public ExceptionTable getExceptionTable() {
162 for (final Attribute attribute : super.getAttributes()) {
163 if (attribute instanceof ExceptionTable) {
164 return (ExceptionTable) attribute;
165 }
166 }
167 return null;
168 }
169
170
171
172
173 public LineNumberTable getLineNumberTable() {
174 final Code code = getCode();
175 if (code == null) {
176 return null;
177 }
178 return code.getLineNumberTable();
179 }
180
181
182
183
184 public LocalVariableTable getLocalVariableTable() {
185 final Code code = getCode();
186 if (code == null) {
187 return null;
188 }
189 return code.getLocalVariableTable();
190 }
191
192
193
194
195
196
197 public LocalVariableTypeTable getLocalVariableTypeTable() {
198 final Code code = getCode();
199 if (code == null) {
200 return null;
201 }
202 return code.getLocalVariableTypeTable();
203 }
204
205
206
207
208
209 public ParameterAnnotationEntry[] getParameterAnnotationEntries() {
210 if (parameterAnnotationEntries == null) {
211 parameterAnnotationEntries = ParameterAnnotationEntry.createParameterAnnotationEntries(getAttributes());
212 }
213 return parameterAnnotationEntries;
214 }
215
216
217
218
219 public Type getReturnType() {
220 return Type.getReturnType(getSignature());
221 }
222
223
224
225
226
227
228
229 @Override
230 public int hashCode() {
231 return bcelComparator.hashCode(this);
232 }
233
234
235
236
237
238
239
240 @Override
241 public String toString() {
242 final String access = Utility.accessToString(super.getAccessFlags());
243
244 ConstantUtf8 c = super.getConstantPool().getConstantUtf8(super.getSignatureIndex());
245 String signature = c.getBytes();
246 c = super.getConstantPool().getConstantUtf8(super.getNameIndex());
247 final String name = c.getBytes();
248 signature = Utility.methodSignatureToString(signature, name, access, true, getLocalVariableTable());
249 final StringBuilder buf = new StringBuilder(signature);
250 for (final Attribute attribute : super.getAttributes()) {
251 if (!(attribute instanceof Code || attribute instanceof ExceptionTable)) {
252 buf.append(" [").append(attribute).append("]");
253 }
254 }
255 final ExceptionTable e = getExceptionTable();
256 if (e != null) {
257 final String str = e.toString();
258 if (!str.isEmpty()) {
259 buf.append("\n\t\tthrows ").append(str);
260 }
261 }
262 return buf.toString();
263 }
264 }