How does AspectJ work?
Asked Answered
Q

1

12

I am trying to understand how Aspect works. I come from a C/C++ background where magic never happens.

I understand that you can annotate some functions with @Aspect then write down the Aspect implementation and so on. But, how (and at what time) does the new code get generated?

Assuming I have no editor. I compile java classes using javacc command that will generate .class files. Now, assume that the java files are annotated using Aspect. Shouldn't I then compile the .class files again with Aspect somehow to generate another set of .class files?

If my understanding is correct, how is this dual compilation step done in maven? or spring? I found many tutorial that will tell you what to add here and there to get things working but no tutorial explains how these things are actually working.

Quinquefid answered 1/2, 2017 at 8:11 Comment(3)
Exactly. There are command-line tools for weaving. You should check the official aspectj documentation: eclipse.org/aspectj/doc/released/devguide/ajc-ref.htmlDeveloping
@PhilippWendler I guess that's also a point I am trying to make. Why are the official AspectJ docs found on eclipse.org. I find this very confusing that AspectJ and an IDE are intermingled as if it is an Eclipse featureQuinquefid
"Eclipse" is much more than just the IDE Eclipse: en.wikipedia.org/wiki/Eclipse_FoundationDeveloping
O
20

It is easy to tell that you are a C++ guy. There is no such thing as javacc (if you are not talking about the Java Parser Generator of the same name) but the Java Compiler is called javac. ;-)

As Philipp Wendler already pointed out, Eclipse is not just an IDE, it is an organisation similar to Apache, and AspectJ has been adopted many years ago as one of its projects. This has also the advantage that AJDT (AspectJ Development Tools) for Eclipse IDE are sort of the best AspectJ support you can get for any IDE, sadly including my favourite IntelliJ IDEA which is superior to Eclipse IDE in almost all respects except its AspectJ support being rather mediocre.

So much for the minor topics. Now as for your main question, it is not really suited for Stack Overflow because it is rather a forum question, not about a concrete programming problem requiring a solution. Anyway, I love AspectJ and will answer briefly:

AspectJ has its own compiler ajc which is basically an enhanced version of the Eclipse Java Compiler ejc, i.e. it can be used to compile normal Java files as well as aspects in native AspectJ syntax or in @AspectJ style annotation-based syntax. No matter which way you weave your aspects, e.g.

  1. build aspect + Java code with ajc from scratch (compile-time weaving),

  2. build only aspects with ajc and Java code with javac, ejc or any other Java compiler, then

  3. weave aspects into Java class files via ajc (post-compile or binary weaving) or

  4. weave aspects into Java class files at runtime during class-loading with a Java agent called the AspectJ Weaver (load-time weaving, LTW),

What AspectJ does is always pretty much the same: It modifies Java byte code by weaving aspect code into it. In case 1 you just get one set of class files directly from ajc. Case 2.1 creates additional, new class files. Case 2.2 just creates new byte code in memory directly in the JVM.

Oceanid answered 1/2, 2017 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.