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.
AspectJ is an implementation of aspect-oriented programming for Java. AspectJ adds to Java just one new concept, a join point -- and that's really just a name for an existing Java concept. It adds to Java only a few new constructs: pointcuts, advice, inter-type declarations and aspects.
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.
An aspect weaver takes information from raw classes and aspects and creates new classes with the aspect code appropriately weaved into the classes.
In this article, we'll look at answering these questions and introduce Spring AOP and AspectJ – the two most popular AOP frameworks for Java.
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.
build aspect + Java code with ajc
from scratch (compile-time weaving),
build only aspects with ajc
and Java code with javac
, ejc
or any other Java compiler, then
weave aspects into Java class files via ajc
(post-compile or binary weaving) or
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With