Any Java Bytecode Generation Guide? [closed]

你离开我真会死。 提交于 2019-11-29 11:11:52

Someone has already written a widely-used byte code generation library: CGLIB.

You'd have it knocked if you could figure out how to get your AST into CGLIB.

Uko

Actually there is an example file inside ASM folder that you download. It's called Helloworld and it's located in examples subfolder. It shows how to compile (generate from scratch) .class file that corresponds to simple hello world app. It also shows how to get date from .class files but it's another story.

Maybe this is'n the best way, but when you need to start with java byte code generation and you need some basic examples it's a good idea to have a look at ASM and the examples that are bundled within standard package.

Moreover Groovy uses ASM to generate its code :)

I don't know if you are aware, but there is a backend for the FPC that generates bytecode compliant to the JDK 1.5. The development looks fairly recent (November 2011). You should have a look at it.

There are a couple of widely-used bytecode generation projects.

ASM and CGLib are probably the two best examples.

You probably don't want to build a generation library for yourself from scratch - it's a lot of work, difficult to get right and probably doesn't offer you much over using an existing project.

ASM is widely used by non-Java languages on the JVM, has OK-ish documentation and is not too bad to get going.

I haven't used CGLib as much, but I didn't find it as easy to get started with.

As a final data point, the Java 8 team are prototyping some of the new Java features (including lambda expressions) with ASM.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!