Can change static methods at run time using java Instrumentation?

941 Views Asked by At

I need to change static methods inside third party libraries. I have done transforming instance method but not sure same thing applies to static methods.

I have several static methods called myMethod inside MyClass.java. I need to change implementation of myMethod during run time. Basically I extended ClassFileTransformer and overrode byte[] transform method something like bellow. This worked for instance methods but I am not sure if I can do the same for static methods.

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

}
1

There are 1 best solutions below

0
Rafael Winterhalter On

Yes, of course this is possible. You can change the entire class file. For most VMs, you can only change the implementation of the method but not its signature. If a method is static or not does not matter.