Confused how ajc works with javac

3.6k views Asked by At

I'm a little confused how the AspectJ compiler, ajc works. To my understanding when talking about CTW, ajc is used to weave aspects into the compiled byte-code - ie: the .class file.

However, when I look at the maven-plugin for AspectJ (aspectj-maven-plugin), it turns out that it is run in the generate-sources phase of maven, before the javac compiler. Which would imply that the compiler runs after the aspect weaving. That kind of makes sense, since you can weave in ITDs, modifying class members, etc. which the java compiler would need to know about in order to compile any dependent classes.

So if that is the case, and ajc runs before javac, I presume that ajc must first compile all the java code into byte code to be able to weave in any aspects.

So the question is, if ajc already goes through the efforts of compiling all the java code into byte code, why does javac even need to run at all? Why isn't ajc the only compiler that is required? Isn't having both run just duplicating the efforts? Additionally, how does javac handle the classes that ajc has already compiled? Does it just ignore them since there is no change in the source file since the .class file was generated?

3

There are 3 answers

2
aepurniet On

ajc can compile all the classes, its built on the eclipse java compiler. ajc is the only compiler that is required to generate classes.

as far as duplicated efforts, javac will most likely not overwrite .class files that have a newer timestamp than the source java file. also you can imagine builds where some of the sources are compiled with ajc, and some with javac.

as far as maven scheduling goes, i dont know.

0
vach On

Aspectj does not need javac to compile java code. Some previous versions of AspectJ had this requirement. As far as i know javac can still be used as ajc back end by using the -usejavac flag. You can also run ajc in preprocessor mode to generate Java source (.java) files to be compiled using javac or another java compiler.

So the answer is javac is not running at all and there is no duplicate effort.

0
Andy Clement On

I wonder if the phase for the aspectj maven plugin that you observe is a left over from the very old days when AspectJ was a source transformer. This was before AspectJ 1.2. At that time it produced sources that then had to go through a compiler (javac). Now that is no longer the case, ajc can compile everything from source to code. Or sometimes it is used after javac to weave things. For example if using annotation style aspects you can compile the code with javac and then a binary weave of those class files with ajc will weave the aspects into the other classes.