Suppose in my JVM I have a loaded class Class<C> myClass
. Is there a reliable way to ask the JVM for the bytecode contents of the .class? I.e. something like this:
<C> byte[] getClassBytecode(Class<C> myClass) {
return /* the contents of the .class resource where C was loaded from */;
}
(obviously an InputStream
would be as good as the byte[]
). I know I can use myClass.getResource()
(and friends) to fetch the class file, but hammering on the class name to get an URL to feed to getResource feels wrong. Also, I am not sure how this would behave in case C
was dynamically generated (e.g. using javax.tools.JavaCompiler
).
Any (better) idea?
note: the goal is to be able to push the bytecode classes to a different JVM and use a custom classloader to load them there
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…