Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Javassist and cglib are two good bytecode engineering libraries. They are used extensively in the Java EE world for generating proxies of objects at runtime. Hibernate and Spring are two leading frameworks making use of these libraries.

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

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.

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

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?]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top