tags:

views:

715

answers:

7

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.

A: 

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

Karl
Beat me by 28 seconds!
S.Lott
I have a feeling that Mnementh is asking about something else and we have a clash of vocabulary problem.
Karl
A: 

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

S.Lott
There are some cases where you want to programatically generate bytecode. If this is on a user's machine, you can't assume they have the full JDK installed.
James Van Huis
Having trouble imagining an application so cool that it requires the compiler, but not cool enough to include it in the download. While it's technically possible, it sounds unlikely.
S.Lott
An common application is compiling languages other than Java to Java bytecode. Scripts extending something like a game or a photo editor may need performance of compiled code. And, Java 7 is likely to include bytecode that can't be produced by a valid Java program (invokedynamic for dynamic langs).
erickson
Manipulating bytecode is essential for implementing instrumentation, AOP support, annotation-based design-by-contract frameworks, etc.
McDowell
Another application: code obfuscation
James Van Huis
Also, you run into legal issues with distributing the JDK.
James Van Huis
Other languages producing bytecode -- like Jython -- use the Jython compiler. Obfuscation of the class file -- don't get it. Legal issues -- Sun grants a license to distribute the JDK. AOP -- use decorators. Not seeing the need to bypass the compiler yet.
S.Lott
+10  A: 

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.

Itay
I think that by "throws events", Itay means it makes use of the visitor pattern. I'm trying ASM at the moment - it is pretty good and has good doc. If you want annotation support in BCEL, you need to build the latest sources.
McDowell
If you don't require fast performance, ASM also provides a DOM-like tree based interface.
Dave L.
+2  A: 

Javassist and cglib are two good code engineering libraries. They are used extensively in the j2ee world for generating proxies of objects and byte code engineering libraries at run time. Hibernate and Spring are two leading frameworks implementing these libraries.

Varun Mehta
+1  A: 

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

iberck
+3  A: 

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.

+1  A: 

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

Jeremybub