Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load time weaving of javax.swing.* classes with AspectJ

I am using AspectJ to trace calls to graphics class. So far, I have used a pre-weaved set of javax.swing.* classes and upon loading I tell JVM to use these weaved classes and not the ones from JRE by using -Xbootclasspath/p switch.

I would like to switch to load time weaving mode. Can anyone help me how to weave javax.swing on load time. I have searched the net but still I cannot figure it out how to do it. I know that by default, AspectJ load time weaver will not weave java.* and javax.* classes. Someone suggested using

-Xset:weaveJavaPackages=true,weaveJavaxPackages=true

in aop.xml but none of this helped because javax.swing classes are loaded before the weaver is attached to the classloader. I guess that the weaver does not see these classes at all.

How can I manage to dynamically weave javax.swing classes? Should I implement a custom class loader that first registers a weaver then does the class loading?

Can someone please suggest any solution?

like image 843
Gonny Avatar asked Feb 10 '12 21:02

Gonny


People also ask

What is weaving in AspectJ?

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.

What is load time 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 is weaving in AOP?

Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime.

What is Java weave?

Weaving is a technique of manipulating the byte-code of compiled Java classes. The EclipseLink JPA persistence provider uses weaving to enhance both JPA entities and Plain Old Java Object (POJO) classes for such things as lazy loading, change tracking, fetch groups, and internal optimizations.


1 Answers

The only solution I can suggest if you need to use execution() pointcuts for javax packages is to binary-weave the JRE's tools.jar and create your own, woven version. I have done that in the past, it worked nicely. But this is only an option if you have control over the runtime environment running your application.

If you do not want to use such an approach, you will have to resort to call() pointcuts. This way you can capture calls made by your own application or any 3rd party libraries affected by LTW. Only calls made by JRE classes loaded by the boot classloader will escape your aspect, which should not be too bad.

like image 108
kriegaex Avatar answered Nov 06 '22 02:11

kriegaex