生成“你好,世界!”带有 Java ASM 库的类
我已经开始为我正在从事的编译器项目摆弄 ASM API。然而,我发现文档在很多地方对于新手来说不太清楚,我认为有一个很好的例子来生成一个简单地打印“Hello, World!”的类。将是一个很好的例子。
目前,我可以使用 main() 生成一个类(使用 ClassWriter、ClassVisitor 和 MethodVisitor 类),但我似乎不知道如何生成 main 的主体。谁能给我一个在 ASM 中生成类文件的示例,该文件:
- 包含 main()
- 在 main() 中创建一个值为“Hello, World!”的本地 String 变量
- 打印变量
I have started messing around with the ASM API for a compiler project I am working on. However, I am finding that the documentation is less than clear for a newcomer in many places and I thought having a good solid example of generating a class that simply prints "Hello, World!" would be a great example to have on here.
Currently, I can generate a class with a main() (using the ClassWriter, ClassVisitor and MethodVisitor classes) but I can't seem to work out how to generate main's body. Could anyone give me an example of generating a class file in ASM that:
- contains a main()
- creates a local String variable in main() with the value "Hello, World!"
- prints the variable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 java 编译一个类,然后让 asm 打印出生成等效类所需的调用,
常见问题解答
ASMifierClassVisitor< /a>
ASMifierClassVisitor javadocs 实际上包含 hello world 代码,
You can compile a class using java, then get asm to print out the calls it would take to generate an equivalent class,
FAQ
ASMifierClassVisitor
The ASMifierClassVisitor javadocs actually has the hello world code in it,
如果您使用 Eclipse,有一个很棒的 ASM 插件 可以帮助您学习。它将现有的 Java 代码显示为检测所述代码所需的实际 ASM 调用。它对于学习非常有用,因为您可以看到实现特定 Java 代码所需的 ASM 调用。
If you are using Eclipse, there is a great ASM plugin that will aid your learning. It displays existing Java code as the actual ASM calls needed to instrument said code. It is quite usefully for learning as you can see the ASM calls needed to implement specific Java code.