Search code examples
javareflectioninstrumentationjavaagents

Java Instrumentation: Prevent class reference from loading class


Hello StackOverflow Community,

I recently discovered Java Instrumentation and what great things you can do with it, so I decided to write a small library for me that simplifies some of these things.

I have the following method (simplified):

public static void editClass(Class<*> clazz) {
   ...
}

It adds a transformer via Instrumentation that transforms the bytecode of loaded classes with the name of clazz.getName().

So in my premain method, I can say

editClass(Foo.class);

My problem is, by specifying the class via a reference to it (.class), this class gets loaded before the transformer is added, so after that, I have to retransform the class which prevents me from adding/removing methods and so on.

So, is there a way to not load the class when using this class reference? Or an other way to implement this? I know that I could just pass the class name as an argument, but I would really like to make this whole library type-safe and make refactoring easier.

Thanks in advance!


Solution

  • If you want to call the editClass method from premain only and we assume that the Java Agent itself does not use the class otherwise, so that the class literal inside the editClass call would be the only trigger, you can do the following:

    • provide both methods, editClass(Class<?> clazz) and editClass(String qualifiedName)
    • write the premain method (or agent classes in general) using editClass(Class<?>) and enjoy compile-time safety regarding the existence of the classes referenced via literals
    • perform a static code transformation of the agent classes, replacing all calls of editClass(Class<?>) with editClass(String)
      This shouldn’t be too hard, as you only have to replace all sequences of ldc packagename/Foo.class, invokestatic (Ljava/lang/Class;)V with ldc "packagename.Foo", invokestatic (Ljava/lang/String;)V.
      It may become even easier when the method editClass(String qualifiedName) can handle the internal class names (using slashes instead of dots).
      Since you said you “recently discovered Java Instrumentation”, this might be a good exercise in class file transformations
    • Use the transformed Agent classes which have no references to the classes to transform anymore, to perform the load time transformations