siddhant3s
siddhant3s

Reputation: 420

Byte Buddy: Convert an object of a class from one class loader to same class loaded in another class loader

I'm exploring if Byte Buddy is the right tool for me to use. I was looking at this answer which talks about how with Byte Buddy, one can take a class Foo loaded with class loader A, rename it to Bar and redefine it on another class loader B. That's incredible. My question is:

  1. Is it possible to have an object which was created from Foo on class loader A and convert to Bar's object which is coming from class loader B? The solution which I have in mind right now is to basically serialize the object from Foo and then edit the serialized bytes to rename the name of the class. It would be nice if I can do it safely with Byte Buddy. Another solution would be to use Transloader.

  2. Is java-agent or any special instrumentation required for doing all the things that was described in the above SO answer

Thank you so much.

Upvotes: 0

Views: 321

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

You can always load a class in another class loader without using a Java agent. As for translating the class, replacing the symbol of one class name to anther should not be a problem but you need to watch out for serial version UIDs where there name of the class might be a part of. Byte Buddy is a code generation library and does not add any extra help for such operations.

Upvotes: 1

Related Questions