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 public ParameterAnnotationEntry[] getParameterAnnotationEntries() {
195 if (parameterAnnotationEntries == null) {
196 parameterAnnotationEntries = ParameterAnnotationEntry.createParameterAnnotationEntries(getAttributes());
197 }
198 return parameterAnnotationEntries;
199 }
200
201
202
203
204 public Type getReturnType() {
205 return Type.getReturnType(getSignature());
206 }
207
208
209
210
211
212
213
214 @Override
215 public int hashCode() {
216 return bcelComparator.hashCode(this);
217 }
218
219
220
221
222
223
224
225 @Override
226 public String toString() {
227 final String access = Utility.accessToString(super.getAccessFlags());
228
229 ConstantUtf8 c = super.getConstantPool().getConstantUtf8(super.getSignatureIndex());
230 String signature = c.getBytes();
231 c = super.getConstantPool().getConstantUtf8(super.getNameIndex());
232 final String name = c.getBytes();
233 signature = Utility.methodSignatureToString(signature, name, access, true, getLocalVariableTable());
234 final StringBuilder buf = new StringBuilder(signature);
235 for (final Attribute attribute : super.getAttributes()) {
236 if (!(attribute instanceof Code || attribute instanceof ExceptionTable)) {
237 buf.append(" [").append(attribute).append("]");
238 }
239 }
240 final ExceptionTable e = getExceptionTable();
241 if (e != null) {
242 final String str = e.toString();
243 if (!str.isEmpty()) {
244 buf.append("\n\t\tthrows ").append(str);
245 }
246 }
247 return buf.toString();
248 }
249 }