12

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.

Community
  • 1
  • 1
jennykwan
  • 2,631
  • 1
  • 22
  • 33
  • I'm by no means an expert on the topic, but it might be worth trying to persist the function *definitions* rather than the underlying bytecode. you can then just recompile the functions to bytecode when you load them back in. – mikera Nov 09 '10 at 20:27

3 Answers3

5

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.

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • p.s. I think agents run in the same VM as the main program, so they may work for you. – Keith Randall Nov 09 '10 at 06:46
  • Javassist already implements this scheme and offers a somewhat convenient access to the bytecode in question. BCEL might also do something similar. – Blaisorblade Jul 08 '11 at 15:02
  • Minimal implementation is pretty trivial: https://gist.github.com/Felk/ed4375d27c755e21d0e6893847286d93 (I know this post is 7 years old) – Felk Sep 15 '17 at 13:56
3

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 3
    Alas, this only works if the .class is written to disk as a resource in the first place. Clojure does not do this. – jennykwan Nov 10 '10 at 23:04
  • It also has problems when the class is defined inside another. Eg.: `class Foo { class Bar {} }` should become "/Foo$Bar.class" – iliis Apr 21 '13 at 17:01
  • 1
    `clazz.getResourceAsStream('/'+clazz.getName().replace('.', '/')+".class")` works with inner classes and even with bootstrap classes having a `null` ClassLoader. – Holger Nov 07 '13 at 14:51
1

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!