The Artima Developer Community
Sponsored Link

Artima Developer Spotlight Forum
Typesafe DSLs in Java

5 replies on 1 page. Most recent reply: Mar 27, 2008 3:45 PM by Guy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Jevgeni Kabanov

Posts: 9
Nickname: ekabanov
Registered: Oct, 2006

Typesafe DSLs in Java Posted: Mar 26, 2008 5:24 PM
Reply to this message Reply
Advertisement

Typed-ASM is a new open-source project on Google Code that aims to make it possible to write type-safe domain-specific languages in Java. Jevgeni Kabanov explains the project's goals and its initial codebase in a pair of blog entries, Typesafe DSLs in Java: Part 1 — Typesafe Bytecode and Typesafe ASM — problems solved?:

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.

What do you think of the Typed-ASM project's goals?


James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Typesafe DSLs in Java Posted: Mar 27, 2008 9:12 AM
Reply to this message Reply
> new ClassBuilder(cw, V1_4, ACC_PUBLIC, "HelloWorld",
> "java/lang/Object", null)
> .beginStaticMethod(ACC_PUBLIC | ACC_STATIC, "main",
> in", void.class, String[].class)
> .getStatic(System.class, "out", PrintStream.class)
> .push("Hello, World!")
> //Here String.class refers to the type of the first
> first parameter
> .invokeVirtualVoid(INVOKEVIRTUAL, PrintStream.class,
> ass, "println", String.class)
> .returnVoid()
> .endMethod();

Just to be clear, is that supposed to be an example of the DSL in use or something else?

V.H.Indukumar

Posts: 28
Nickname: vhi
Registered: Apr, 2005

Re: Typesafe DSLs in Java Posted: Mar 27, 2008 10:06 AM
Reply to this message Reply
I would prefer something like:


new ClassBuilder()
.Class("HelloWorld").Public().Extends(Object.class)
.Begin()
.Method("main").Public().Static().Final().Void().Args(String[].class)
.Begin()
.On(System.class, "out").Call(PrintStream.class, "println").With("HelloW")
.End()
.End()



But it would be a lot more effort to implement.

Note: Capitalized keywords to be able to use the same names as in Java.

Jevgeni Kabanov

Posts: 9
Nickname: ekabanov
Registered: Oct, 2006

Re: Typesafe DSLs in Java Posted: Mar 27, 2008 10:36 AM
Reply to this message Reply
> Just to be clear, is that supposed to be an example of the
> DSL in use or something else?

Yes, that's the DSL in use. Of course it is not finalized yet.

Jevgeni Kabanov

Posts: 9
Nickname: ekabanov
Registered: Oct, 2006

Re: Typesafe DSLs in Java Posted: Mar 27, 2008 10:42 AM
Reply to this message Reply
> I would prefer something like:
>
> [snip]
>
> But it would be a lot more effort to implement.
>
> Note: Capitalized keywords to be able to use the same
> names as in Java.

I don't know what exactly you're proposing, but that syntax would not be possible to make fully typesafe (you can restrict class generics in methods, that's why you need to put a class literal both in producer and consumer) and would be less expressive than Java bytecode. My goal is full expressiveness and as part of the project I'll write a disassembler a la ASMfier.

Guy

Posts: 1
Nickname: guymac
Registered: Jun, 2003

Re: Typesafe DSLs in Java Posted: Mar 27, 2008 3:45 PM
Reply to this message Reply
It seems to be an example of a DSL for some bytecode-generating compiler, where it itself is generated by interpreting some higher-level (and presumably human-readable) DSL?

Flat View: This topic has 5 replies on 1 page
Topic: Typesafe DSLs in Java Previous Topic   Next Topic Topic: John Resig on Inheritance in JavaScript

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use