Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] After Advice not called if using Reflection API

Dean,

Yes, you are right. it works!

I love this forum. :)

Thanks you for the explanation between "call" vs "execute" !

-herve


On Dec 14, 2007 11:10 AM, Dean Wampler < dean@xxxxxxxxxxxxxxxxxxxxx> wrote:
Doing a quick read of your code, I think the problem is that you're using "call(...)" in the pointcut definition, which won't work with the reflection API. The "call" means aspectj will try to advise the code that is calling the method, but that's happening inside Java's reflection API, which aspectj won't touch. Even if aspectj could advise JDK code, it still wouldn't know to advise the reflection API in this situation, because the method is referred to by a string name, rather than by the actual class and method itself.

Instead, I think if you use "execute(...)" in your pointcut, it will work. In this case, aspectj will advise your code - the method whose exception you are trying to catch.

I hope this works!

dean 


On Dec 14, 2007, at 1:02 PM, Herve Attia wrote:

Hello,

First of all, I'm a newbie with AOP. I'm learning aspectJ framework

I'm testing the "exception introduction pattern" example ( ch11.4.2) from the "AspectJ in action" book.

The sample works great until you invoke the caller object using reflection API. The PreserveCheckedException advice code (described below ) is NOT invoked anymore.

Once the the ConcernAspect around advice throws the ConcernRuntimeException, this exception is directly wrapped within an java.lang.reflect.InvocationTargetException .

The issue is not related to the sample but to some side effect between reflection framework exception handling and AspectJ weaving.

How can we make the PreserveBusinessException advice code invoked in this special case ?

I attached the source code.

Thanks for your advice! ;)

-herve




///////////////////  test program ////////////////////////////////

import java.lang.reflect.Method;


public class TestException {
    public static void main(String[] args) {
    BusinessClass bc = new BusinessClass();
   
/*   
     //works fine!. PreserveBusinessException Advice code is invoked
     bc.businessMethod1();
    try {
        bc.businessMethod2();
    } catch (Exception ex) {
        // Do something...
        // Log it, execute recovery mechanism, etc.
        System.out.println ("Caught:" + ex);
    }
*/   

    try {

        //PreserveBusinessException Advice code is not invoked
       
        Class cl=Class.forName ("BusinessClass");
        Method method = cl.getMethod("businessMethod2", null);
        method.invoke(bc, null);

       
       
    } catch (Exception ex) {
        // Do something...
        // Log it, execute recovery mechanism, etc.
        System.out.println("Caught:" + ex);
    }
  }
}

////////////////////////  abstract aspect class ///////////////////////

public abstract aspect ConcernAspect {
    abstract pointcut operations();

    Object around() : operations() {
    try {
        return proceed();
    }
     catch (Throwable ex) {
        // do something
        throw new ConcernRuntimeException(ex);
    }
 }
}


////////////////////////  PreserveBusinessException : aspect which captures all the methods throwing a ConcernRuntimeException and throws the cause instead ///////////////////////

public aspect PreserveBusinessException {
    declare precedence: PreserveBusinessException, ConcernAspect+;

    after() throwing(ConcernRuntimeException ex) throws BusinessException: call(* *.*(..) throws BusinessException) {
    Throwable cause = ex.getCause();
    System.out.println("PreserveBusinessException advice called:" + ex);

    if (cause instanceof BusinessException) {
        throw (BusinessException)cause;
    }
    throw ex;
    }
}


////////////////////////  business concrete aspect implementation class ///////////////////////

public aspect BusinessConcernAspect extends ConcernAspect {
    pointcut operations() : execution(* BusinessClass.business*());
}


////////////////////////  businessClass ///////////////////////

public class BusinessClass {
    public void businessMethod2() throws BusinessException, IllegalStateException{
    // business logic...
    throw new BusinessException();
    }
}


////////////////////////  BusinessException class ///////////////////////

public class BusinessException extends Exception {
}

////////////////////////  ConcernRuntimeException class ///////////////////////

public class ConcernRuntimeException extends RuntimeException {
    public ConcernRuntimeException(Throwable cause) {
    super(cause);
    }
}

  
  

<testException.rar>_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Dean Wampler, Ph.D.
See also:
http://aquarium.rubyforge.org     AOP for Ruby
http://www.contract4j.org          Design by Contract for Java5




_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users




--

www.herveattia.com
Cell (650) 861 0007

Back to the top