-
1. Re: Acting on class load
kabirkhan Oct 29, 2008 7:51 AM (in response to fbrueseke)AOP out of the box does not support this. If you are running in JBoss AS with -javaagent, you can add an addidional transformer to PluggableInstrumentor's Instrumentation as shown here
https://svn.jboss.org/repos/jbossas/projects/aop/branches/Branch_2_0/asintegration-core/src/main/org/jboss/aop/asintegration/core/AspectManagerServiceDelegateJDK5.java
When running standalone with -javaagent, rather than using PluggableInstrumentor, you use https://svn.jboss.org/repos/jbossas/projects/aop/branches/Branch_2_0/aop/src/main/org/jboss/aop/standalone/Agent.java instead, which unfortunately does not allow you access to the Instrumentation. I have added support for this in svn. https://jira.jboss.org/jira/browse/JBAOP-672[kabir@~/sourcecontrol/jboss-aop/trunk/subversion] $svn diff aop/src/main/org/jboss/aop/standalone/Agent.java Index: aop/src/main/org/jboss/aop/standalone/Agent.java =================================================================== --- aop/src/main/org/jboss/aop/standalone/Agent.java (revision 79975) +++ aop/src/main/org/jboss/aop/standalone/Agent.java (working copy) @@ -35,9 +35,16 @@ */ public class Agent { + private static Instrumentation instrumentation; + + public static Instrumentation getInstrumentation() + { + return instrumentation; + } public static void premain(String agentArgs, Instrumentation inst) { + instrumentation = inst; StandaloneClassPoolFactory factory = new StandaloneClassPoolFactory(); AspectManager.setClassPoolFactory(factory); // necessary for configuration
-
2. Re: Acting on class load
fbrueseke Oct 29, 2008 8:22 AM (in response to fbrueseke)Thanks for the quick answer.
So what I would have to do is:
- Implement my own class that conforms to the interface "java.lang.instrument.ClassFileTransformer"
- Register an object of that class with the PluggableInstrumentor
and then I'm ready to go?
So in code:public class MyTransformer implements ClassFileTransformer { public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { //do something ... } } public class someOtherClass { //all static for simplicity public static MyTransformer transformer= new MyTransformer(); static{ PluggableInstrumentor.getInstrumentor().addTransformer(transformer) } }
Is that correct?
Kind regards
Frank -
3. Re: Acting on class load
kabirkhan Oct 29, 2008 8:40 AM (in response to fbrueseke)I've not actually tried this, but I think something like what you say should work. You have access to the classloader and the classname, so you can register that with you registry. Once the class has completed, i.e. after your transformer and the aop transformer have finsished, you should be able to load the class in your registry using the classname and classloader.
I would wrap what your SomeOtherClass does in an MBean so that you have control over the transformer. Let us know how you get on! -
4. Re: Acting on class load
techtigre1 Sep 29, 2011 12:16 PM (in response to fbrueseke)Frank,
I know this is an old post, just wanted to know if your above-mentioned post worked. I have need to do a
similar thing under JBoss5.x, enabled with custom ClassFileTransformer.
-Techtigre