6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
package com.sun.org.apache.bcel.internal.generic;
|
|
6 |
|
|
7 |
/* ====================================================================
|
|
8 |
* The Apache Software License, Version 1.1
|
|
9 |
*
|
|
10 |
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
|
11 |
* reserved.
|
|
12 |
*
|
|
13 |
* Redistribution and use in source and binary forms, with or without
|
|
14 |
* modification, are permitted provided that the following conditions
|
|
15 |
* are met:
|
|
16 |
*
|
|
17 |
* 1. Redistributions of source code must retain the above copyright
|
|
18 |
* notice, this list of conditions and the following disclaimer.
|
|
19 |
*
|
|
20 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
21 |
* notice, this list of conditions and the following disclaimer in
|
|
22 |
* the documentation and/or other materials provided with the
|
|
23 |
* distribution.
|
|
24 |
*
|
|
25 |
* 3. The end-user documentation included with the redistribution,
|
|
26 |
* if any, must include the following acknowledgment:
|
|
27 |
* "This product includes software developed by the
|
|
28 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
29 |
* Alternately, this acknowledgment may appear in the software itself,
|
|
30 |
* if and wherever such third-party acknowledgments normally appear.
|
|
31 |
*
|
|
32 |
* 4. The names "Apache" and "Apache Software Foundation" and
|
|
33 |
* "Apache BCEL" must not be used to endorse or promote products
|
|
34 |
* derived from this software without prior written permission. For
|
|
35 |
* written permission, please contact apache@apache.org.
|
|
36 |
*
|
|
37 |
* 5. Products derived from this software may not be called "Apache",
|
|
38 |
* "Apache BCEL", nor may "Apache" appear in their name, without
|
|
39 |
* prior written permission of the Apache Software Foundation.
|
|
40 |
*
|
|
41 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
42 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
43 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
44 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
45 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
46 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
47 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
48 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
49 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
50 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
51 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
52 |
* SUCH DAMAGE.
|
|
53 |
* ====================================================================
|
|
54 |
*
|
|
55 |
* This software consists of voluntary contributions made by many
|
|
56 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
57 |
* information on the Apache Software Foundation, please see
|
|
58 |
* <http://www.apache.org/>.
|
|
59 |
*/
|
|
60 |
|
|
61 |
import com.sun.org.apache.bcel.internal.Constants;
|
|
62 |
import com.sun.org.apache.bcel.internal.classfile.*;
|
|
63 |
import java.util.*;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Template class for building up a method. This is done by defining exception
|
|
67 |
* handlers, adding thrown exceptions, local variables and attributes, whereas
|
|
68 |
* the `LocalVariableTable' and `LineNumberTable' attributes will be set
|
|
69 |
* automatically for the code. Use stripAttributes() if you don't like this.
|
|
70 |
*
|
|
71 |
* While generating code it may be necessary to insert NOP operations. You can
|
|
72 |
* use the `removeNOPs' method to get rid off them.
|
|
73 |
* The resulting method object can be obtained via the `getMethod()' method.
|
|
74 |
*
|
|
75 |
* @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
|
|
76 |
* @author <A HREF="http://www.vmeng.com/beard">Patrick C. Beard</A> [setMaxStack()]
|
|
77 |
* @see InstructionList
|
|
78 |
* @see Method
|
|
79 |
*/
|
|
80 |
public class MethodGen extends FieldGenOrMethodGen {
|
|
81 |
private String class_name;
|
|
82 |
private Type[] arg_types;
|
|
83 |
private String[] arg_names;
|
|
84 |
private int max_locals;
|
|
85 |
private int max_stack;
|
|
86 |
private InstructionList il;
|
|
87 |
private boolean strip_attributes;
|
|
88 |
|
|
89 |
private ArrayList variable_vec = new ArrayList();
|
|
90 |
private ArrayList line_number_vec = new ArrayList();
|
|
91 |
private ArrayList exception_vec = new ArrayList();
|
|
92 |
private ArrayList throws_vec = new ArrayList();
|
|
93 |
private ArrayList code_attrs_vec = new ArrayList();
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Declare method. If the method is non-static the constructor
|
|
97 |
* automatically declares a local variable `$this' in slot 0. The
|
|
98 |
* actual code is contained in the `il' parameter, which may further
|
|
99 |
* manipulated by the user. But he must take care not to remove any
|
|
100 |
* instruction (handles) that are still referenced from this object.
|
|
101 |
*
|
|
102 |
* For example one may not add a local variable and later remove the
|
|
103 |
* instructions it refers to without causing havoc. It is safe
|
|
104 |
* however if you remove that local variable, too.
|
|
105 |
*
|
|
106 |
* @param access_flags access qualifiers
|
|
107 |
* @param return_type method type
|
|
108 |
* @param arg_types argument types
|
|
109 |
* @param arg_names argument names (if this is null, default names will be provided
|
|
110 |
* for them)
|
|
111 |
* @param method_name name of method
|
|
112 |
* @param class_name class name containing this method (may be null, if you don't care)
|
|
113 |
* @param il instruction list associated with this method, may be null only for
|
|
114 |
* abstract or native methods
|
|
115 |
* @param cp constant pool
|
|
116 |
*/
|
|
117 |
public MethodGen(int access_flags, Type return_type, Type[] arg_types,
|
|
118 |
String[] arg_names, String method_name, String class_name,
|
|
119 |
InstructionList il, ConstantPoolGen cp) {
|
|
120 |
setAccessFlags(access_flags);
|
|
121 |
setType(return_type);
|
|
122 |
setArgumentTypes(arg_types);
|
|
123 |
setArgumentNames(arg_names);
|
|
124 |
setName(method_name);
|
|
125 |
setClassName(class_name);
|
|
126 |
setInstructionList(il);
|
|
127 |
setConstantPool(cp);
|
|
128 |
|
|
129 |
boolean abstract_ = isAbstract() || isNative();
|
|
130 |
InstructionHandle start = null;
|
|
131 |
InstructionHandle end = null;
|
|
132 |
|
|
133 |
if(!abstract_) {
|
|
134 |
start = il.getStart();
|
|
135 |
end = il.getEnd();
|
|
136 |
|
|
137 |
/* Add local variables, namely the implicit `this' and the arguments
|
|
138 |
*/
|
|
139 |
if(!isStatic() && (class_name != null)) { // Instance method -> `this' is local var 0
|
|
140 |
addLocalVariable("this", new ObjectType(class_name), start, end);
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
if(arg_types != null) {
|
|
145 |
int size = arg_types.length;
|
|
146 |
|
|
147 |
for(int i=0; i < size; i++) {
|
|
148 |
if(Type.VOID == arg_types[i]) {
|
|
149 |
throw new ClassGenException("'void' is an illegal argument type for a method");
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
if(arg_names != null) { // Names for variables provided?
|
|
154 |
if(size != arg_names.length)
|
|
155 |
throw new ClassGenException("Mismatch in argument array lengths: " +
|
|
156 |
size + " vs. " + arg_names.length);
|
|
157 |
} else { // Give them dummy names
|
|
158 |
arg_names = new String[size];
|
|
159 |
|
|
160 |
for(int i=0; i < size; i++)
|
|
161 |
arg_names[i] = "arg" + i;
|
|
162 |
|
|
163 |
setArgumentNames(arg_names);
|
|
164 |
}
|
|
165 |
|
|
166 |
if(!abstract_) {
|
|
167 |
for(int i=0; i < size; i++) {
|
|
168 |
addLocalVariable(arg_names[i], arg_types[i], start, end);
|
|
169 |
}
|
|
170 |
}
|
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Instantiate from existing method.
|
|
176 |
*
|
|
177 |
* @param m method
|
|
178 |
* @param class_name class name containing this method
|
|
179 |
* @param cp constant pool
|
|
180 |
*/
|
|
181 |
public MethodGen(Method m, String class_name, ConstantPoolGen cp) {
|
|
182 |
this(m.getAccessFlags(), Type.getReturnType(m.getSignature()),
|
|
183 |
Type.getArgumentTypes(m.getSignature()), null /* may be overridden anyway */,
|
|
184 |
m.getName(), class_name,
|
|
185 |
((m.getAccessFlags() & (Constants.ACC_ABSTRACT | Constants.ACC_NATIVE)) == 0)?
|
|
186 |
new InstructionList(m.getCode().getCode()) : null,
|
|
187 |
cp);
|
|
188 |
|
|
189 |
Attribute[] attributes = m.getAttributes();
|
|
190 |
for(int i=0; i < attributes.length; i++) {
|
|
191 |
Attribute a = attributes[i];
|
|
192 |
|
|
193 |
if(a instanceof Code) {
|
|
194 |
Code c = (Code)a;
|
|
195 |
setMaxStack(c.getMaxStack());
|
|
196 |
setMaxLocals(c.getMaxLocals());
|
|
197 |
|
|
198 |
CodeException[] ces = c.getExceptionTable();
|
|
199 |
|
|
200 |
if(ces != null) {
|
|
201 |
for(int j=0; j < ces.length; j++) {
|
|
202 |
CodeException ce = ces[j];
|
|
203 |
int type = ce.getCatchType();
|
|
204 |
ObjectType c_type = null;
|
|
205 |
|
|
206 |
if(type > 0) {
|
|
207 |
String cen = m.getConstantPool().getConstantString(type, Constants.CONSTANT_Class);
|
|
208 |
c_type = new ObjectType(cen);
|
|
209 |
}
|
|
210 |
|
|
211 |
int end_pc = ce.getEndPC();
|
|
212 |
int length = m.getCode().getCode().length;
|
|
213 |
|
|
214 |
InstructionHandle end;
|
|
215 |
|
|
216 |
if(length == end_pc) { // May happen, because end_pc is exclusive
|
|
217 |
end = il.getEnd();
|
|
218 |
} else {
|
|
219 |
end = il.findHandle(end_pc);
|
|
220 |
end = end.getPrev(); // Make it inclusive
|
|
221 |
}
|
|
222 |
|
|
223 |
addExceptionHandler(il.findHandle(ce.getStartPC()), end,
|
|
224 |
il.findHandle(ce.getHandlerPC()), c_type);
|
|
225 |
}
|
|
226 |
}
|
|
227 |
|
|
228 |
Attribute[] c_attributes = c.getAttributes();
|
|
229 |
for(int j=0; j < c_attributes.length; j++) {
|
|
230 |
a = c_attributes[j];
|
|
231 |
|
|
232 |
if(a instanceof LineNumberTable) {
|
|
233 |
LineNumber[] ln = ((LineNumberTable)a).getLineNumberTable();
|
|
234 |
|
|
235 |
for(int k=0; k < ln.length; k++) {
|
|
236 |
LineNumber l = ln[k];
|
|
237 |
addLineNumber(il.findHandle(l.getStartPC()), l.getLineNumber());
|
|
238 |
}
|
|
239 |
} else if(a instanceof LocalVariableTable) {
|
|
240 |
LocalVariable[] lv = ((LocalVariableTable)a).getLocalVariableTable();
|
|
241 |
|
|
242 |
removeLocalVariables();
|
|
243 |
|
|
244 |
for(int k=0; k < lv.length; k++) {
|
|
245 |
LocalVariable l = lv[k];
|
|
246 |
InstructionHandle start = il.findHandle(l.getStartPC());
|
|
247 |
InstructionHandle end = il.findHandle(l.getStartPC() + l.getLength());
|
|
248 |
|
|
249 |
// Repair malformed handles
|
|
250 |
if(null == start) {
|
|
251 |
start = il.getStart();
|
|
252 |
}
|
|
253 |
|
|
254 |
if(null == end) {
|
|
255 |
end = il.getEnd();
|
|
256 |
}
|
|
257 |
|
|
258 |
addLocalVariable(l.getName(), Type.getType(l.getSignature()),
|
|
259 |
l.getIndex(), start, end);
|
|
260 |
}
|
|
261 |
} else
|
|
262 |
addCodeAttribute(a);
|
|
263 |
}
|
|
264 |
} else if(a instanceof ExceptionTable) {
|
|
265 |
String[] names = ((ExceptionTable)a).getExceptionNames();
|
|
266 |
for(int j=0; j < names.length; j++)
|
|
267 |
addException(names[j]);
|
|
268 |
} else
|
|
269 |
addAttribute(a);
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
/**
|
|
274 |
* Adds a local variable to this method.
|
|
275 |
*
|
|
276 |
* @param name variable name
|
|
277 |
* @param type variable type
|
|
278 |
* @param slot the index of the local variable, if type is long or double, the next available
|
|
279 |
* index is slot+2
|
|
280 |
* @param start from where the variable is valid
|
|
281 |
* @param end until where the variable is valid
|
|
282 |
* @return new local variable object
|
|
283 |
* @see LocalVariable
|
|
284 |
*/
|
|
285 |
public LocalVariableGen addLocalVariable(String name, Type type, int slot,
|
|
286 |
InstructionHandle start,
|
|
287 |
InstructionHandle end) {
|
|
288 |
byte t = type.getType();
|
|
289 |
|
|
290 |
if(t != Constants.T_ADDRESS) {
|
|
291 |
int add = type.getSize();
|
|
292 |
|
|
293 |
if(slot + add > max_locals)
|
|
294 |
max_locals = slot + add;
|
|
295 |
|
|
296 |
LocalVariableGen l = new LocalVariableGen(slot, name, type, start, end);
|
|
297 |
int i;
|
|
298 |
|
|
299 |
if((i = variable_vec.indexOf(l)) >= 0) // Overwrite if necessary
|
|
300 |
variable_vec.set(i, l);
|
|
301 |
else
|
|
302 |
variable_vec.add(l);
|
|
303 |
|
|
304 |
return l;
|
|
305 |
} else {
|
|
306 |
throw new IllegalArgumentException("Can not use " + type +
|
|
307 |
" as type for local variable");
|
|
308 |
|
|
309 |
}
|
|
310 |
}
|
|
311 |
|
|
312 |
/**
|
|
313 |
* Adds a local variable to this method and assigns an index automatically.
|
|
314 |
*
|
|
315 |
* @param name variable name
|
|
316 |
* @param type variable type
|
|
317 |
* @param start from where the variable is valid, if this is null,
|
|
318 |
* it is valid from the start
|
|
319 |
* @param end until where the variable is valid, if this is null,
|
|
320 |
* it is valid to the end
|
|
321 |
* @return new local variable object
|
|
322 |
* @see LocalVariable
|
|
323 |
*/
|
|
324 |
public LocalVariableGen addLocalVariable(String name, Type type,
|
|
325 |
InstructionHandle start,
|
|
326 |
InstructionHandle end) {
|
|
327 |
return addLocalVariable(name, type, max_locals, start, end);
|
|
328 |
}
|
|
329 |
|
|
330 |
/**
|
|
331 |
* Remove a local variable, its slot will not be reused, if you do not use addLocalVariable
|
|
332 |
* with an explicit index argument.
|
|
333 |
*/
|
|
334 |
public void removeLocalVariable(LocalVariableGen l) {
|
|
335 |
variable_vec.remove(l);
|
|
336 |
}
|
|
337 |
|
|
338 |
/**
|
|
339 |
* Remove all local variables.
|
|
340 |
*/
|
|
341 |
public void removeLocalVariables() {
|
|
342 |
variable_vec.clear();
|
|
343 |
}
|
|
344 |
|
|
345 |
/**
|
|
346 |
* Sort local variables by index
|
|
347 |
*/
|
|
348 |
private static final void sort(LocalVariableGen[] vars, int l, int r) {
|
|
349 |
int i = l, j = r;
|
|
350 |
int m = vars[(l + r) / 2].getIndex();
|
|
351 |
LocalVariableGen h;
|
|
352 |
|
|
353 |
do {
|
|
354 |
while(vars[i].getIndex() < m) i++;
|
|
355 |
while(m < vars[j].getIndex()) j--;
|
|
356 |
|
|
357 |
if(i <= j) {
|
|
358 |
h=vars[i]; vars[i]=vars[j]; vars[j]=h; // Swap elements
|
|
359 |
i++; j--;
|
|
360 |
}
|
|
361 |
} while(i <= j);
|
|
362 |
|
|
363 |
if(l < j) sort(vars, l, j);
|
|
364 |
if(i < r) sort(vars, i, r);
|
|
365 |
}
|
|
366 |
|
|
367 |
/*
|
|
368 |
* If the range of the variable has not been set yet, it will be set to be valid from
|
|
369 |
* the start to the end of the instruction list.
|
|
370 |
*
|
|
371 |
* @return array of declared local variables sorted by index
|
|
372 |
*/
|
|
373 |
public LocalVariableGen[] getLocalVariables() {
|
|
374 |
int size = variable_vec.size();
|
|
375 |
LocalVariableGen[] lg = new LocalVariableGen[size];
|
|
376 |
variable_vec.toArray(lg);
|
|
377 |
|
|
378 |
for(int i=0; i < size; i++) {
|
|
379 |
if(lg[i].getStart() == null)
|
|
380 |
lg[i].setStart(il.getStart());
|
|
381 |
|
|
382 |
if(lg[i].getEnd() == null)
|
|
383 |
lg[i].setEnd(il.getEnd());
|
|
384 |
}
|
|
385 |
|
|
386 |
if(size > 1)
|
|
387 |
sort(lg, 0, size - 1);
|
|
388 |
|
|
389 |
return lg;
|
|
390 |
}
|
|
391 |
|
|
392 |
/**
|
|
393 |
* @return `LocalVariableTable' attribute of all the local variables of this method.
|
|
394 |
*/
|
|
395 |
public LocalVariableTable getLocalVariableTable(ConstantPoolGen cp) {
|
|
396 |
LocalVariableGen[] lg = getLocalVariables();
|
|
397 |
int size = lg.length;
|
|
398 |
LocalVariable[] lv = new LocalVariable[size];
|
|
399 |
|
|
400 |
for(int i=0; i < size; i++)
|
|
401 |
lv[i] = lg[i].getLocalVariable(cp);
|
|
402 |
|
|
403 |
return new LocalVariableTable(cp.addUtf8("LocalVariableTable"),
|
|
404 |
2 + lv.length * 10, lv, cp.getConstantPool());
|
|
405 |
}
|
|
406 |
|
|
407 |
/**
|
|
408 |
* Give an instruction a line number corresponding to the source code line.
|
|
409 |
*
|
|
410 |
* @param ih instruction to tag
|
|
411 |
* @return new line number object
|
|
412 |
* @see LineNumber
|
|
413 |
*/
|
|
414 |
public LineNumberGen addLineNumber(InstructionHandle ih, int src_line) {
|
|
415 |
LineNumberGen l = new LineNumberGen(ih, src_line);
|
|
416 |
line_number_vec.add(l);
|
|
417 |
return l;
|
|
418 |
}
|
|
419 |
|
|
420 |
/**
|
|
421 |
* Remove a line number.
|
|
422 |
*/
|
|
423 |
public void removeLineNumber(LineNumberGen l) {
|
|
424 |
line_number_vec.remove(l);
|
|
425 |
}
|
|
426 |
|
|
427 |
/**
|
|
428 |
* Remove all line numbers.
|
|
429 |
*/
|
|
430 |
public void removeLineNumbers() {
|
|
431 |
line_number_vec.clear();
|
|
432 |
}
|
|
433 |
|
|
434 |
/*
|
|
435 |
* @return array of line numbers
|
|
436 |
*/
|
|
437 |
public LineNumberGen[] getLineNumbers() {
|
|
438 |
LineNumberGen[] lg = new LineNumberGen[line_number_vec.size()];
|
|
439 |
line_number_vec.toArray(lg);
|
|
440 |
return lg;
|
|
441 |
}
|
|
442 |
|
|
443 |
/**
|
|
444 |
* @return `LineNumberTable' attribute of all the local variables of this method.
|
|
445 |
*/
|
|
446 |
public LineNumberTable getLineNumberTable(ConstantPoolGen cp) {
|
|
447 |
int size = line_number_vec.size();
|
|
448 |
LineNumber[] ln = new LineNumber[size];
|
|
449 |
|
|
450 |
try {
|
|
451 |
for(int i=0; i < size; i++)
|
|
452 |
ln[i] = ((LineNumberGen)line_number_vec.get(i)).getLineNumber();
|
|
453 |
} catch(ArrayIndexOutOfBoundsException e) {} // Never occurs
|
|
454 |
|
|
455 |
return new LineNumberTable(cp.addUtf8("LineNumberTable"),
|
|
456 |
2 + ln.length * 4, ln, cp.getConstantPool());
|
|
457 |
}
|
|
458 |
|
|
459 |
/**
|
|
460 |
* Add an exception handler, i.e., specify region where a handler is active and an
|
|
461 |
* instruction where the actual handling is done.
|
|
462 |
*
|
|
463 |
* @param start_pc Start of region (inclusive)
|
|
464 |
* @param end_pc End of region (inclusive)
|
|
465 |
* @param handler_pc Where handling is done
|
|
466 |
* @param catch_type class type of handled exception or null if any
|
|
467 |
* exception is handled
|
|
468 |
* @return new exception handler object
|
|
469 |
*/
|
|
470 |
public CodeExceptionGen addExceptionHandler(InstructionHandle start_pc,
|
|
471 |
InstructionHandle end_pc,
|
|
472 |
InstructionHandle handler_pc,
|
|
473 |
ObjectType catch_type) {
|
|
474 |
if((start_pc == null) || (end_pc == null) || (handler_pc == null))
|
|
475 |
throw new ClassGenException("Exception handler target is null instruction");
|
|
476 |
|
|
477 |
CodeExceptionGen c = new CodeExceptionGen(start_pc, end_pc,
|
|
478 |
handler_pc, catch_type);
|
|
479 |
exception_vec.add(c);
|
|
480 |
return c;
|
|
481 |
}
|
|
482 |
|
|
483 |
/**
|
|
484 |
* Remove an exception handler.
|
|
485 |
*/
|
|
486 |
public void removeExceptionHandler(CodeExceptionGen c) {
|
|
487 |
exception_vec.remove(c);
|
|
488 |
}
|
|
489 |
|
|
490 |
/**
|
|
491 |
* Remove all line numbers.
|
|
492 |
*/
|
|
493 |
public void removeExceptionHandlers() {
|
|
494 |
exception_vec.clear();
|
|
495 |
}
|
|
496 |
|
|
497 |
/*
|
|
498 |
* @return array of declared exception handlers
|
|
499 |
*/
|
|
500 |
public CodeExceptionGen[] getExceptionHandlers() {
|
|
501 |
CodeExceptionGen[] cg = new CodeExceptionGen[exception_vec.size()];
|
|
502 |
exception_vec.toArray(cg);
|
|
503 |
return cg;
|
|
504 |
}
|
|
505 |
|
|
506 |
/**
|
|
507 |
* @return code exceptions for `Code' attribute
|
|
508 |
*/
|
|
509 |
private CodeException[] getCodeExceptions() {
|
|
510 |
int size = exception_vec.size();
|
|
511 |
CodeException[] c_exc = new CodeException[size];
|
|
512 |
|
|
513 |
try {
|
|
514 |
for(int i=0; i < size; i++) {
|
|
515 |
CodeExceptionGen c = (CodeExceptionGen)exception_vec.get(i);
|
|
516 |
c_exc[i] = c.getCodeException(cp);
|
|
517 |
}
|
|
518 |
} catch(ArrayIndexOutOfBoundsException e) {}
|
|
519 |
|
|
520 |
return c_exc;
|
|
521 |
}
|
|
522 |
|
|
523 |
/**
|
|
524 |
* Add an exception possibly thrown by this method.
|
|
525 |
*
|
|
526 |
* @param class_name (fully qualified) name of exception
|
|
527 |
*/
|
|
528 |
public void addException(String class_name) {
|
|
529 |
throws_vec.add(class_name);
|
|
530 |
}
|
|
531 |
|
|
532 |
/**
|
|
533 |
* Remove an exception.
|
|
534 |
*/
|
|
535 |
public void removeException(String c) {
|
|
536 |
throws_vec.remove(c);
|
|
537 |
}
|
|
538 |
|
|
539 |
/**
|
|
540 |
* Remove all exceptions.
|
|
541 |
*/
|
|
542 |
public void removeExceptions() {
|
|
543 |
throws_vec.clear();
|
|
544 |
}
|
|
545 |
|
|
546 |
/*
|
|
547 |
* @return array of thrown exceptions
|
|
548 |
*/
|
|
549 |
public String[] getExceptions() {
|
|
550 |
String[] e = new String[throws_vec.size()];
|
|
551 |
throws_vec.toArray(e);
|
|
552 |
return e;
|
|
553 |
}
|
|
554 |
|
|
555 |
/**
|
|
556 |
* @return `Exceptions' attribute of all the exceptions thrown by this method.
|
|
557 |
*/
|
|
558 |
private ExceptionTable getExceptionTable(ConstantPoolGen cp) {
|
|
559 |
int size = throws_vec.size();
|
|
560 |
int[] ex = new int[size];
|
|
561 |
|
|
562 |
try {
|
|
563 |
for(int i=0; i < size; i++)
|
|
564 |
ex[i] = cp.addClass((String)throws_vec.get(i));
|
|
565 |
} catch(ArrayIndexOutOfBoundsException e) {}
|
|
566 |
|
|
567 |
return new ExceptionTable(cp.addUtf8("Exceptions"),
|
|
568 |
2 + 2 * size, ex, cp.getConstantPool());
|
|
569 |
}
|
|
570 |
|
|
571 |
/**
|
|
572 |
* Add an attribute to the code. Currently, the JVM knows about the
|
|
573 |
* LineNumberTable, LocalVariableTable and StackMap attributes,
|
|
574 |
* where the former two will be generated automatically and the
|
|
575 |
* latter is used for the MIDP only. Other attributes will be
|
|
576 |
* ignored by the JVM but do no harm.
|
|
577 |
*
|
|
578 |
* @param a attribute to be added
|
|
579 |
*/
|
|
580 |
public void addCodeAttribute(Attribute a) { code_attrs_vec.add(a); }
|
|
581 |
|
|
582 |
/**
|
|
583 |
* Remove a code attribute.
|
|
584 |
*/
|
|
585 |
public void removeCodeAttribute(Attribute a) { code_attrs_vec.remove(a); }
|
|
586 |
|
|
587 |
/**
|
|
588 |
* Remove all code attributes.
|
|
589 |
*/
|
|
590 |
public void removeCodeAttributes() {
|
|
591 |
code_attrs_vec.clear();
|
|
592 |
}
|
|
593 |
|
|
594 |
/**
|
|
595 |
* @return all attributes of this method.
|
|
596 |
*/
|
|
597 |
public Attribute[] getCodeAttributes() {
|
|
598 |
Attribute[] attributes = new Attribute[code_attrs_vec.size()];
|
|
599 |
code_attrs_vec.toArray(attributes);
|
|
600 |
return attributes;
|
|
601 |
}
|
|
602 |
|
|
603 |
/**
|
|
604 |
* Get method object. Never forget to call setMaxStack() or setMaxStack(max), respectively,
|
|
605 |
* before calling this method (the same applies for max locals).
|
|
606 |
*
|
|
607 |
* @return method object
|
|
608 |
*/
|
|
609 |
public Method getMethod() {
|
|
610 |
String signature = getSignature();
|
|
611 |
int name_index = cp.addUtf8(name);
|
|
612 |
int signature_index = cp.addUtf8(signature);
|
|
613 |
|
|
614 |
/* Also updates positions of instructions, i.e., their indices
|
|
615 |
*/
|
|
616 |
byte[] byte_code = null;
|
|
617 |
|
|
618 |
if(il != null)
|
|
619 |
byte_code = il.getByteCode();
|
|
620 |
|
|
621 |
LineNumberTable lnt = null;
|
|
622 |
LocalVariableTable lvt = null;
|
|
623 |
|
|
624 |
/* Create LocalVariableTable and LineNumberTable attributes (for debuggers, e.g.)
|
|
625 |
*/
|
|
626 |
if((variable_vec.size() > 0) && !strip_attributes)
|
|
627 |
addCodeAttribute(lvt = getLocalVariableTable(cp));
|
|
628 |
|
|
629 |
if((line_number_vec.size() > 0) && !strip_attributes)
|
|
630 |
addCodeAttribute(lnt = getLineNumberTable(cp));
|
|
631 |
|
|
632 |
Attribute[] code_attrs = getCodeAttributes();
|
|
633 |
|
|
634 |
/* Each attribute causes 6 additional header bytes
|
|
635 |
*/
|
|
636 |
int attrs_len = 0;
|
|
637 |
for(int i=0; i < code_attrs.length; i++)
|
|
638 |
attrs_len += (code_attrs[i].getLength() + 6);
|
|
639 |
|
|
640 |
CodeException[] c_exc = getCodeExceptions();
|
|
641 |
int exc_len = c_exc.length * 8; // Every entry takes 8 bytes
|
|
642 |
|
|
643 |
Code code = null;
|
|
644 |
|
|
645 |
if((il != null) && !isAbstract()) {
|
|
646 |
// Remove any stale code attribute
|
|
647 |
Attribute[] attributes = getAttributes();
|
|
648 |
for(int i=0; i < attributes.length; i++) {
|
|
649 |
Attribute a = attributes[i];
|
|
650 |
|
|
651 |
if(a instanceof Code)
|
|
652 |
removeAttribute(a);
|
|
653 |
}
|
|
654 |
|
|
655 |
code = new Code(cp.addUtf8("Code"),
|
|
656 |
8 + byte_code.length + // prologue byte code
|
|
657 |
2 + exc_len + // exceptions
|
|
658 |
2 + attrs_len, // attributes
|
|
659 |
max_stack, max_locals,
|
|
660 |
byte_code, c_exc,
|
|
661 |
code_attrs,
|
|
662 |
cp.getConstantPool());
|
|
663 |
|
|
664 |
addAttribute(code);
|
|
665 |
}
|
|
666 |
|
|
667 |
ExceptionTable et = null;
|
|
668 |
|
|
669 |
if(throws_vec.size() > 0)
|
|
670 |
addAttribute(et = getExceptionTable(cp)); // Add `Exceptions' if there are "throws" clauses
|
|
671 |
|
|
672 |
Method m = new Method(access_flags, name_index, signature_index,
|
|
673 |
getAttributes(), cp.getConstantPool());
|
|
674 |
|
|
675 |
// Undo effects of adding attributes
|
|
676 |
if(lvt != null) removeCodeAttribute(lvt);
|
|
677 |
if(lnt != null) removeCodeAttribute(lnt);
|
|
678 |
if(code != null) removeAttribute(code);
|
|
679 |
if(et != null) removeAttribute(et);
|
|
680 |
|
|
681 |
return m;
|
|
682 |
}
|
|
683 |
|
|
684 |
/**
|
|
685 |
* Remove all NOPs from the instruction list (if possible) and update every
|
|
686 |
* object refering to them, i.e., branch instructions, local variables and
|
|
687 |
* exception handlers.
|
|
688 |
*/
|
|
689 |
public void removeNOPs() {
|
|
690 |
if(il != null) {
|
|
691 |
InstructionHandle next;
|
|
692 |
/* Check branch instructions.
|
|
693 |
*/
|
|
694 |
for(InstructionHandle ih = il.getStart(); ih != null; ih = next) {
|
|
695 |
next = ih.next;
|
|
696 |
|
|
697 |
if((next != null) && (ih.getInstruction() instanceof NOP)) {
|
|
698 |
try {
|
|
699 |
il.delete(ih);
|
|
700 |
} catch(TargetLostException e) {
|
|
701 |
InstructionHandle[] targets = e.getTargets();
|
|
702 |
|
|
703 |
for(int i=0; i < targets.length; i++) {
|
|
704 |
InstructionTargeter[] targeters = targets[i].getTargeters();
|
|
705 |
|
|
706 |
for(int j=0; j < targeters.length; j++)
|
|
707 |
targeters[j].updateTarget(targets[i], next);
|
|
708 |
}
|
|
709 |
}
|
|
710 |
}
|
|
711 |
}
|
|
712 |
}
|
|
713 |
}
|
|
714 |
|
|
715 |
/**
|
|
716 |
* Set maximum number of local variables.
|
|
717 |
*/
|
|
718 |
public void setMaxLocals(int m) { max_locals = m; }
|
|
719 |
public int getMaxLocals() { return max_locals; }
|
|
720 |
|
|
721 |
/**
|
|
722 |
* Set maximum stack size for this method.
|
|
723 |
*/
|
|
724 |
public void setMaxStack(int m) { max_stack = m; }
|
|
725 |
public int getMaxStack() { return max_stack; }
|
|
726 |
|
|
727 |
/** @return class that contains this method
|
|
728 |
*/
|
|
729 |
public String getClassName() { return class_name; }
|
|
730 |
public void setClassName(String class_name) { this.class_name = class_name; }
|
|
731 |
|
|
732 |
public void setReturnType(Type return_type) { setType(return_type); }
|
|
733 |
public Type getReturnType() { return getType(); }
|
|
734 |
|
|
735 |
public void setArgumentTypes(Type[] arg_types) { this.arg_types = arg_types; }
|
|
736 |
public Type[] getArgumentTypes() { return (Type[])arg_types.clone(); }
|
|
737 |
public void setArgumentType(int i, Type type) { arg_types[i] = type; }
|
|
738 |
public Type getArgumentType(int i) { return arg_types[i]; }
|
|
739 |
|
|
740 |
public void setArgumentNames(String[] arg_names) { this.arg_names = arg_names; }
|
|
741 |
public String[] getArgumentNames() { return (String[])arg_names.clone(); }
|
|
742 |
public void setArgumentName(int i, String name) { arg_names[i] = name; }
|
|
743 |
public String getArgumentName(int i) { return arg_names[i]; }
|
|
744 |
|
|
745 |
public InstructionList getInstructionList() { return il; }
|
|
746 |
public void setInstructionList(InstructionList il) { this.il = il; }
|
|
747 |
|
|
748 |
public String getSignature() {
|
|
749 |
return Type.getMethodSignature(type, arg_types);
|
|
750 |
}
|
|
751 |
|
|
752 |
/**
|
|
753 |
* Computes max. stack size by performing control flow analysis.
|
|
754 |
*/
|
|
755 |
public void setMaxStack() {
|
|
756 |
if(il != null)
|
|
757 |
max_stack = getMaxStack(cp, il, getExceptionHandlers());
|
|
758 |
else
|
|
759 |
max_stack = 0;
|
|
760 |
}
|
|
761 |
|
|
762 |
/**
|
|
763 |
* Compute maximum number of local variables.
|
|
764 |
*/
|
|
765 |
public void setMaxLocals() {
|
|
766 |
if(il != null) {
|
|
767 |
int max = isStatic()? 0 : 1;
|
|
768 |
|
|
769 |
if(arg_types != null)
|
|
770 |
for(int i=0; i < arg_types.length; i++)
|
|
771 |
max += arg_types[i].getSize();
|
|
772 |
|
|
773 |
for(InstructionHandle ih = il.getStart(); ih != null; ih = ih.getNext()) {
|
|
774 |
Instruction ins = ih.getInstruction();
|
|
775 |
|
|
776 |
if((ins instanceof LocalVariableInstruction) ||
|
|
777 |
(ins instanceof RET) || (ins instanceof IINC))
|
|
778 |
{
|
|
779 |
int index = ((IndexedInstruction)ins).getIndex() +
|
|
780 |
((TypedInstruction)ins).getType(cp).getSize();
|
|
781 |
|
|
782 |
if(index > max)
|
|
783 |
max = index;
|
|
784 |
}
|
|
785 |
}
|
|
786 |
|
|
787 |
max_locals = max;
|
|
788 |
} else
|
|
789 |
max_locals = 0;
|
|
790 |
}
|
|
791 |
|
|
792 |
/** Do not/Do produce attributes code attributesLineNumberTable and
|
|
793 |
* LocalVariableTable, like javac -O
|
|
794 |
*/
|
|
795 |
public void stripAttributes(boolean flag) { strip_attributes = flag; }
|
|
796 |
|
|
797 |
static final class BranchTarget {
|
|
798 |
InstructionHandle target;
|
|
799 |
int stackDepth;
|
|
800 |
|
|
801 |
BranchTarget(InstructionHandle target, int stackDepth) {
|
|
802 |
this.target = target;
|
|
803 |
this.stackDepth = stackDepth;
|
|
804 |
}
|
|
805 |
}
|
|
806 |
|
|
807 |
static final class BranchStack {
|
|
808 |
Stack branchTargets = new Stack();
|
|
809 |
Hashtable visitedTargets = new Hashtable();
|
|
810 |
|
|
811 |
public void push(InstructionHandle target, int stackDepth) {
|
|
812 |
if(visited(target))
|
|
813 |
return;
|
|
814 |
|
|
815 |
branchTargets.push(visit(target, stackDepth));
|
|
816 |
}
|
|
817 |
|
|
818 |
public BranchTarget pop() {
|
|
819 |
if(!branchTargets.empty()) {
|
|
820 |
BranchTarget bt = (BranchTarget) branchTargets.pop();
|
|
821 |
return bt;
|
|
822 |
}
|
|
823 |
|
|
824 |
return null;
|
|
825 |
}
|
|
826 |
|
|
827 |
private final BranchTarget visit(InstructionHandle target, int stackDepth) {
|
|
828 |
BranchTarget bt = new BranchTarget(target, stackDepth);
|
|
829 |
visitedTargets.put(target, bt);
|
|
830 |
|
|
831 |
return bt;
|
|
832 |
}
|
|
833 |
|
|
834 |
private final boolean visited(InstructionHandle target) {
|
|
835 |
return (visitedTargets.get(target) != null);
|
|
836 |
}
|
|
837 |
}
|
|
838 |
|
|
839 |
/**
|
|
840 |
* Computes stack usage of an instruction list by performing control flow analysis.
|
|
841 |
*
|
|
842 |
* @return maximum stack depth used by method
|
|
843 |
*/
|
|
844 |
public static int getMaxStack(ConstantPoolGen cp, InstructionList il, CodeExceptionGen[] et) {
|
|
845 |
BranchStack branchTargets = new BranchStack();
|
|
846 |
|
|
847 |
/* Initially, populate the branch stack with the exception
|
|
848 |
* handlers, because these aren't (necessarily) branched to
|
|
849 |
* explicitly. in each case, the stack will have depth 1,
|
|
850 |
* containing the exception object.
|
|
851 |
*/
|
|
852 |
for (int i = 0; i < et.length; i++) {
|
|
853 |
InstructionHandle handler_pc = et[i].getHandlerPC();
|
|
854 |
if (handler_pc != null)
|
|
855 |
branchTargets.push(handler_pc, 1);
|
|
856 |
}
|
|
857 |
|
|
858 |
int stackDepth = 0, maxStackDepth = 0;
|
|
859 |
InstructionHandle ih = il.getStart();
|
|
860 |
|
|
861 |
while(ih != null) {
|
|
862 |
Instruction instruction = ih.getInstruction();
|
|
863 |
short opcode = instruction.getOpcode();
|
|
864 |
int delta = instruction.produceStack(cp) - instruction.consumeStack(cp);
|
|
865 |
|
|
866 |
stackDepth += delta;
|
|
867 |
if(stackDepth > maxStackDepth)
|
|
868 |
maxStackDepth = stackDepth;
|
|
869 |
|
|
870 |
// choose the next instruction based on whether current is a branch.
|
|
871 |
if(instruction instanceof BranchInstruction) {
|
|
872 |
BranchInstruction branch = (BranchInstruction) instruction;
|
|
873 |
if(instruction instanceof Select) {
|
|
874 |
// explore all of the select's targets. the default target is handled below.
|
|
875 |
Select select = (Select) branch;
|
|
876 |
InstructionHandle[] targets = select.getTargets();
|
|
877 |
for (int i = 0; i < targets.length; i++)
|
|
878 |
branchTargets.push(targets[i], stackDepth);
|
|
879 |
// nothing to fall through to.
|
|
880 |
ih = null;
|
|
881 |
} else if(!(branch instanceof IfInstruction)) {
|
|
882 |
// if an instruction that comes back to following PC,
|
|
883 |
// push next instruction, with stack depth reduced by 1.
|
|
884 |
if(opcode == Constants.JSR || opcode == Constants.JSR_W)
|
|
885 |
branchTargets.push(ih.getNext(), stackDepth - 1);
|
|
886 |
ih = null;
|
|
887 |
}
|
|
888 |
// for all branches, the target of the branch is pushed on the branch stack.
|
|
889 |
// conditional branches have a fall through case, selects don't, and
|
|
890 |
// jsr/jsr_w return to the next instruction.
|
|
891 |
branchTargets.push(branch.getTarget(), stackDepth);
|
|
892 |
} else {
|
|
893 |
// check for instructions that terminate the method.
|
|
894 |
if(opcode == Constants.ATHROW || opcode == Constants.RET ||
|
|
895 |
(opcode >= Constants.IRETURN && opcode <= Constants.RETURN))
|
|
896 |
ih = null;
|
|
897 |
}
|
|
898 |
// normal case, go to the next instruction.
|
|
899 |
if(ih != null)
|
|
900 |
ih = ih.getNext();
|
|
901 |
// if we have no more instructions, see if there are any deferred branches to explore.
|
|
902 |
if(ih == null) {
|
|
903 |
BranchTarget bt = branchTargets.pop();
|
|
904 |
if (bt != null) {
|
|
905 |
ih = bt.target;
|
|
906 |
stackDepth = bt.stackDepth;
|
|
907 |
}
|
|
908 |
}
|
|
909 |
}
|
|
910 |
|
|
911 |
return maxStackDepth;
|
|
912 |
}
|
|
913 |
|
|
914 |
private ArrayList observers;
|
|
915 |
|
|
916 |
/** Add observer for this object.
|
|
917 |
*/
|
|
918 |
public void addObserver(MethodObserver o) {
|
|
919 |
if(observers == null)
|
|
920 |
observers = new ArrayList();
|
|
921 |
|
|
922 |
observers.add(o);
|
|
923 |
}
|
|
924 |
|
|
925 |
/** Remove observer for this object.
|
|
926 |
*/
|
|
927 |
public void removeObserver(MethodObserver o) {
|
|
928 |
if(observers != null)
|
|
929 |
observers.remove(o);
|
|
930 |
}
|
|
931 |
|
|
932 |
/** Call notify() method on all observers. This method is not called
|
|
933 |
* automatically whenever the state has changed, but has to be
|
|
934 |
* called by the user after he has finished editing the object.
|
|
935 |
*/
|
|
936 |
public void update() {
|
|
937 |
if(observers != null)
|
|
938 |
for(Iterator e = observers.iterator(); e.hasNext(); )
|
|
939 |
((MethodObserver)e.next()).notify(this);
|
|
940 |
}
|
|
941 |
|
|
942 |
/**
|
|
943 |
* Return string representation close to declaration format,
|
|
944 |
* `public static void _main(String[]) throws IOException', e.g.
|
|
945 |
*
|
|
946 |
* @return String representation of the method.
|
|
947 |
*/
|
|
948 |
public final String toString() {
|
|
949 |
String access = Utility.accessToString(access_flags);
|
|
950 |
String signature = Type.getMethodSignature(type, arg_types);
|
|
951 |
|
|
952 |
signature = Utility.methodSignatureToString(signature, name, access,
|
|
953 |
true, getLocalVariableTable(cp));
|
|
954 |
|
|
955 |
StringBuffer buf = new StringBuffer(signature);
|
|
956 |
|
|
957 |
if(throws_vec.size() > 0) {
|
|
958 |
for(Iterator e = throws_vec.iterator(); e.hasNext(); )
|
|
959 |
buf.append("\n\t\tthrows " + e.next());
|
|
960 |
}
|
|
961 |
|
|
962 |
return buf.toString();
|
|
963 |
}
|
|
964 |
|
|
965 |
/** @return deep copy of this method
|
|
966 |
*/
|
|
967 |
public MethodGen copy(String class_name, ConstantPoolGen cp) {
|
|
968 |
Method m = ((MethodGen)clone()).getMethod();
|
|
969 |
MethodGen mg = new MethodGen(m, class_name, this.cp);
|
|
970 |
|
|
971 |
if(this.cp != cp) {
|
|
972 |
mg.setConstantPool(cp);
|
|
973 |
mg.getInstructionList().replaceConstantPool(this.cp, cp);
|
|
974 |
}
|
|
975 |
|
|
976 |
return mg;
|
|
977 |
}
|
|
978 |
}
|