My goal is to access the byte[] representing the bytecode of a class without specifically knowing the location of the class files at runtime.
I have looked into two solutions and was able to get mild success out of one of them, but I was wondering if there might be other ways to accomplish it (or how I went wrong in the second solution that I couldn't get to work).
My first (mildly) successful solution was to use the java.lang.instrumentation ClassFileTransformer class to access the byte[] of the classes. Though this workek, I assumed that there must be a cleaner way to accomplish this.
My second solution was to use the -Xbootclasspath JVM argument to replace java.lang.ClassLoader with my own allowing it to have access to the byte[] of the classes loaded. I added a simple System.out.println debug message to confirm that the overriding of the ClassLoader was working, but it wasn't. I got this idea from this paper on the same subject. My class was made similarly to how the Integer class was remade in the linked paper. I also used a similar directory setup for the JVM argument looking something like this:
java -Xbootclasspath/p:.\out\production\boot\java\lang TestLoader
My thought is that the ClassLoader class specifically cannot be overridden using the method in the paper I linked.
I would be interested seeing why my attempt at overriding the ClassLoader did not work and also in hearing what else I could do to access the byte[] of classes.
Could you just read the class bytes using getResourceAsStream()?
InputStream is = String.class.getResourceAsStream("String.class");
Edit adding alternative:
(copied from comment)
Given all the possibilities that need to be covered, ClassFileTransformer and instrumentation API might be the way to go. I don't know what the requirements are for 'clean', but if the issue is having to specify command line arguments to the JVM you could try using the Attach API - you can attach to an already running Java process, push in your ClassFileTransformer, and look at all the classes already loaded in the JVM plus any that are loaded thereafter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With