Reflective access to bytes[] of loaded class

Gary Frost gary.frost at oracle.com
Thu Feb 9 16:03:10 UTC 2023


I would like to make a case for adding a reflection API for getting the bytes for class loaded by the VM as part of this Classfile API.

Something akin to

byte[] Class.getClassfileBytes();


At present developers usually resort to something like

clazz.getClassLoader().getResourceAsStream(
    clazz.getName().replace(".", "/") + ".class")

Ignoring the fact that we may have just forced an expensive network fetch, to bytes that the JVM clearly already has 'squirrelled away' somewhere...

For me this is hugely problematic as there is no guarentee that the bytes fetched from such a stream will match the bytes that the JVM is using for the class in question....

Java offers all sorts of opportunities (JVMTI agents, Java agents, even custom ClassLoaders) for mutating the incoming classfile's bytes. All of which the stream fetched via getResourceAsStream() completely sidesteps.

This may well be considered 'out of bounds' for the Classfile API, but I think we should consider it, as it seems to be something that users of the Classfile API will need going forward.

Gary
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20230209/a02c21df/attachment.htm>


More information about the classfile-api-dev mailing list