Download weaving time javax.swing. * Classes with AspectJ

I use AspectJ to track calls to the graphics class. So far, I have used the pre-woven set of javax.swing classes. *, And at boot, I tell the JVM to use these woven classes, not the ones from the JRE, using the -Xbootclasspath / p switch.

I would like to switch to weaving mode at boot time. Can anyone help me how to weave javax.swing at boot time. I searched the network, but still I can’t figure out how to do this. 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 the javax.swing classes are loaded before the weaver is attached to the classloader. I think that the weaver does not see these classes at all.

How can I dynamically bind javax.swing classes? Should I implement a custom classloader that first registers the weaver and then loads the class?

Can anyone suggest any solution?

+6
source share
3 answers

The only solution I can offer if you need to use execution() pointcuts for javax packages is to binary weave the JRE tools.jar tool and create your own, woven version. I did this in the past, it worked beautifully. But this is only an option if you have control over the runtime using your application.

If you do not want to use this approach, you will have to call() pointcuts. In this way, you can capture calls made by your own application or any third-party libraries affected by LTW. Only calls made by JRE classes loaded by the bootloader loader will come out of your aspect, which should not be too bad.

+2
source

I am by no means an expert, but so far this looks promising:

Setting boot time with aop.xml files

In particular:

 <aspectj> <aspects> ... </aspects> <weaver options="-verbose"> <!-- Weave types that are within the javax.* or org.aspectj.* packages. Also weave all types in the foo package that do not have the @NoWeave annotation. --> <include within="javax.*"/> <include within="org.aspectj.*"/> <include within="( !@NoWeave foo.*) AND foo.*"/> ... </weaver> </aspectj> 

I'm not sure how this could work when using the ClassPreProcessor used by the LTW agent (although I have not tried it). Here is the beginning of his preProcess method:

 public byte[] preProcess(String className, byte[] bytes, ClassLoader loader, ProtectionDomain protectionDomain) { if (loader == null || className == null || loader.getClass().getName().equals(deleLoader)) { // skip boot loader, null classes (hibernate), or those from a reflection loader return bytes; } ... try { synchronized (loader) { ... WeavingAdaptor weavingAdaptor = WeaverContainer.getWeaver(loader, weavingContext); ... } } } 

This allows you to understand how the loader classes are skipped. Also, calling WeaverContainer.getWeaver() is what seems to trigger aop.xml processing, and this does not happen until a class with a non-zero loader is processed.

Not sure how much this helps with a real solution, but hopefully it sheds light on why everything behaves the way they do ...

+1
source

You are halfway right. It's not that difficult, it's pretty pretty simple.

 <?xml version="1.0" encoding="UTF-8"?> <aspectj> <aspects> <aspect name="com.ciena.EDTCheck"/> </aspects> <weaver options="-Xset:weaveJavaxPackages=true -verbose"> </weaver> </aspectj> 

Make sure you have

aspectjweaver.jar in javaagent as -javaagent: C: \ aspectj1.8 \ lib \ aspectjweaver.jar

also make sure your aop.xml above is in the META-INF folder

that everything to run from Eclipse or some IDE

if you want to run from the command line, make sure you also specify -javagaent

0
source

Source: https://habr.com/ru/post/908184/


All Articles