herve (2) [Avatar] Offline
#1
Hello,

First of all, I'm a newbie with AOP. I'm learning aspectJ framework with the help of this great book.

Not sure if the below problen is a known issue with AspectJ.

The sample ch11.4.2 which demonstrates exception introduction pattern DOES not work if we use reflection API . The PreserveCheckedException advice code is NEVER invoked.

Is there a work around to solve this problem?

Thanks!


public class TestException {
public static void main(String[] args) {
BusinessClass bc = new BusinessClass();

/*

//works fine. PreserveCheckedException advice is invoked.

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

// PreserveCheckedException advice is never invoked.
// exception is wrapped with a java.lang.reflect.InvocationTargetException!!!

try {
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);

//print "Caught:java.lang.reflect.InvocationTargetException"

}


}
}
herve (2) [Avatar] Offline
#2
Re: ch11.4.2 PreserveCheckedException advice not called if using Reflection
replying to my own thread.smilie

found the issue. The problem is that using "call(...)" in the pointcut definition don't work with the reflection API.

Replaced by "execute"
ramnivas (171) [Avatar] Offline
#3
Re: ch11.4.2 PreserveCheckedException advice not called if using Reflection
Sorry, couldn't get to you earlier.

I am glad that you resolved the problem.

Best luck with your AspectJ exploration.

-Ramnivas