16 January 2011

Tags: asm ast bytecode groovy programming transformation

Following my post about Fibonacci performance in Groovy, Guillaume Laforge gently asked me to continue my work, so I’m releasing the first version of a Groovy AST transformation which allows you to write a method body directly as bytecode. There are several usages for this, but I think the most important is educational : it’s one of the simplest (if not the simplest) way of testing JVM bytecode.

Here’s an example of what you may write :

@groovyx.ast.bytecode.Bytecode
int fib(int i) {
    iload 1
    iconst_2
    if_icmpge l1
    iload 1
    _goto l2
   l1
    aload 0
    iload 1
    iconst_2
    isub
    invokevirtual '.fib', '(I)I'
    aload 0
    iload 1
    iconst_1
    isub
    invokevirtual '.fib' ,'(I)I'
    iadd
   l2
    ireturn
}
println fib(40)

The documentation about how to write bytecode and the supported features are on the project homepage at GitHub. The source code is licensed under the Apache License version 2. If you find any bug or have feature requests, please fill in the bug tracker.

Downloads and sources

You must use the groovy-all version of Groovy for this AST transform to work. Then, download the following jar :

Sources are built with Gradle and tests made with the Spock framework. Unit tests are also a great source of documentation, do hot hesitate to check them out.