[jboss-user] [Javassist] New message: "Re: determining if code passed to insertAfter() is executed after an exception handler or before normal return"

Arvind K do-not-reply at jboss.com
Tue Feb 16 23:08:45 EST 2010


User development,

A new message was posted in the thread "determining if code passed to insertAfter() is executed after an exception handler or before normal return":

http://community.jboss.org/message/526689#526689

Author  : Arvind K
Profile : http://community.jboss.org/people/megalodon

Message:
--------------------------------------------------------------
I didnt know much about CtMethod.instrument() and ExprEditor and its edit methods before. You can use them to solve the above problem:

// Suppose you want to know from where the insertBefore code of 'myCtMethod' is running, You can add a boolean 
// variable to the class for this purpose (unfortunately, we need one for every such method). In our case, I call 
// it 'inHandler'. Set it to false upon initialization.
  
   myCtMethod.instrument(new ExprEditor() {
   public void edit(Handler myHandler) throws CannotCompileException {
    try {
   
     // insertBefore inserts the statement before the catch block is entered. You can even change the exception here, i think.
     // Here I set inHandler to true signifying entry into the catch block.
     myHandler.insertBefore("inHandler = true;");  // inHandler is an (added) field of the CtClass containing myCtMethod
     
     // Below statement prints some description of the handler object, if needed.
     myHandler.insertBefore("System.out.println(\"Instrumentation detected presence of a Handler for " + myHandler.getType().getName() + " at line number " + myHandler.getLineNumber()+"\");");
    } catch (NotFoundException ntfnde) {
     ntfnde.printStackTrace();
    }
   }
  });
 
 
// The below if statement will distinguish between normal code and exception handler code and execute accordingly
  sayCtM.insertAfter("if (inHandler == true) System.out.println(\"Running within a handler.\"); else System.out.println(\"Running in normal code\");");
 


--------------------------------------------------------------

To reply to this message visit the message page: http://community.jboss.org/message/526689#526689




More information about the jboss-user mailing list