Java:从同一JVM中获取运行时的类的字节码 [英] Java: Getting Bytecode of Class at Runtime from within the Same JVM

查看:1023
本文介绍了Java:从同一JVM中获取运行时的类的字节码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关: Java:有一种方法可以在运行时获取类的字节码?

我要为Clojure添加持久性,我终于在我准备添加功能的点。在Clojure中,函数被字节编译成具有调用方法的类(等等)。这样,函数是第一类。为了使这些持久,我需要序列化和反序列化这些类。如何获取类的字节码,而无需访问.class文件?

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?

如果我错了,请纠正我,但使用代理需要产生一个单独的VM,代理连接到第一个VM。我需要从同一个VM。

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.

使用Serializable设置和获取Class对象是不够的。反序列化后,我需要加载类,并且在后续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.

推荐答案

我们需要修改字节码以将类重命名为在反序列化/自己 ClassLoader ,并且在加载类时记录字节码的方案。

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

code> findClass 自己找到类文件,将它加载到内存,保存数据(以后序列化),然后调用 defineClass 在JVM中定义该类。

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.

这篇关于Java:从同一JVM中获取运行时的类的字节码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆