Search code examples
javaaspectjinstrumentation

java.lang.instrument vs AspectJ


For instrumentation in Java, why java.lang.instrument is preferred over AspectJ though both does the job of instrumentation equally. Is there any specific advantage of using java.lang.instrument? Thanks in advance...


Solution

  • Those are two completely different things. The Instrumentation API offers a hook into the JVM runtime which allows Java agents to access more advanced features of the JVM. It also offers a hook into the JVM's class loading where an agent can be notified when a class is loaded and redefine its class file. Similary, it is possible to redefine/retransform already loaded classes.

    AspectJ is a library that offers a more high-level view on redefining existing classses. It works with point cuts and join points to explain how a class should be redefined. The redefinition by AspectJ is then either applied at compile-time with the help of a tool like Maven or AspectJ can use a Java agebt and the instrumentation API to apply the redefinition of classes at runtime.