0

i have a test method:

public List<User> getUsers(){
List list = new ArrayList();
return list;
}

so,I want insert two for-loop code to the method, to new list before and after, asm code same is :

final int returnValueStackNum = adapter.newLocal(Type.INT_TYPE);
mv.visitInsn(Opcodes.ICONST_0);
mv.visitVarInsn(Opcodes.ISTORE, returnValueStackNum);
final Label l0 = new Label();
mv.visitJumpInsn(Opcodes.GOTO, l0);
final Label l1 = new Label();
mv.visitLabel(l1);
mv.visitIincInsn(returnValueStackNum, 1);
mv.visitLabel(l0);
mv.visitVarInsn(Opcodes.ILOAD, returnValueStackNum);
mv.visitIntInsn(Opcodes.BIPUSH, 10);
mv.visitJumpInsn(Opcodes.IF_ICMPLT, l1);

first for-loop is right in new class file, but second for-loop is wrong and return is lost, code same:

List list = new ArrayList();
for (int i = 0; i < 10; i++);
int j = 0;
tmpTernaryOp = localArrayList;
while (j < 10)
   j++;

Where has the problem, thank you very much

4

1 回答 1

0

In the topic you mention onmethodexit which leads me to guess you are using AdviceAdapter to add code to the end of methods. This is a good. I assume the code you showed was copypasted from the onMethodExit() method.

However in your code you seem to be using both adapter and mv variables - what does the "adapter" variable contain? My guess is you should not need the adapter variable at all - instead call newLocal() directly in the onMethodExit() method.

If you are using some separate adapter which is not chained properly it will probably "allocate" variables that are already in use.

So without more code to understand the whole picture, my suggestion is: try changing adapter.newLocal(Type.INT_TYPE) to just newLocal(Type.INT_TYPE).

于 2013-02-07T20:51:25.110 回答