Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Getting Bytecode of Class at Runtime from within the Same JVM

Related to: Is there a way to obtain the bytecode for a class at runtime?

I'm adding durability to Clojure, and I'm finally at the point where I'm ready to add functions. In Clojure, functions are byte compiled into classes with invoke methods (among others). In this way, functions are first class. To make these durable, I need to serialize and deserialize these classes. How do I get the bytecode for the class without having access to the .class file?

Please correct me if I'm mistaken, but using an agent requires spawning a separate VM with the agent connecting to the first VM. I need to do it from the same VM.

It's not enough to use Serializable to set and get the Class object. Upon deserializing, I need to load the class, and upon subsequent VM instances, there may be a class name collision. I need to modify the bytecode to rename the class to something unique at deserialization/class-load time.

like image 587
jennykwan Avatar asked Nov 09 '10 05:11

jennykwan


People also ask

Does JVM convert source code to byte code?

Remember,JVM only works during Run Time means after the compilation of Source Code into Byte Code..but before that javac compiles the source code into byte code. JVM converts bytecode into corresponding machine code by JIT Compiler and Java Interpreter.

How is bytecode executed in JVM?

They can be executed by intepretation, just-in-time compiling, or any other technique that was chosen by the designer of a particular JVM. A method's bytecode stream is a sequence of instructions for the Java virtual machine. Each instruction consists of a one-byte opcode followed by zero or more operands.

What provides runtime environment where Java bytecode is executed?

JVM is the abbreviation for Java virtual machine which is basically specification that provides a runtime environment in which Java byte code can be executed i.e it is something which is abstract and its implementation is independent to choose the algorithm and has been provided by Sun and other companies.

Can byte code compiled by a Java compiler run on different machines?

Nope. Byte code will differ on which compiler you use it in.


3 Answers

You could write your own ClassLoader and hack up a scheme which records the bytecode as classes are loaded.

You would need to override findClass to find the class file yourself, load it into memory, save the data somewhere (for later serialization), then call defineClass to define that class in the JVM.

like image 138
Keith Randall Avatar answered Oct 22 '22 17:10

Keith Randall


Unless you are running code via a tricky classloader, you should be able to do something like this:

Class<?> clazz = ....
String className = clazz.getCanonicalName();  // e.g. "foo.Bar"
String resourceName = ... // map className to a resource name; e.g. "/foo/Bar.class" 
InputStream is = clazz.getClassLoader().getResourceAsStream(resourceName);

This gives you a handle on the contents of the ".class" file ... if it can be found.

Caveats. Some classloaders might:

  • not let to open the ".class" resources at all,
  • give you an encrypted bytecode stream, or
  • give you bytecodes that are nor exactly what is being run, due to some on-the-fly transformation performed by the classloader.

If this approach doesn't work, you are pretty much out of options because the JVM does not provide a way to access the actual bytecodes that were loaded.

like image 3
Stephen C Avatar answered Oct 22 '22 17:10

Stephen C


You can also use the Java Instrumentation API for this. You get access to the bytes of the classfile before defineClass is invoked. You can change them too!

like image 1
Jeff Williams Avatar answered Oct 22 '22 19:10

Jeff Williams