2. What is AspectJ
• “AspectJ is an aspect-oriented programming (AOP) extension […] for
the Java programming language” [1]
3. What is AspectJ
• AspectJ intercepts code. It can run any code before / after / around /
instead of the original code.
• Other Aspect-Oriented Programming (AOP) libraries do the same
• AspectJ adds code between the caller and the called method
• Does this sound familiar?
• Database Triggers can intercept SQL commands and run something before /
after / around / instead of the original command.
• Wrappers (Proxies, Skeletons) are similar: they come between the caller and
the called method
4. What is AOP good for – typical use cases
• Audit logging
• Transaction-handling
• Start transaction before the method call
• Commit transaction after the method call
• Roll back transaction at exception
• Enforcing security constraints
• Profiling: measure time before and after a method call
• Pluggable architecture – we can add after-event callbacks, just like the
Tapestry web framework does [2]
5. AOP – more use-cases
• Enforcing methods to run asynchronously
• Debug logging
• Extending business logic – just like the normal Object-Oriented
programs would do
• This makes maintenance difficult
• Sometimes this is the only option for somebody else’s code
• This establishes the same dependencies just like the normal Object-Oriented
inheritance would
• AspectJ in Action describes several more use-cases [7]
6. Other AOP solutions for Java
• Other than AspectJ, there are several tools for Aspect-Oriented
programming in Java. One of those tools is AOP Alliance and they list
their competitors [6]. So here is a list:
• AOP Alliance
• ASM
• Javassist
• Jboss-AOP
• Nanning
7. Who uses AspectJ
• “AspectJ has become a widely used de facto standard for AOP by
emphasizing simplicity and usability for end users.” [1]
• Spring, the leading java IoC framework
• Hadoop, the framework for data-intensive distributed system
• AWS SDK for Java – the Software Development Kit for Amazons could
• CAS, the Configuration Server
• MySQL Connector for java
• Xwiki, the collaborative editing tool
• Atlassian Bamboo, the CI server
8. Who uses other AOP tools
• Tapestry web framework uses javassist
• JBoss Application Server dependencies include javassist, ASM
• Provides JBoss-AOP to its users
• The gwt web framework uses AOP Alliance – through Guice IoC
framework
• The Jetty web container uses ASM
• The Wicket web framework uses ASM
9. Can you use AspectJ?
• AspectJ is distributed with Eclipse Public License 1.0 [3] [4]
• Summary:
“Most users only want to use AspectJ to build programs they distribute. There are no restrictions
here. When you distribute your program, be sure to include all the runtime classes from the
aspectjrt.jar for that version of AspectJ. When distributing only the runtime classes, you need not
provide any notice that the program was compiled with AspectJ or includes binaries from the
AspectJ project, except as necessary to preserve the warranty disclaimers in our license.” [5]
• Legally: yes, you can use AspectJ
10. How does AspectJ work?
• AspectJ intercepts code – this is called instrumentalization.
• Code that can be instrumentalized:
• Methods
• Constructors
• Static initializers
• Intercepting means we can run something around the original code:
• We can run stuff before and / or after the original code – so we slightly modify the original
behavior
• We can run stuff instead of the original code – we can specify totally different behavior
• What we write:
• Instruct AspectJ what to intercept – i.e. method, constructor, initializer
• Instruct AspectJ what behavior it should add around the original code
• What AspectJ does: AspectJ is weaving our code with the original code. It makes
our code run around the original code.
11. How does AspectJ work?
• AspectJ weaving modifies the JVM byte code.
• JVM byte code: the language that runs on the Java Virtual Machine (JVM)
• The JVM byte code is a standard language
• Hardly anybody writes byte code. We compile it from a high-level language, like Java, Groovy,
Scala, Kotlin, …
• AspectJ can modify the byte code at compile time.
• It’s called compile-time weaving.
• It’s a convenience for the developers.
• AspectJ can modify the byte code at load time. I.e. when the JVM loads the classes.
• It’s called load-time weaving.
• It’s a way to modify 3rd party code.
• When AspectJ modifies the byte code, it basically applies the Proxy Object Design
Pattern.
• The Proxy Object Design Pattern is a generalization of the Wrapper Design Pattern.
12. What is AspectJ good for
• AspectJ is good to implement Wrappers when the original Service has
lots of SQL config.
• Logging in test servers to figure out what’s going on.
• Implementing cross-cutting concerns quickly
• A controversial use-case:
• “Patching” 3rd party code for experiments
• This is controversial because modifying 3rd party code can cause political
trouble between the vendor and the customer. I.e. whose code is broken
now?
13. What kind of problems can AspectJ cause?
• It might make code harder to maintain
• It has memory requirements
14. How to debug code that uses AspectJ?
• In the exception traces we’ll see things like around<MethodName>
• We can turn it off with the –XnoInline switch. It too has a memory
implication.
• We can always check how the process was started:
• If we see –Djavaagent=…/aspectjweaver.jar, then it uses AspectJ
• The Aspects are registered under META-INF/aop.xml unless specified
otherwise
• When it’s specified otherwise, it’s on the process start parameters, like this:
• -Dorg.aspectj.weaver.loadtime.configuration=META-
INFdebug_logging_aop.xml
• Those parameters are usually specified at the registry or in command files
15. Questions
• If you have any questions, please contact me on twitter:
• https://twitter.com/tamasrev
• You can see a programming guide on eclipse.org :
https://www.eclipse.org/aspectj/doc/released/progguide/starting.ht
ml
16. References
• [1] AspectJ on Wikipedia https://en.wikipedia.org/wiki/AspectJ
• [2] AfterRender annotation in Tapestry
http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/annotations/A
fterRender.html
• [3] Eclipse Public License https://www.eclipse.org/legal/epl-v10.html
• [4] About the AspectJ project https://projects.eclipse.org/projects/tools.aspectj
• [5] AspectJ FAQ: How is AspectJ licensed?
https://www.eclipse.org/aspectj/doc/released/faq.php#q:license
• [6] AOP tools listed at AOP Alliance
http://aopalliance.sourceforge.net/motivations.html
• [7] AspectJ in Action https://www.manning.com/books/aspectj-in-action-second-
edition