有高级字节码编辑器吗?
假设我在 Scala 中有以下内容
object Foo {
var functions: List[String => String] = Nil // can be any type to any type.
def addFunc(f:String => String) = functions = f :: functions
}
,在运行时,我得到了 Foo
并添加了一些函数。我现在想要构造一个新的 .class 文件,在 Scala 中实现如下所示的内容:
object MyObject {
def process1(s:String) = // call Foo.functions(1)
}
然后我想将 MyObject
保存在字节码中,即使 Foo
不存在,也可以稍后执行那里。
上面只是一个例子来展示我想要做什么。我的名称为 MyObject
、process1
,并且我必须生成一个可执行文件 MyObject.class
。不需要 MyObject
的源代码(它很可能是 Java 源代码)。
因此,在较高层面上,我们需要拍摄 Foo.function(1)
的内存“快照”,将该快照转换为字节码进行存储,并生成 MyObject
的字节码使用这个。
我发现的所有字节码工程库都太低级,所以我想知道是否有一个更高级别的库可以让我处理抽象对象,例如函数等。
Suppose I have a the following in Scala
object Foo {
var functions: List[String => String] = Nil // can be any type to any type.
def addFunc(f:String => String) = functions = f :: functions
}
At runtime, I am given Foo
with some functions added. I now want to construct a new .class file implementing something like following in Scala:
object MyObject {
def process1(s:String) = // call Foo.functions(1)
}
I then want to save MyObject
in bytecode that can be executed later on even when Foo
is not there.
The above is just an example to show what I want to do . I am given the names MyObject
, process1
, and I have to generate an executable file MyObject.class
. The source of MyObject
is not needed (it could well have been Java source).
So, at a high level, we need to take memory "snapshot" of Foo.function(1)
, convert that snapshot into bytecode to store, and generate bytecode of MyObject
using this.
All the bytecode engineering libraries I found are too low-level, so I was wondering if there is a higher level library that lets me deal with abstract objects such as functions etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你看过ASM的树模型吗?我以前只使用过事件模型,但树听起来正是您正在寻找的。您可以在 ASM 用户指南(PDF——我认为没有 HTML 版本,否则我会链接它)。
Have you looked at the Tree model of ASM? I've only used the Event model before, but the Tree sounds like just what you're looking for. You'll find an overview in section 1.2.2 of the ASM user guide (a PDF--I don't think there's an HTML version, or I'd link that).
我还会推荐 ASM 框架。 AOSD'07 有一篇关于使用 ASM 实现常见字节码转换模式的论文。 “将两个类合并为一个”和“内联方法”部分描述的字节码转换与您的非常接近。
I will also recommend ASM framework. There is a paper from AOSD'07 about implementing common bytecode transformation patterns with ASM. Sections "Merging Two Classes into One" and "Inline Method" describing bytecode transformations very close to yours.