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