1 Reply Latest reply on Oct 26, 2007 9:11 AM by Andi

    javassist as a profiler in jboss

    Andi Newbie

      Im trying to use javassist to create a profiler for our classes in jboss. i have gotten the -javagent part to work, i have gotten the transform part to work for classes in the run.jar. now i can see my classes(from the ears, wars and jars) being passed into the transformer class. i can even get the CtClass from the pool. but when i try to manipulate the CtClass i get a null pointer exception. what am i doing wrong? i tried to follow the tutorial and it says you have to do x and y for multiple class loaders in jboss but they dont seem to work. any help or links would be appreciated.

        • 1. Re: javassist as a profiler in jboss
          Andi Newbie

          here is my transform method:

          public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
           byte[] transformed = null;
           System.out.println("Loading class: " + className);
           if (!className.startsWith("org/jboss")) {
           ClassPool pool = ClassPool.getDefault();
           Loader cl = new Loader(pool);
           ClassPool child = new ClassPool(pool);
           child.appendClassPath(new ClassClassPath(classBeingRedefined));
           try {
           CtClass c = child.get(className.replaceAll("/", "."));
           if (!c.isModified()) {
           CtMethod[] x = c.getMethods();
           for (CtMethod meth : x) {
           if (meth.getName().equals("finalize")) {
           try {
           meth = c.getDeclaredMethod(meth.getName());
           } catch (NotFoundException e) {
           System.out.println("finalize not found creating it");
           CtMethod m = CtNewMethod.make("public void finalize() throws Throwable { super.finalize();System.out.println(\"calling finalize\"); } ", c);
           c.addMethod(m);
           break;
           }
           System.out.println("modifing " + c.getName() + "'s finalize method");
           meth.insertBefore("{ System.out.println(\"calling finalize\"); }");
           }
           }
           return c.toBytecode();
           }
           } catch (Exception e) {
           e.printStackTrace();
           }
           }
           return transformed;
           }
          


          which throws:


          Loading class: java/io/ObjectStreamClass$FieldReflector
          finalize not found creating it
          Loading class: sun/reflect/GeneratedSerializationConstructorAccessor2
          java.lang.NullPointerException
           at javassist.ClassClassPath.find(ClassClassPath.java:84)
           at javassist.ClassPoolTail.find(ClassPoolTail.java:317)
           at javassist.ClassPool.find(ClassPool.java:495)
           at javassist.ClassPool.createCtClass(ClassPool.java:479)
           at javassist.ClassPool.get0(ClassPool.java:445)
           at javassist.ClassPool.get(ClassPool.java:414)
           at com.healthmarkets.profiler.Xformer.transform(Xformer.java:37)
           at sun.instrument.TransformerManager.transform(TransformerManager.java:122)
           at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:155)
           at sun.misc.Unsafe.defineClass(Native Method)
           at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:45)
           at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:381)
           at java.security.AccessController.doPrivileged(Native Method)
           at sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:377)
           at sun.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerat
          or.java:95)
           at sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:313)
           at java.io.ObjectStreamClass.getSerializableConstructor(ObjectStreamClass.java:1272)
           at java.io.ObjectStreamClass.access$1400(ObjectStreamClass.java:47)
           at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:393)
           at java.security.AccessController.doPrivileged(Native Method)
           at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:373)
           at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:268)
           at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:369)
           at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:268)
           at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1029)
           at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:291)
           at javax.management.remote.rmi.RMIConnectorServer.encodeJRMPStub(RMIConnectorServer.java:715)
          
           at javax.management.remote.rmi.RMIConnectorServer.encodeStub(RMIConnectorServer.java:708)
           at javax.management.remote.rmi.RMIConnectorServer.encodeStubInAddress(RMIConnectorServer.java
          :680)
           at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:438)
           at sun.management.jmxremote.ConnectorBootstrap.startLocalConnectorServer(ConnectorBootstrap.j
          ava:390)
           at sun.management.Agent.premain(Agent.java:92)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:585)
           at sun.management.Agent.startAgent(Agent.java:197)
          Loading class: sun/reflect/GeneratedSerializationConstructorAccessor3
          java.lang.NullPointerException
           at javassist.ClassClassPath.find(ClassClassPath.java:84)
           at javassist.ClassPoolTail.find(ClassPoolTail.java:317)
           at javassist.ClassPool.find(ClassPool.java:495)
           at javassist.ClassPool.createCtClass(ClassPool.java:479)
           at javassist.ClassPool.get0(ClassPool.java:445)
           at javassist.ClassPool.get(ClassPool.java:414)
           at com.healthmarkets.profiler.Xformer.transform(Xformer.java:37)
           at sun.instrument.TransformerManager.transform(TransformerManager.java:122)
           at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:155)
           at sun.misc.Unsafe.defineClass(Native Method)
           at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:45)
           at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:381)
          


          any help would really be appreciated