You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by Kees Jan Koster <kj...@kjkoster.org> on 2005/01/02 20:40:44 UTC

Re: BCEL: Method Invocation Redirection

Dear Dharma,

>
>> I'm new to BCEL. I started using BCEL to do run-time analysis. I have
>> question: How to redirect a method invocation to a different method.
>
I do not think that doing this is best done using BCEL directly. For this  
particular case I would use AspectJ and not BCEL.

AspectJ allows you to wrap method calls into others using so-called  
aspects. It gives an easier syntax to work with than BCEL does. I have  
attached a bit of AspectJ source code that shows how to log all calls to  
notify() and friends. Of course, this code is easily modified to do the  
wrapping that you intend to do.

As an interesting aside, AspectJ uses BCEL to do the actual work. :-)

Kees Jan

---- TraceWaitNotify.java ---
aspect TraceWaitNotify {
   pointcut notify(): call (* notify());
   pointcut notifyAll(): call (* notifyAll());
   pointcut wait(): call (* wait());

   before (): notify() {
     System.err.println(format(thisJoinPoint.getThis()) + " notifies one on  
"
                      + format(thisJoinPoint.getTarget())
                      + " at " + thisJoinPoint.getSourceLocation());
   }

   before (): notifyAll() {
     System.err.println(format(thisJoinPoint.getThis()) + " notifies all on  
"
                      + format(thisJoinPoint.getTarget())
                      + " at " + thisJoinPoint.getSourceLocation());
   }

   before (): wait() {
     System.err.println(format(thisJoinPoint.getThis()) + " falls asleep on  
"
                      + format(thisJoinPoint.getTarget())
                      + " at " + thisJoinPoint.getSourceLocation());
   }

   after (): wait() {
     System.err.println(format(thisJoinPoint.getThis()) + " wakes up from "
                      + format(thisJoinPoint.getTarget())
                      + " at " + thisJoinPoint.getSourceLocation());
   }

   private String format(final Object object) {
     if (object == null) {
       return "<null object>";
     }

     if (object instanceof Channel) {
       return "channel[" + ((Channel)object).getNumber() + "]";
     }

     return object.getClass().getName() + "@" + object.hashCode();
   }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org