Monday, January 2, 2012

AspectJ vs Javassist

AspectJ:

Advantages:
  • Very powerful, can do nearly anything you might need
  • Powerful pointcut expressions for defining where to inject an advice and when to activate it (including some run-time checks) – fully enables DRY, i.e. write once & inject many times
  • Both build-time and load-time code injection (weaving)
Disadvantages:
  • The modified code depends on the AspectJ runtime library
  • The pointcut expressions are very powerful but it might be difficult to get them right and there isn’t much support for “debugging” them though the AJDT plugin is partially able to visualize their effects
  • It will likely take some time to get started though the basic usage is pretty simple (using @Aspect, @Around, and a simple pointcut expression, as we will see in the example)

Javassist:

Advantages:
  • No run-time dependencies if build-time weaving used (load-time weaving requires the GluonJ agent library or gluonj.jar)
  • Simple Java syntax using GlutonJ’s annotation – though the custom syntax is also trivial to understand and easy to use
  • Easy, automatic weaving into the target classes with GlutonJ’s JAR tool, an Ant task or dynamically at the load-time
  • Support for both build-time and load-time weaving
Disadvantages:
  • An aspect can modify only a single class, you cannot inject the same piece of code to multiple classes/methods
  • Limited power – only provides for field/method addition and execution of a code instead of/around a target method, either upon any of its executions or only if the execution happens in a particular context, i.e. when called from a particular class/method



No comments: