Re: Re: Re: Re: add code to existing method code

"luc peuvrier at home" <[email protected]>
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <E9D5B77C47AC440FBCD5FA5A4D48425A@peuvrierluc>
----- Original Message ----- 
From: "Eugene Kuleshov" <[email protected]>
To: <[email protected]>
Sent: Sunday, September 26, 2010 3:00 PM
Subject: [asm] Re: Re: Re: add code to existing method code


> Rémi Forax wrote:
>>>  Your assumption is correct. The order of visit calls is documented
>>> at
>>> http://asm.ow2.org/asm33/javadoc/user/org/objectweb/asm/MethodVisitor.html
>>>
>> And if you want to put additional code, the best is to trap all return
>> instructions
>> in visitInsn.
>>
>> public void visitInsn(int opcode) {
>>   // trick all return opcodes are between IRETURN and RETURN
>>   if (opcode >= IRETURN && opcode <= RETURN) {
>>      // don't forget that the return value is already on top of the stack
>>      ...
>>   }
>>   super.visitInsn(opcode);
>> }
>  Good point Remi. We there is also an AdviceAdapter that takes care of
> that:
>
> http://asm.ow2.org/asm33/javadoc/user/org/objectweb/asm/commons/AdviceAdapter.html
>
>  regards,
>  Eugene
>

Thank you for the reply.

My goal is to notify exit from method in all the cases
I already added call to notify before all returns instruction as you 
describe it.
But I also want to notify in case of exception, so I enhance the code adding 
a try { } finaly{ notification } like this:

 @Override
 public void visitCode() {
  methodVisitor.visitCode();
  if (addEnable && !INIT_METHOD_NAME.equals(methodName)) {
   /*
    * add call to intercept$JOAFIP$ to begin of method code (not in
    * constructor)
    */
   final Label label0 = new Label();
   label1 = new Label();
   methodVisitor.visitTryCatchBlock(label0, label1, label1, null);
   methodVisitor.visitLabel(label0);
   methodVisitor.visitVarInsn(ALOAD, 0);
   methodVisitor.visitMethodInsn(
   /**/INVOKESTATIC, STATIC_PROXY_CALL_BACK,
   /**/"intercept$JOAFIP$", LJAVA_LANG_OBJECT_V_DESC);
  }
 }

and

 @Override
 public void visitMaxs(final int maxStack, final int maxLocals) {
  if (addEnable && !INIT_METHOD_NAME.equals(methodName)) {
   methodVisitor.visitLabel(label1);
   methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1,
     new Object[] { "java/lang/Throwable" });
   methodVisitor.visitVarInsn(ASTORE, maxLocals - 1);
   // this
   methodVisitor.visitVarInsn(ALOAD, 0);
   methodVisitor.visitMethodInsn(
   /**/INVOKESTATIC, STATIC_PROXY_CALL_BACK,
   /**/"methodEnd$JOAFIP$", LJAVA_LANG_OBJECT_V_DESC);
   methodVisitor.visitVarInsn(ALOAD, maxLocals - 1);
   methodVisitor.visitInsn(ATHROW);
  }
 .......
 }

Best regards
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.