In Java, DSLs are ... becoming more popular. The first examples, like jMock and Hibernate Criteria, were coined as Fluent Interface. However most of these DSLs are not typesafe and will allow wrong statements to be compiled...
The point of the [typed-asm] project is that you can write Java bytecode using a DSL-like API and [the] Java compiler will validate for you that it is, indeed, correct. This has implications for all Java developers, since you can use the same approach to write typesafe DSLs for other projects as well...
Part of the reason the project chose ASM code generation for its initial implementation is because ASM is used as the basis for a lot of higher-level Java-based DSLs:
One of the best libraries for working with Java bytecode is ASM. It provides both a lightweight, fast visitor-based interface and a more comfortable tree-based object-oriented interface. Unfortunately both of them (and especially visitor-based one) are completely untyped, and debugging the wrong bytecode created using them is a huge pain in the neck. 'Nuff to say that Java bytecode verifier will tell you there's a problem, but will not tell you where exactly it is.
What we want to do is for compiler to issue an error when the stack in fact does not contain such elements. We propose the following DSL as the basis:
new ClassBuilder(cw, V1_4, ACC_PUBLIC, "HelloWorld", "java/lang/Object", null)
.beginStaticMethod(ACC_PUBLIC | ACC_STATIC, "main", void.class, String[].class)
.getStatic(System.class, "out", PrintStream.class)
.push("Hello, World!")
//Here String.class refers to the type of the first parameter
.invokeVirtualVoid(INVOKEVIRTUAL, PrintStream.class, "println", String.class)
.returnVoid()
.endMethod();
Note that now all the types are written as class literals instead of strings. In Java 5, class literals are generified to Class<C>, where C refers to the actual underlying type. This already allows for less mistakes, since the classes are no longer written as strings...
Kabanov shows how a more type-safe ASM DSL can be achieved with the Typed-ASM library.