Re: BCEL: Method Invocation Redirection

"Kees Jan Koster" <[email protected]>
Newsgroups gmane.comp.jakarta.bcel.user
Message-ID <opsjzp9603h1gx1t@likeever>
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();
   }
}
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.