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 "Ganesan, Dharmalingam" <Dh...@iese.fraunhofer.de> on 2004/12/26 12:35:00 UTC

BCEL: Method Invocation Redirection

Hi,

> 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.
> 
> 
> /* Original code */
> public class MyClass {
> 
>     public void start() {
> 
>     }
> 
>     public void kick() {
> 	start();
>     }
> }
> 
> 
> /* New code after redirecting start() method to start(Thread)  should
> look as below */
> 
> public class MyClass {
> 
>     public void start() {
> 
>     }
> 
>     public void start(Thread callerThread) {
>           start();
>     }
> 
>     public void kick() {
>            start(Thread.currentThread());
>     }
> }
> 
> My question is how to do redirection with the kick() method. That is,
> the kick() should call start(Thread), instead of start().
> 
> I learned how to introduce a new method into java class. Therefore, It
> is enough if you can explain me about the redirection.
> 
> Thank you in advance.
> 
> Best Regards,
> Dharma
> 

Re: BCEL: Method Invocation Redirection

Posted by Kees Jan Koster <kj...@kjkoster.org>.
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