0

Is it possible to redefine Bootstrap classes using Java agent during runtime? To be more specific, I want to redefine some classes in java.io package during runtime after they have been loaded.

1 Answers1

1

Yes, it is possible using a Javaagent that is capable of redefining classes since many classes in java.io will already be loaded once your agent is activated. To do so, you have to create a jar file with a premain method that declares the Can-Redefine-Classes or Can-Retransform-Classes property.

Doing so, you can register a ClassFileTransformer with retransformation capabilities and retransform the classes you require to change. Note that this does not allow you to change the class file layout by for example adding fields or methods.

You can use a library such as ByteBuddy that offers a standard way of retransforming classes using its AgentBuilder to simplify the process. Note that you have to define an ignore matcher that does not exclude all bootstrap classes what is the default to accomplish this.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • Thanks Rafael. Is there any way to inject a field into java.lang.Runnable using that mechanism? If I understood correctly, we can't change the class layout dynamically. – Sanjeev Kumar Sep 05 '18 at 22:32
  • 1
    No, you cannot add a field to this class which will always be loaded prior to your agent. – Rafael Winterhalter Sep 06 '18 at 07:50
  • 1
    Adding a field to an *interface* would be of limited use anyway, as only static constants are allowed in an interface. – Holger Nov 15 '21 at 11:03