Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get bytes for a class that was generated at runtime

I am working with a Java framework that generates some (proxy) classes at runtime, using a custom ClassLoader. I would like to get for any such class that the custom ClassLoader returns from loadClass(..) the raw byte array that corresponds to this class. Is this possible? I know that if a class exists as a resource then you can use an input stream to load the class in binary format but how can I go about this if the class is generated at runtime?

like image 389
user66237 Avatar asked Jan 15 '10 19:01

user66237


2 Answers

If you replace the custom ClassLoader with your own, you can add some mechanism for saving the raw bytes yourself.

like image 192
Adam Goode Avatar answered Oct 19 '22 11:10

Adam Goode


Register a ClassFileTransformer. Rather than modifying the bytes, record the data you need.

like image 25
Brett Kail Avatar answered Oct 19 '22 11:10

Brett Kail