Javassist ClassPool instantiation fails silently

62 Views Asked by At

I'm currently trying to write a Java agent for monitoring purposes. I use maven as build system.

I have a Agent.java file:


import java.lang.instrument.Instrumentation;

public class Agent {

    public static void premain(String args, Instrumentation inst) {
        System.out.println("Running premain");

        MonitorTransformer transformer = new MonitorTransformer();
        inst.addTransformer( transformer );
    }

}

And a MonitorTransformer.java file implementing the ClassFileTransformer interface by simply overriding the transform method. This is how the implementation looks like:

@Override
    public byte[] transform(
        ClassLoader loader,
        String className,
        Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain,
        byte[] classfileBuffer
    ) throws IllegalClassFormatException {

        System.out.println("Before ClassPool Instantiation");
        ClassPool classPool = new ClassPool();
        System.out.println("After ClassPool Instantiation");

        return classfileBuffer;
    }

When i run the agent statically by typing the following command

java -javaagent:target/agent.jar -jar example-app.jar

I get the following output:

Running premain
Before ClassPool Instantiation
Before ClassPool Instantiation
Before ClassPool Instantiation
Hello World!
Before ClassPool Instantiation
Before ClassPool Instantiation
Before ClassPool Instantiation
Before ClassPool Instantiation

It seems the print statement after the ClassPool object is instantiatet is not executed. ** Example class is just printing "Hello World" onto the console

I have tried other examples provided in the internet, with the same result. Wrapping it into try/catch block doesn't throw an error either.

0

There are 0 best solutions below