Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aspect weaving at runtime

I'm looking for a Java solution that would allow me to use AOP to weave new code on top of already running code at runtime. The key is not to require the restart of the JVM. Also, I'd like to remove the woven at runtime, leaving the old code running the way it was before weaving.

I'm thinking AspectJ load time weaving + runtime class loading/unloading would do it. Has anyone tried it? Any recommendations? Thank you.

like image 505
Faustas Avatar asked Jun 26 '13 14:06

Faustas


People also ask

What is runtime weaving?

Load-time weaving (LTW) is simply binary weaving defered until the point that a class loader loads a class file and defines the class to the JVM. To support this, one or more "weaving class loaders", either provided explicitly by the run-time environment or enabled through a "weaving agent" are required.

What are some different types of aspect weaving?

If you have grasped the basic concepts of AOP (moreover AspectJ) and want to know the details about different types of AspectJ weaving, which are Compile-time weaving, Post-compile (binary) weaving, and Load-time weaving, do continue reading this article.

What is weaving in aspect?

Weaving in AspectJ Classes are defined using Java syntax. The weaving process consists of executing the aspect advice to produce only a set of generated classes that have the aspect implementation code woven into it.

Should I use AspectJ?

AspectJ is very mature, powerful and widely used in today's enterprise Java frameworks like Spring. Spring uses AspectJ to make it easy for the developers to use Transaction Management, or applying security (using Spring Security) in your application.


2 Answers

A few things to consider:

  • True, you can do LTW during class loading, but not after a class has already been loaded.
  • There is no such concept as class unloading because for a class to be unloaded it needs to be garbage-collected and for that no references to the class must exist anymore. Even if the latter was the case, the JVM specification AFAIK declares it as optional whether or not and when unloading or GC should clear off an already loaded class. You could never rely on it.

Having said that, you either can try concepts such as OSGi or write your own class loader (or find one of many existing ones on the Internet) which loads each class or each JAR in a separate classloader instance. This can get arbitrarily complex, so maybe you want to consider this simple approach as long as it is within the technical bounds of your situation:

  • Compile your aspects into your code or use LTW, it does not really matter. Just make sure that the aspect code is woven before the classes are actually used. Compile time is obviously more than soon enough, load time is just barely soon enough, but fine.
  • Use if() pointcuts for all relevant advice and provide a means to dynamically change the value of the variable used by the pointcuts so as to be enable to dynamically switch advice on and off. The performance overhead will usually be minimal, don't worry. Just give it a try before you say it is too expensive.

This solution satisfies your conditions that it can be (de)activated dynamically and that no JVM restart is necessary after the aspect code has been woven.

like image 157
kriegaex Avatar answered Oct 14 '22 04:10

kriegaex


Aspect

An aspect is the cross-cutting functionality you are implementing. It is the aspect, or area, of your application you are modularizing. The most common (albeit simple) example of an aspect is logging. Logging is something that is required throughout an application. However, because applications tend to be broken down into layers based on functionality, reusing a logging module through inheritance

does not make sense. However, you can create a logging aspect and apply it throughout your application using AOP.


Weaving

Weaving is the process of applying aspects to a target object to create a new, proxied object. The aspects are woven into the target object at the specified joinpoints. The weaving can take place at several points in the target class’s lifetime:

  • Compile time :Aspects are woven in when the target class is compiled. This requires a special compiler.
  • Classload time :Aspects are woven in when the target class is loaded into the JVM. This requires a special ClassLoader that enhances that target class’s bytecode before the class is introduced into the application.
  • Runtime :Aspects are woven in sometime during the execution of the application. Typically, an AOP container will dynamically generate a proxy class that will delegate to the target class while weaving in the aspects.

enter image description here
enter image description here

(Source)

like image 37
Premraj Avatar answered Oct 14 '22 03:10

Premraj