1 package serp.bytecode;
2
3 import serp.bytecode.visitor.*;
4 import serp.util.*;
5
6
7
8
9
10
11 public class BCMethod extends BCMember implements VisitAcceptor {
12 BCMethod(BCClass owner) {
13 super(owner);
14 }
15
16
17
18
19
20
21
22
23 public boolean isSynchronized() {
24 return (getAccessFlags() & Constants.ACCESS_SYNCHRONIZED) > 0;
25 }
26
27
28
29
30 public void setSynchronized(boolean on) {
31 if (on)
32 setAccessFlags(getAccessFlags() | Constants.ACCESS_SYNCHRONIZED);
33 else
34 setAccessFlags(getAccessFlags() & ~Constants.ACCESS_SYNCHRONIZED);
35 }
36
37
38
39
40 public boolean isNative() {
41 return (getAccessFlags() & Constants.ACCESS_NATIVE) > 0;
42 }
43
44
45
46
47 public void setNative(boolean on) {
48 if (on)
49 setAccessFlags(getAccessFlags() | Constants.ACCESS_NATIVE);
50 else
51 setAccessFlags(getAccessFlags() & ~Constants.ACCESS_NATIVE);
52 }
53
54
55
56
57 public boolean isAbstract() {
58 return (getAccessFlags() & Constants.ACCESS_ABSTRACT) > 0;
59 }
60
61
62
63
64 public void setAbstract(boolean on) {
65 if (on)
66 setAccessFlags(getAccessFlags() | Constants.ACCESS_ABSTRACT);
67 else
68 setAccessFlags(getAccessFlags() & ~Constants.ACCESS_ABSTRACT);
69 }
70
71
72
73
74 public boolean isStrict() {
75 return (getAccessFlags() & Constants.ACCESS_STRICT) > 0;
76 }
77
78
79
80
81 public void setStrict(boolean on) {
82 if (on)
83 setAccessFlags(getAccessFlags() | Constants.ACCESS_STRICT);
84 else
85 setAccessFlags(getAccessFlags() & ~Constants.ACCESS_STRICT);
86 }
87
88
89
90
91 public boolean isVarArgs() {
92 return (getAccessFlags() & Constants.ACCESS_VARARGS) > 0;
93 }
94
95
96
97
98 public void setVarArgs(boolean on) {
99 if (on)
100 setAccessFlags(getAccessFlags() | Constants.ACCESS_VARARGS);
101 else
102 setAccessFlags(getAccessFlags() & ~Constants.ACCESS_VARARGS);
103 }
104
105
106
107
108 public boolean isBridge() {
109 return (getAccessFlags() & Constants.ACCESS_BRIDGE) > 0;
110 }
111
112
113
114
115 public void setBridge(boolean on) {
116 if (on)
117 setAccessFlags(getAccessFlags() | Constants.ACCESS_BRIDGE);
118 else
119 setAccessFlags(getAccessFlags() & ~Constants.ACCESS_BRIDGE);
120 }
121
122
123
124
125
126
127
128
129
130
131
132 public String getReturnName() {
133 return getProject().getNameCache().getExternalForm(getProject().
134 getNameCache().getDescriptorReturnName(getDescriptor()), false);
135 }
136
137
138
139
140
141
142 public Class getReturnType() {
143 return Strings.toClass(getReturnName(), getClassLoader());
144 }
145
146
147
148
149
150
151 public BCClass getReturnBC() {
152 return getProject().loadClass(getReturnName(), getClassLoader());
153 }
154
155
156
157
158 public void setReturn(String name) {
159 setDescriptor(getProject().getNameCache().getDescriptor(name,
160 getParamNames()));
161 }
162
163
164
165
166 public void setReturn(Class type) {
167 setReturn(type.getName());
168 }
169
170
171
172
173 public void setReturn(BCClass type) {
174 setReturn(type.getName());
175 }
176
177
178
179
180
181
182
183
184
185
186
187 public String[] getParamNames() {
188
189 String[] params = getProject().getNameCache().getDescriptorParamNames
190 (getDescriptor());
191
192
193 for (int i = 0; i < params.length; i++)
194 params[i] = getProject().getNameCache().getExternalForm(params[i],
195 false);
196 return params;
197 }
198
199
200
201
202
203
204
205 public Class[] getParamTypes() {
206 String[] paramNames = getParamNames();
207 Class[] params = new Class[paramNames.length];
208 for (int i = 0; i < paramNames.length; i++)
209 params[i] = Strings.toClass(paramNames[i], getClassLoader());
210 return params;
211 }
212
213
214
215
216
217
218 public BCClass[] getParamBCs() {
219 String[] paramNames = getParamNames();
220 BCClass[] params = new BCClass[paramNames.length];
221 for (int i = 0; i < paramNames.length; i++)
222 params[i] = getProject().loadClass(paramNames[i], getClassLoader());
223 return params;
224 }
225
226
227
228
229
230
231 public void setParams(String[] names) {
232 if (names == null)
233 names = new String[0];
234 setDescriptor(getProject().getNameCache().getDescriptor(getReturnName(),
235 names));
236 }
237
238
239
240
241
242
243 public void setParams(Class[] types) {
244 if (types == null)
245 setParams((String[]) null);
246 else {
247 String[] names = new String[types.length];
248 for (int i = 0; i < types.length; i++)
249 names[i] = types[i].getName();
250 setParams(names);
251 }
252 }
253
254
255
256
257
258
259 public void setParams(BCClass[] types) {
260 if (types == null)
261 setParams((String[]) null);
262 else {
263 String[] names = new String[types.length];
264 for (int i = 0; i < types.length; i++)
265 names[i] = types[i].getName();
266 setParams(names);
267 }
268 }
269
270
271
272
273 public void addParam(String type) {
274 String[] origParams = getParamNames();
275 String[] params = new String[origParams.length + 1];
276 for (int i = 0; i < origParams.length; i++)
277 params[i] = origParams[i];
278 params[origParams.length] = type;
279 setParams(params);
280 }
281
282
283
284
285 public void addParam(Class type) {
286 addParam(type.getName());
287 }
288
289
290
291
292 public void addParam(BCClass type) {
293 addParam(type.getName());
294 }
295
296
297
298
299
300
301 public void addParam(int pos, String type) {
302 String[] origParams = getParamNames();
303 if ((pos < 0) || (pos >= origParams.length))
304 throw new IndexOutOfBoundsException("pos = " + pos);
305
306 String[] params = new String[origParams.length + 1];
307 for (int i = 0, index = 0; i < params.length; i++) {
308 if (i == pos)
309 params[i] = type;
310 else
311 params[i] = origParams[index++];
312 }
313 setParams(params);
314 }
315
316
317
318
319
320
321 public void addParam(int pos, Class type) {
322 addParam(pos, type.getName());
323 }
324
325
326
327
328
329
330 public void addParam(int pos, BCClass type) {
331 addParam(pos, type.getName());
332 }
333
334
335
336
337
338
339 public void setParam(int pos, String type) {
340 String[] origParams = getParamNames();
341 if ((pos < 0) || (pos >= origParams.length))
342 throw new IndexOutOfBoundsException("pos = " + pos);
343
344 String[] params = new String[origParams.length];
345 for (int i = 0; i < params.length; i++) {
346 if (i == pos)
347 params[i] = type;
348 else
349 params[i] = origParams[i];
350 }
351 setParams(params);
352 }
353
354
355
356
357
358
359 public void setParam(int pos, Class type) {
360 setParam(pos, type.getName());
361 }
362
363
364
365
366
367
368 public void setParam(int pos, BCClass type) {
369 setParam(pos, type.getName());
370 }
371
372
373
374
375 public void clearParams() {
376 setParams((String[]) null);
377 }
378
379
380
381
382 public void removeParam(int pos) {
383 String[] origParams = getParamNames();
384 if ((pos < 0) || (pos >= origParams.length))
385 throw new IndexOutOfBoundsException("pos = " + pos);
386
387 String[] params = new String[origParams.length - 1];
388
389 for (int i = 0, index = 0; i < origParams.length; i++)
390 if (i != pos)
391 params[index++] = origParams[i];
392 setParams(params);
393 }
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408 public Exceptions getExceptions(boolean add) {
409 Exceptions exceptions = (Exceptions) getAttribute
410 (Constants.ATTR_EXCEPTIONS);
411 if (!add || (exceptions != null))
412 return exceptions;
413
414 if (exceptions == null)
415 exceptions = (Exceptions) addAttribute(Constants.ATTR_EXCEPTIONS);
416 return exceptions;
417 }
418
419
420
421
422
423
424
425 public boolean removeExceptions() {
426 return removeAttribute(Constants.ATTR_EXCEPTIONS);
427 }
428
429
430
431
432
433
434
435
436
437
438
439 public Code getCode(boolean add) {
440 Code code = (Code) getAttribute(Constants.ATTR_CODE);
441 if (code != null) {
442 code.beforeFirst();
443 return code;
444 }
445 if (!add)
446 return null;
447 return (Code) addAttribute(Constants.ATTR_CODE);
448 }
449
450
451
452
453
454
455
456 public boolean removeCode() {
457 return removeAttribute(Constants.ATTR_CODE);
458 }
459
460
461
462
463
464 public void acceptVisit(BCVisitor visit) {
465 visit.enterBCMethod(this);
466 visitAttributes(visit);
467 visit.exitBCMethod(this);
468 }
469
470 void initialize(String name, String descriptor) {
471 super.initialize(name, descriptor);
472 makePublic();
473 }
474 }