This opus appeared in connection with the article
habrahabr.ru/blogs/java/74208UPD. Much more suitable solution and its discussion -
habrahabr.ru/blogs/java/74208/#com_2141270Below is the information as it is intended, but does not work yet (maybe it will not work at all :(). We need to do 3 things:
')
First, we create the interface and want the
java.lang.Thread
class to implement it:
public interface TreeInfo { public Thread getParentThread(); }
Secondly, we need to save somewhere the value of
Thread.currentThread()
when calling the constructor of the class Thread.
Thirdly, you need to somehow "fly up with all this garbage."
The first 2 points are solved as follows:
Create a class
public class ThreadTreeMixin { Object target; Thread parent; public ThreadTreeMixin(Object target) { this.target = target; this.parent = Thread.currentThread(); } public Thread getParentThread() { return parent; } }
And we tell JBoss AOP that we want to mix our
TreeInfo
interface and the
ThreadTreeMixin
class with
java.lang.Thread
. To do this, we create the file META-INF / jboss-aop.xml in the program's resources with the following contents:
<aop> <introduction class="java.lang.Thread"> <mixin> <interfaces> threadtree.TreeInfo </interfaces> <class>threadtree.aspects.ThreadTreeMixin</class> <construction>new threadtree.aspects.ThreadTreeMixin(this)</construction> </mixin> </introduction> </aop>
And the final "non-working" part (paragraph 3).
We take off with this as usual - with the help of javaagent:
java -javaagent: jboss-aop-single.jar -classpath myapp.jar: ... myapp.Main
The agent itself will already read the contents of the file myapp.jar! /META-INF/jboss-aop.xml and perform the necessary bytecode manipulations.
And now the saddest thing is that apparently the JBoss AOP does not want to work with the JRE classes, although everything works fine with the project classes (well, or I “do not know how to cook it”). Can someone tell me the solution? At AspectJ, at least in the documentation, it is directly written that they do not support system classes (all with classLoader == null).