Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ compiler (ajc) vs load-time weaving

Tags:

aspectj

Several questions here:

  • Does ajc change all the classes it compiles (even non-aspect ones)? What if I compile only aspect classes ant then put them in the same classpath with the common ones?

  • Does the ajc-compiled project perform faster then the one that uses load-time weaving?

  • What if I need to write a library, that does tracing with AspectJ, and then I want this library to work with ANY project? Is load-time weaving the only option in this case?

like image 596
weekens Avatar asked Apr 19 '11 14:04

weekens


People also ask

What is AJC compiler?

The ajc command compiles and weaves AspectJ and Java source and . class files, producing . class files compliant with any Java VM (1.1 or later). It combines compilation and bytecode weaving and supports incremental builds; you can also weave bytecode at run-time using Load-Time Weaving.

What is weaving in AspectJ?

Weaving in AspectJ Classes are defined using Java syntax. The weaving process consists of executing the aspect advice to produce only a set of generated classes that have the aspect implementation code woven into it.

What is post compile weaving?

Post-compile weaving (also sometimes called binary weaving) is used to weave existing class files and JAR files. As with compile-time weaving, the aspects used for weaving may be in source or binary form, and may themselves be woven by aspects.

What is runtime weaving?

Runtime :Aspects are woven in sometime during the execution of the application. Typically, an AOP container will dynamically generate a proxy class that will delegate to the target class while weaving in the aspects.


1 Answers

  1. ajc (compile time) will only change classes that are affected by aspects. Remember, ajc is an extension of a Java compiler (to be precise, it is based on Eclipse 3.3's JDT compiler). So, it will compile all your Java classes as would a normal Java compiler. It will then additionally weave all classes that are affected by an aspect. If you compile your aspects separately from your non-aspects then there will be no compile time weaving going on and your aspects will not have any affect. However, you can put your aspects on the aspect path of the compilation of your non-aspects (if your non-aspects are compiled by ajc). This will allow your non-aspects to be woven by your aspects.
  2. Start-up time under CTW is much better than LTW, but after all classes are loaded, speed difference should be negligible. The reason is that under LTW, all classes are woven when they are loaded. This means that classloading requires the additional step of weaving which is not necessary under CTW.
  3. No. As mentioned above, you can add the aspects to your aspect path of the second project, and then they will be woven during compilation.

More on Aspect path:

http://www.eclipse.org/aspectj/doc/released/devguide/ajc-ref.html

like image 143
Andrew Eisenberg Avatar answered Sep 21 '22 15:09

Andrew Eisenberg