I try to hijack/intercept a method of a class in EclipseLink. I had tried Spring AOP and AspectJ and I failed. I want to do something when class org.eclipse.persistence.internal.localization.i18n.TraceLocalizationResource
throw exception while calling method getString(..);
My implementation as below:
@AfterThrowing(pointcut = "execution(* org.eclipse.persistence.internal.localization.i18n.TraceLocalizationResource.getString(..))",throwing="error")
public void logAfterThrowing(JoinPoint joinPoint,Throwable error) {
....
}
And calssTraceLocalizationResource
was called (I debug into it). But the hijack method wasn't called.
MY questions:
Basically the syntax looks okay, but I have not tried to replicate a full aspect from your code snippet here. Furthermore, I only use native AspectJ syntax and am not quite familiar with annotation-based syntax, so if there is a problem there I might not have seen it.
No, but if you want to weave a library (JAR or unpacked into the file system) used by your main application, you must make sure that it is in the in-path for the weaver. Please consult AspectJ documentation for the syntax. Edit: I posted some helpful links in another answer earlier today.
By default, packages org.aspectj
, java
and javax
are excluded from weaving. For the latter two, there are command line switches to enable weaving. But LTW will be tricky at best and a pain in the a** (next to impossible) at worst because you have a hen-and-egg problem there: The AspectJ weaver runtime classes need the JDK, and JDK bootstrapping happens before any Java agent like the weaver is attached. So your best bet is compile-time weaving for JDK classes and then using the woven rt.jar later in your application. Even with compile-time weaving you might run into issues if your advice use JDK woven classes themselves. You want to make sure to avoid infinite recursions there. But it is possible.
Update: Another hint which sometimes seems too simple to mention for someone who has used AspectJ for a while: Instead of an execution
pointcut which makes it necessary to weave the target class, you may just use a call
pointcut which intercepts (as the name implies) calls to the target everywhere in your client classes. It might be a little less efficient, but maybe your best and easiest bet unless you really want to take the trouble of weaving the JDK.