4 Replies Latest reply on Sep 29, 2011 12:16 PM by T Rich

    Acting on class load

    Frank Brüseke Newbie

      Hi JBoss AOP folks,

      My question is the following:
      Can I use JBoss AOP to act upon a class being loaded?
      The AOP framework catches this event anyways doesn't it?

      My particular problem is that I want to enter classes that implement a common superclass into a registry as soon as they are loaded. Is that possible?
      I considered introductions, but this is no good as then I can only register on the first instantiation of an object.

      Hope you can help.
      Kind regards

        • 1. Re: Acting on class load
          Kabir Khan Master

          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

          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

          $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();
           // necessary for configuration

          • 2. Re: Acting on class load
            Frank Brüseke Newbie

            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();

            Is that correct?

            Kind regards

            • 3. Re: Acting on class load
              Kabir Khan Master

              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
                T Rich Newbie



                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.