Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does AspectJ work?

Tags:

java

aop

aspectj

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.

like image 941
Kam Avatar asked Feb 01 '17 08:02

Kam


People also ask

What is AspectJ used for?

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.

How does AspectJ work?

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.

What does AspectJ Weaver do?

An aspect weaver takes information from raw classes and aspects and creates new classes with the aspect code appropriately weaved into the classes.

Is AspectJ a framework?

In this article, we'll look at answering these questions and introduce Spring AOP and AspectJ – the two most popular AOP frameworks for Java.


1 Answers

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.

like image 101
kriegaex Avatar answered Oct 13 '22 01:10

kriegaex