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