adding try finnaly to the whole method
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <[email protected]> |
Hi!
I'm trying to add try finally to every method. Here is the code:
class MyAdapter extends MethodAdapter {
public AddTimerMethodAdapter(MethodVisitor mv) {
super(mv);
}
@Override public void visitCode() {
mv.visitCode();
startTryCatchBlockLabel = new Label();
endTryCatchBlockLabel = new Label();
mv.visitLabel(startTryCatchBlockLabel);
mv.visitTryCatchBlock(startTryCatchBlockLabel,
endTryCatchBlockLabel, endTryCatchBlockLabel, null);
String MID = "Method " +methodName+" with signature
"+methodDesc+" is executing";
mv.visitLdcInsn(MID);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "jp/Profiler",
"printMID", "(Ljava/lang/String;)V");
}
//adding the method after target method
@Override public void visitInsn(int opcode) {
if ((opcode >= Opcodes.IRETURN && opcode <=
Opcodes.RETURN) ){
normalExecutionFinallyBlockLabel = new Label();
mv.visitJumpInsn(Opcodes.GOTO,
normalExecutionFinallyBlockLabel);
mv.visitLabel(endTryCatchBlockLabel);
Label exceptionExecutionFinallyBlockLabel = new
Label();
mv.visitLabel(exceptionExecutionFinallyBlockLabel);
String MID = "Method " +methodName+" with
signature "+methodDesc+" is exiting";
mv.visitLdcInsn(MID);
mv.visitMethodInsn(Opcodes.INVOKESTATIC,
"jp/Profiler", "printMID", "(Ljava/lang/String;)V");
Label l4 = new Label();
mv.visitLabel(l4);
mv.visitInsn(Opcodes.ATHROW);
mv.visitLabel(normalExecutionFinallyBlockLabel);
mv.visitLdcInsn(MID);
mv.visitMethodInsn(Opcodes.INVOKESTATIC,
"jp/Profiler", "printMID", "(Ljava/lang/String;)V");
}
mv.visitInsn(opcode);
mv.visitMaxs(2, 3);
}
@Override public void visitMaxs(int maxStack, int maxLocals) {
mv.visitMaxs(maxStack + 4, maxLocals+1);
}
}
It seems to me that everything is right, I've doublechecked with javap and
ASMifier but it fails with DaCapo benchmark(Illegal exception table range) and
it is strange, cause it is non deterministic, yesterday it failed and now it is
working! Aslo I wanted to make the same with tree API, here is the code:
public static void AddMethods(ClassNode clazz){
MethodNode m_clinit = null;
List<MethodNode> methods = clazz.methods;
// for (int m=0; m<methods.size();m++) {
// if
(methods.get(m).name.equals("<init>")||methods.get(m).name.equals("<clinit>")){
// m_clinit= methods.get(m);
// break;
// }
// }
for (int i=0; i < methods.size();i++) {
MethodNode method = methods.get(i);
// if(method == m_clinit)
// continue;
// else{
String MethodMID =
method.name+method.desc;
InsnList instructions =
method.instructions;
List<TryCatchBlockNode> tryCatchBlocks=
method.tryCatchBlocks;
InsnList newInstructionList = new
InsnList();
LabelNode startTryCatchBlockLabel = new
LabelNode();
LabelNode endTryCatchBlockLabel = new
LabelNode();
newInstructionList.add(startTryCatchBlockLabel);
newInstructionList.add(endTryCatchBlockLabel);
tryCatchBlocks.add(new
TryCatchBlockNode(startTryCatchBlockLabel,endTryCatchBlockLabel,endTryCatchBlockLabel,null));
//add instructionList to the existing one
instructions.insert(newInstructionList);
Iterator j = instructions.iterator();
while(j.hasNext()){
AbstractInsnNode instruction =
(AbstractInsnNode) j.next() ;
int opcode =
instruction.getOpcode();
if(opcode>=Opcodes.IRETURN &&
opcode<= Opcodes.RETURN )
{
// insert smth before the
method return
String byeString =
"Method "+method.name+method.desc+" is exiting";
newInstructionList.clear();
LabelNode
normalExecutionFinallyBlockLabel = new LabelNode();
newInstructionList.add(normalExecutionFinallyBlockLabel);
JumpInsnNode
jumpInstruction = new
JumpInsnNode(Opcodes.GOTO,normalExecutionFinallyBlockLabel);
newInstructionList.add(jumpInstruction);
newInstructionList.add(endTryCatchBlockLabel);
LabelNode
exceptionExecutionFinallyBlockLabel = new LabelNode();
newInstructionList.add(exceptionExecutionFinallyBlockLabel);
newInstructionList.add(new LdcInsnNode(byeString));
newInstructionList.add(new
MethodInsnNode(Opcodes.INVOKESTATIC, "test/MID", "printMID",
"(Ljava/lang/String;)V"));
LabelNode l4 = new
LabelNode();
newInstructionList.add(l4);
newInstructionList.add(new InsnNode(Opcodes.ATHROW));
newInstructionList.add(normalExecutionFinallyBlockLabel);
newInstructionList.add(new LdcInsnNode(byeString));
newInstructionList.add(new
MethodInsnNode(Opcodes.INVOKESTATIC, "test/MID", "printMID",
"(Ljava/lang/String;)V"));
//add instructionList to the
existing one
instructions.insert(instruction.getPrevious(),newInstructionList);
}
}
}
}
But this code is not working, it doesn't insert labels into the code, so that
Exception table looks like this:
from to target type
0 0 0 any
and there is also no GOTO jump instruction in the instrumented bytecode.
So, my questions are following is my first code correct(core API) if so why it
fails with dacapo, and what is wrong in the code using the Tree API?
Thanks in advance! I appreciate any help!
Aibek
message-footer.txt
(text/plain, 238 B)
-- You receive this message as a subscriber of the [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help OW2 mailing lists service home page: http://www.ow2.org/wws