有高级字节码编辑器吗?

发布于 2024-12-10 05:53:30 字数 817 浏览 0 评论 0原文

假设我在 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 不存在,也可以稍后执行那里。

上面只是一个例子来展示我想要做什么。我的名称为 MyObjectprocess1,并且我必须生成一个可执行文件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2024-12-17 05:53:30

你看过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).

行至春深 2024-12-17 05:53:30

我还会推荐 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文