Which library/program can be used to generate Java

2020-02-17 12:03发布

I know about BCEL, but this project seems to be dead, as it had no releases for two years. And the Java-world moves on. For example JDK 1.6 has a new class-file-format.

So what library can be used to create bytecode for the JVM. If no library, a program is ok too, if I can manipulate the generated code in detail, for example a bytecode-assembler.

Which software can you recommend? Is it easy too use? has good examples/tutorials?

EDIT: For all asking: Yes, the javac is fine. But for generating some classes at runtime, a path directly to bytecode would be cleaner.

7条回答
Viruses.
2楼-- · 2020-02-17 12:39

ASM

http://asm.objectweb.org/

It is much faster than BCEL and supports generics and annotations. One point about its architecture: in order to ensure high performance ASM is built around a parser that throws events (in contrast to BCEL where the parser builds a data structure). This is somewhat similar to the difference between SAX and DOM parsers. It takes some practice to get used to this kind of thinking.

EDIT (Following McDowell's comment): Indeed visitors are heavily used in ASM, but it's more than plain visitors: the visited data structure is lazily built by the parser, so if you're not interested in certain parts of the classfile (for example, you want to know the names of the methods but you don't care about their body), you can return a null from the visitMethod() method. This will make the parser skip the method body sections thereby preventing the (expensive) construction of the net of objects fully describing the method.

查看更多
男人必须洒脱
3楼-- · 2020-02-17 12:41

http://serp.sourceforge.net/ is a great library for more abstraction when editing the bytecode.

查看更多
做个烂人
4楼-- · 2020-02-17 12:44

Why not use the Java compiler, javac? What's wrong with using it to generate JVM byte code?

[Seriously. What stops you from taking your source, making Java and compiling it?]

查看更多
Emotional °昔
5楼-- · 2020-02-17 12:49

I think my favorite java bytecode creator is called javac and you can find it at www.sun.com

查看更多
老娘就宠你
6楼-- · 2020-02-17 12:50

There are technologies like asm and cglib but I recomend Javaassist because it is a very good library for that and you can find examples in tapestry5 framework.

查看更多
ゆ 、 Hurt°
7楼-- · 2020-02-17 13:04

There is a fairly complete example of using ASM to generate byte code from a Java-like intermediate language in the implementation of CAL (a Haskell-like language for the JVM). If you download the sources at http://openquark.org/Open_Quark/Download.html then you can find the code in AsmJavaByteCodeGenerator.java, and the java model classes in the same folder. The code generated is basically what javac would do, minus debug annotations.

The CAL implementation originally used BCEL but switched to ASM because ASM was significantly faster (probably an order of magnitude), and just as significantly, ASM is thread safe, so that concurrent compilation is possible, which is needed by CAL.

查看更多
登录 后发表回答