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!
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:
editClass(Class<?> clazz)
and editClass(String qualifiedName)
premain
method (or agent classes in general) using editClass(Class<?>)
and enjoy compile-time safety regarding the existence of the classes referenced via literalseditClass(Class<?>)
with editClass(String)
ldc packagename/Foo.class
, invokestatic (Ljava/lang/Class;)V
with ldc "packagename.Foo"
, invokestatic (Ljava/lang/String;)V
.editClass(String qualifiedName)
can handle the internal class names (using slashes instead of dots).