Jump to: navigation, search

AspectJPitfalls

The official documentation contains a section on common pitfalls.

Advice not matching

When your pointcuts get complicated, it may happen that they don't match and you don't exactly know why. Here are some tips for "debugging" them.

Split into smaller named pointcuts

Using small named pointcuts improves your code by making it more readable. There is no tangible performance impact in using named pointcuts and then combining them.

You can then test single named pointcuts individually, by creating small temporary advices and looking at the AJDT cross reference view or placing a log inside it.

Look at weaver tracing messages

TODO

Temporary remove runtime checks

Runtime checking pointcuts (like if(), cflow() etc..) are difficult to understand and debug. You can try to :

  • Temporary remove them and see if the rest of the pointcut expression is correctly matching.
  • If you are using an if() pointcut, temporary move it inside the advice body. This is a performance anti-pattern, but now you can check (with a log statement or debugging) how the if is evaluated.

Debug cflow()

Debugging cflow() is a bit complicated cause it involves debugging two pointcuts.

Suppose for example :

 pointcut dao() : execution(* DAO+.*(..));
 pointcut log() : call(void Logger.*(..));
 pointcut daoLogs() : log() && cflow(daoOperation());

To see what happens, you can add some temporary advice like the following :

 // temporary advices
 before() : dao() { System.out.println("Entering DAO cflow"); }
 after() : dao() { System.out.println("Exiting DAO cflow"); }
 before() : log() { System.out.println("Entering log call"); }
 after() : log() { System.out.println("Exiting log call"); }


Exceptions

These are some exceptions you could incurr when using AspectJ. Many of these exceptions may happen in plain Java programs too; this document will cover only situations where this exceptions are thrown by AspectJ, that is when you see org.aspectj. in the stacktrace or when you are sure that the program works correctly removing aspects.

VerifyError

If you see a VerifyError exception when running an AspectJ weaved program, then you hit a bug. Please report the exception on the list and/or open a bug on bugzilla.

StackOverflowException

The common cause for this exception is an advice advicing itself. This case is described in the offical documentation pitfalls.

NoClassDefFoundError : org.aspectj...

This means you are missing aspectjrt.jar on you execution classpath. aspectjrt.jar contains the AspectJ runtime classes, which are needed to run a program using AspectJ.

SAXParseException

AspectJ parses all META-INF/aop.xml and META-INF/aop-ajc.xml file present on the classpath. These files are used to control the weaving process. This exception means that there is an error in one of these XML files. The exception cause may report more detailed informations regarding the error.

There is an open bug about DTD parsing of these XML files. You could check if you are hitting that bug.