2

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!

3
  • Given that Spring Boot does bytecode analysis on the class files specifically to enable the ability to use class literals as annotation arguments, I'm pretty sure there's no way to do it inline. Nov 30, 2020 at 21:44
  • Who was first, the chicken or the egg? You cannot reference a class at the same time as transforming it before it is being referenced (and thus resolved). Either you use class name strings or you have to live with the limitations involved in retransformation, such as the ones you described. There is no magic wand here. Maybe if your class was in a quantum state like Schrödinger's cat, both loaded and not loaded at the same time, you could devise a scheme to pull this off. 😉
    – kriegaex
    Dec 1, 2020 at 11:49
  • 1
    Your wish to make the operation type safe suggests that the caller of the editClass method will use the Foo class anyway, so attempts to control the loading time are doomed to fail (or end up in very fragile code). The Foo class could be loaded when the calling class is linked or verified.
    – Holger
    Dec 3, 2020 at 11:36

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.