An Approach to Internal Domain-Specific Languages in Java

Posted by Alex Ruiz and Jeff Bay on Feb 19, 2008 |


A domain-specific language (DSL) is commonly described as a computer language targeted at a particular kind of problem and it is not planned to solve problems outside of its domain. DSLs have been formally studied for many years. Until recently, however, internal DSLs have been written into programs only as a happy accident by programmers simply trying to solve their problems in the most readable and concise way possible. Lately, with the advent of Ruby and other dynamic languages, there has been a growing interest in DSLs amongst programmers. These loosely structured languages offer an approach to DSLs which allow a minimum of grammar and therefore the most direct representation of a particular language. However, discarding the compiler and the ability to use the most powerful modern IDEs such as Eclipse is a definite disadvantage with this approach. The authors have successfully compromised between the two approaches, and will argue that is quite possible and helpful to approach API design from the DSL orientation in a structured language such as Java. This article describes how it is possible to write domain-specific languages using the Java language and suggests some patterns for constructing them.

Is Java suited for creation of internal Domain-Specific Languages?

Before we examine the Java language as a tool for creation of DSLs we need to introduce the concept of "internal DSLs." An internal DSL is created with the main language of an application without requiring the creation (and maintenance) of custom compilers and interpreters. Martin Fowler has written extensively on the various types of DSL, internal and external, as well as some nice examples of each. Creating a DSL in a language like Java, however, he only addresses in passing.

It is important to note as well that it is difficult to differentiate between a DSL and an API. In the case of internal DSLs, they are essentially the same. When thinking in terms of DSL, we exploit the host language to create a readable API with a limited scope. "Internal DSL" is more or less a fancy name for an API that has been created thinking in terms of readability and focusing on a particular problem of a specific domain.

Any internal DSL is limited to the syntax and structure of its base language. In the case of Java, the obligatory use of curly braces, parenthesis and semicolons, and the lack of closures and meta-programming may lead to a DSL that is more verbose that one created with a dynamic language.

On the bright side, by using the Java language we can take advantage of powerful and mature IDEs like Eclipse and IntelliJ IDEA, which can make creation, usage and maintenance of DSLs easier thanks to features like "auto-complete," automatic refactoring and debugging. In addition, new language features in Java 5 (generics, varargs and static imports) can help us create a more compact API than previous versions of the language.

In general, a DSL written in Java will not lead to a language that a business user can create from scratch. It will lead to a language that is quite readable by a business user, as well as being very intuitive to read and write from the perspective of the programmer. It has the advantage over an external DSL or a DSL written in a dynamic language that the compiler can enforce correctness along the way, and flag inappropriate usage where Ruby or Pearl would happily accept nonsensical input and fail at run-time. This reduces the verbosity of testing substantially and can dramatically improve application quality. Using the compiler to improve quality in this way is an art however, and currently, many programmers are bemoaning the "hard work" of satisfying the compiler instead of using it to build a language that uses syntax to enforce semantics.

There are advantages and disadvantages to using Java for creation of DSLs. In the end, your business needs and the environment you work in will determine whether it is the right choice for you.

Java as a platform for internal DSLs

Dynamically constructing SQL is a great example where building a "DSL"appropriate to the domain of SQL is a compelling advantage.

Traditional Java code that uses SQL would look something like the following:

String sql = "select id, name " +
"from customers c, order o " +
"where " +
"c.since >= sysdate - 30 and " +
"sum( > " + significantTotal + " and " +
" = o.customer_id and " +
"nvl(c.status, 'DROPPED') != 'DROPPED'";

An alternative representation taken from a recent system worked on by the authors:

Table c = CUSTOMER.alias();
Table o = ORDER.alias();
Clause recent = c.SINCE.laterThan(daysEarlier(30));
Clause hasSignificantOrders = o.TOTAT.sum().isAbove(significantTotal);
Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);
Clause activeCustomer = c.STATUS.isNotNullOr("DROPPED");
String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)
.select(c.ID, c.NAME)

The DSL version has several advantages. The latter version was able to accommodate a switch to using PreparedStatements transparently - the String version requires extensive modification to switch to using bind variables. The latter will not compile if the quoting is incorrect or an integer parameter is passed to a date column for comparison. The phrase "nvl(foo, 'X') != 'X'" is a specific form found in Oracle SQL. It is virtually unreadable to a non-Oracle SQL programmer or anyone unfamiliar with SQL. That idiom in SQL Server, for example, would be "(foo is null or foo != 'X')." By replacing this phrase with the more easily understandable and language-like "isNotNullOr(rejectedValue)," readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.

Creating internal DSLs in Java

The best way to create a DSL is by first prototyping the desired API and then work on implementing it given the constraints of the base language. Implementation of a DSL will involve testing continuously to ensure that we are advancing in the right direction. This "prototype and test" approach is what Test-Driven Development (TDD) advocates.

When using Java to create a DSL, we might want to create the DSL through a fluent interface. A fluent interface provides a compact and yet easy-to-read representation of the domain problem we want to model. Fluent interfaces are implemented using method chaining. It is important to note that method chaining by itself is not enough to create a DSL. A good example is Java's StringBuilder which method "append" always return an instance of the same StringBuilder. Here is an example:

StringBuilder b = new StringBuilder();
b.append("Hello. My name is ")
.append(" and my age is ")

This example does not solve any domain-specific domain.

In addition to method chaining, static factory methods and imports are a great aid in creating a compact, yet readable DSL. We will cover these techniques in more detail in the following sections.

1. Method Chaining

There are two approaches to create a DSL using method chaining, and both are related to the return value of the methods in the chain. Our options are to return this or to return an intermediate object, depending on what we are trying to do.

1.1 Returning this

We usually return this when calls to the methods in the chain can be:

  • optional
  • called in any order
  • called any number of times

We have found two use cases for this approach:

  1. chaining of related object behavior
  2. simple construction/configuration of an object

1.1.1 Chaining related object behavior

Many times, we only want to chain methods of an object to reduce unnecessary text in our code, by simulating dispatch of "multiple messages" (or multiple method calls) to the same object. The following code listing shows an API used to test Swing GUIs. The test verifies that an error message is displayed if a user tries to log into a system without entering her password.

DialogFixture dialog = new DialogFixture(new LoginDialog());;
TextComponentFixture usernameTextBox = dialog.textBox("username");
OptionPaneFixture errorDialog = dialog.optionPane();
errorDialog.requireMessage("Enter your password");

Although the code is easy to read, it is verbose and requires too much typing.

The following are two methods from TextComponentFixture that were used in our example:

public void clear() {

public void enterText(String text) {
robot.enterText(target, text);

We can simplify our testing API by simply returning this, and therefore enable method chaining:

public TextComponentFixture clear() {
return this;

public TextComponentFixture enterText(String text) {
robot.enterText(target, text);
return this;

After enabling method chaining in all the test fixtures, our testing code is now reduced to:

dialog.optionPane().requireError().requireMessage("Enter your password");

The result is more compact and readable code. As previously mentioned, method chaining by itself does not imply having a DSL. We need to chain methods that correspond to related behavior of an object that together solve a domain-specific problem. In our example, the domain-specific problem was Swing GUI testing.

1.1.2 Simple construction/configuration of an object

This case is similar to the previous one, with the difference that instead of just chaining related methods of an object, we create a "builder" to create and/or configure objects using a fluent interface.

The following example illustrates a "dream car" created using setters:

DreamCar car = new DreamCar();

The code for the DreamCar class is pretty simple:

// package declaration and imports

public class DreamCar {

private Color color;
private String brand;
private boolean leatherSeats;
private boolean fuelEfficient;
private int passengerCount = 2;

// getters and setters for each field

Although creating a DreamCar is easy and the code is quite readable, we can create more compact code using a car builder:

// package declaration and imports

public class DreamCarBuilder {

public static DreamCarBuilder car() {
return new DreamCarBuilder();

private final DreamCar car;

private DreamCarBuilder() {
car = new DreamCar();

public DreamCar build() { return car; }

public DreamCarBuilder brand(String brand) {
return this;

public DreamCarBuilder fuelEfficient() {
return this;

// similar methods to set field values

Using the builder we can rewrite the DreamCar creation as follows:

DreamCar car = car().brand("Tesla")

Using a fluent interface, once again, reduced noise in code, which resulted in more readable code. It is imperative to note that, when returning this, any method in the chain can be called at any time and any number of times. In our example, we can call the method color as many times as we wish, and each call will override the value set by the previous call, which in the context of the application, may be valid.

Another important observation is that there is no compiler checking to enforce required field values. A possible solution would be to throw exceptions at run-time if any object creation and/or configuration rule is violated (e.g. a required field missing). It is possible to achieve rule validation by returning intermediate objects from the methods in the chain.

1.2 Returning an intermediate object

Returning an intermediate object from methods in a fluent interface has some advantages over returning this:

  • we can use the compiler to enforce business rules (e.g. required fields)
  • we can guide our users of the fluent interface through a specific path by limiting the available options for the next element in the chain
  • gives API creators greater control of which methods a user can (or must) call, as well as the order and how many times a user of the API can call a method

The following example illustrates a vacation created using constructor arguments:

Vacation vacation = new Vacation("10/09/2007", "10/17/2007",
"Paris", "Hilton",
"United", "UA-6886");

The benefit of this approach is that it forces our users to specify all required parameters. Unfortunately, there are too many parameters and they do not communicate their purpose. Do "Paris" and "Hilton" refer to the city and hotel of destination? Or do they refer to the name of our companion? :)

A second approach is to use setters as a way to document each parameter:

Vacation vacation = new Vacation();

Our code is more readable now, but it is also verbose. A third approach could be to create a fluent interface to build a vacation, like in the example in the previous section:

Vacation vacation = vacation().starting("10/09/2007")

This version is more compact and readable, but we have lost the compiler checks for missing fields that we had in the first version (the one using a constructor.) In another words, we are not exploiting the compiler to check for possible mistakes. At this point, the best we can do with this approach is to throw exceptions at run-time if any of the required fields was not set.

The following is a fourth, more sophisticated version of the fluent interface. This time, methods return intermediate objects instead of this:

Period vacation = from("10/09/2007").to("10/17/2007");
Booking booking ="Paris").hotel("Hilton"));

Here we have introduced the concept of Period, a Booking, as well as a Location and BookableItem (Hotel and Flight), and an Airline. The airline, in this context, is acting as a factory for Flight objects; the Location is acting as a factory for Hotel items, etc. Each of these objects is implied by the booking syntax we desired, but will almost certainly grow to have many other important behaviors in the system as well. The use of intermediate objects allows us to introduce compiler-checked constraints of what the user can and cannot do. For example, if a user of the API tries to book a vacation with a starting date and without an ending date, the code will simply not compile. As we mentioned previously, we can build a language that uses syntax to enforce semantics.

We have also introduced the usage of static factory methods in the previous example. Static factory methods, when used with static imports, can help us create more compact fluent interfaces. For example, without static imports, the previous example will need to be coded like this:

Period vacation = Period.from("10/09/2007").to("10/17/2007");

The example above is not as readable as the one using static imports. We will cover static factory methods and imports in more detail in the following section.

Here is a second example of a DSL in Java. This time, we are simplifying usage of Java reflection:

Person person = constructor().withParameterTypes(String.class)



We need to be cautious when using method chaining. It is quite easy to overuse, resulting in "train wrecks" of many calls chained together in a single line. This can lead to many problems, including a significant reduction in readability and vagueness in a stack trace when exceptions arise.

2. Static Factory Methods and Imports

Static factory methods and imports can make an API more compact and easier to read. We have found that static factory methods are a convenient way to simulate named parameters in Java, a feature that many developers wish the language had. For example, consider this code, which purpose is to test a GUI by simulating a user selecting a row in a JTable:

dialog.table("results").selectCell(6, 8); // row 6, column 8 

Without the comment "// row 6, column 8," it would be easy to misunderstand (or not understand at all) what the purpose of this code is. We would need to spend some extra time checking documentation or reading some more lines of code to understand what '6' and '8' stand for. We could also declare the row and column indices as variables or better yet, as constants:

int row = 6;
int column = 8;
dialog.table("results").selectCell(row, column);

We have improved readability of code, at the expense of adding more code to maintain. To keep code as compact as possible, the ideal solution would to write something like this:

dialog.table("results").selectCell(row: 6, column: 8); 

Unfortunately, we cannot do that because Java does not support named parameters. On the bright side, we can simulate them by using a static factory method and static imports, to get something like:


We can start by changing the signature of the method, by replacing all the parameters with one object that will contain them. In our example, we can change the signature of selectCell(int, int) to:


TableCell will contain the values for the row and column indices:

public final class TableCell {

public final int row;
public final int column;

public TableCell(int row, int column) {
this.row = row;
this.column = column;

At this point, we just have moved the problem around: TableCell's constructor is still taking two int values. The next step is to introduce a factory of TableCells, which will have one method per parameter in the original version of selectCell. In addition, to force users to use the factory, we need to change TableCell's constructor to private:

public final class TableCell {

public static class TableCellBuilder {
private final int row;

public TableCellBuilder(int row) {
this.row = row;

public TableCell column(int column) {
return new TableCell(row, column);

public final int row;
public final int column;

private TableCell(int row, int column) {
this.row = row;
this.column = column;

By having the factory TableCellBuilder we can create a TableCell having one parameter per method call. Each method in the factory communicates the purpose of its parameter:

selectCell(new TableCellBuilder(6).column(8)); 

The last step is to introduce a static factory method to replace usage of TableCellBuilder constructor, which is not communicating what 6 stands for. As we did previously, we need to make the constructor private to force our users to use the factory method:

public final class TableCell {

public static class TableCellBuilder {
public static TableCellBuilder row(int row) {
return new TableCellBuilder(row);

private final int row;

private TableCellBuilder(int row) {
this.row = row;

private TableCell column(int column) {
return new TableCell(row, column);

public final int row;
public final int column;

private TableCell(int row, int column) {
this.row = row;
this.column = column;

Now we only need to add to our code calling selectCell is include an static import for the method row in TableCellBuilder. To refresh our memories, this is how calls to selectCell look like:


Our example shows that with some little extra work we can overcome some of the limitations of our host language. As we mentioned before, this is only one of the multiple ways we can improve code readability using static factory methods and imports. The following code listing shows an alternative way to solve the same problem of table indices, using a static factory methods and imports in a different way:

* @author Mark Alexandre
public final class TableCellIndex {

public static final class RowIndex {
final int row;
RowIndex(int row) {
this.row = row;

public static final class ColumnIndex {
final int column;
ColumnIndex(int column) {
this.column = column;

public final int row;
public final int column;
private TableCellIndex(RowIndex rowIndex, ColumnIndex columnIndex) {
this.row = rowIndex.row;
this.column = columnIndex.column;

public static TableCellIndex cellAt(RowIndex row, ColumnIndex column) {
return new TableCellIndex(row, column);

public static TableCellIndex cellAt(ColumnIndex column, RowIndex row) {
return new TableCellIndex(row, column);

public static RowIndex row(int index) {
return new RowIndex(index);

public static ColumnIndex column(int index) {
return new ColumnIndex(index);

The second version of the solution is more flexible than the first one, because allows us to specify the row and column indices in two ways:

dialog.table("results").select(cellAt(row(6), column(8));
dialog.table("results").select(cellAt(column(3), row(5));

Organizing Code

It is a lot easier to organize code of a fluent interface which methods return this, than the one which methods return intermediate objects. In the case of the former, we end up with fewer classes that encapsulate the logic of the fluent interface, allowing us to use the same rules or conventions we use when organizing non-DSL code.

Organizing code of a fluent interface using intermediate objects as return type is trickier because we have the logic of the fluent interface scattered across several small classes. Since these classes, together, as a whole, form our fluent interface, it makes sense to keep them together and we might not want them to mix them with classes outside of the DSL. We have found two options:

  • Create intermediate objects as inner classes
  • Have intermediate objects in their own top-level classes, all in the same package

The decision of the approach to use to decompose our system can depend on several factors the syntax we want to achieve, the purpose of the DSL, the number and size (in lines of code) of intermediate objects (if any,) and how the DSL can fit with the rest of the code base, as well as any other DSLs.

Documenting Code

As in organizing code, documenting a fluent interface which methods return this is a lot easier than documenting a fluent interface returning intermediate objects, especially if documenting using Javadoc.

Javadoc displays documentation of one class at a time, which may not be the best in a DSL using intermediate objects: the DSL is composed of a group of classes, not individual ones. Since we cannot change how Javadoc displays the documentation of our APIs, we have found that having an example usage of the fluent interface (including all the participating classes) with links to each of the methods in the chain, in the package.html file, can minimize the limitations of Javadoc.

We should be careful and not duplicate documentation, because it will increase maintenance costs for the API creators. The best approach is to rely on tests as executable documentation as much as possible.

In Conclusion

Java can be suited to create internal domain-specific languages that developers can find very intuitive to read and write, and still be quite readable by business users. DSLs created in Java may be more verbose than the ones created with dynamic languages. On the bright side, by using Java we can exploit the compiler to enforce semantics of a DSL. In addition we can count on mature and powerful Java IDEs that can make creation, usage and maintenance of DSLs a lot easier.

Creating DSLs in Java also requires more work from API designers. There is more code and more documentation to create and maintain. The results can be rewarding though. Users of our APIs will see improvements in their code bases. Their code will be more compact and easier to maintain, which can simplify their lives.

There are many different ways to create DSLs in Java, depending on what we are trying to accomplish. Although there is no "one size fits all" approach, we have found that combining method chaining and static factory methods and imports can lead to a clean, compact API that is both easy to write and read.

In summary, there are advantages and disadvantages when using Java for creation of DSLs. It is up to us, the developers, to decide whether is the right choice based on the needs of our projects.

As a side note, Java 7 may include new language features (such as closures) that may help us create less verbose DSLs. For a comprehensive list of the proposed features, please visit Alex Miller's blog.

About the Authors

Alex Ruiz is a Software Engineer in the development tools organization at Oracle. Alex enjoys reading anything related to Java, testing, OOP, and AOP and has programming as his first love. Before joining Oracle, Alex was a consultant for ThoughtWorks. Alex maintains a blog at

Jeff Bay is a Senior Software Engineer at a hedge fund in New York. He has repeatedly built high quality, high velocity XP teams working on diverse systems such as program enrollment for Onstar, leasing software, web servers, construction project management, and others. He approaches software with a passion for removing duplication and preventing bugs in order to improve developer efficiency and time on task.


Rate this Article


Hello stranger!


Get the most out of the InfoQ experience.

Tell us what you think


Email me replies to any of my messages in this thread

Brilliant article by Henri Frilund

Very interesting article. I'm right now working on a personal project trying to create a tool for creating web applications with an internal Java-hosted DSL, backed by a component model (Wheel).

Designing the API is indeed quite difficult especially if the DSL is rather large. Worst problems for me so far have been limitations posed by the static type system (which on the other hand helps a lot when using the DSL with a modern IDE), method grouping and not having named attributes.

Thanks to some of the ideas in your article, I think I can get around the missing named attributes.

The which I mean the situation where you start to have dozens of methods (DSL expressions) in a class and you need some way to group them functionally, but can't break the class-hierarchy either (in my case the DSL is used by extending a a class containing the DSL so I can't split the class itself). I ended up creating sort of "side-show" classes that hold the methods, but share state with the "main" class. Example: A Configuration-class that holds method for manipulating the "main" classes configuration properties. The "main" class then as a method that returns the Configuration-object.

In code it looks like this:

initialFieldValue("guess", "give a number").

What the config() does is that is returns the Configuration-object with methods that will return this. It's not very OO, but it works and the result looks pleasing.

Not sure if the grouping-issue is common, but thought I'd mention it.


A Pointless Exercise by Erkki Lindpere

Creating internal DSL-s in Java is a pointless exercise. Scala, Groovy, JRuby or another DSL-friendly language should be used instead. My personal preference is Scala. It has limitations on how nice the syntax can be, but it performs as well as Java, unlike the dynamically typed languages.

Very good read. by Richard L. Burton III

I know a lot of developers are often reluctant to follow a DSL naming convention like expressed in the examples contained within this article. I've recently become a firm believer in method chaining and following what I like to call "DSL Naming Convention".

Good job guys.

Best Regards,
Richard L. Burton III

Good stuff by Manuel Palacio

Very good article. Looking forward to seeing more of the SQL example.

Re: SQL-Example by Michael Hunger

Perhaps you'd like to have a look at the embedded Java-SQL DSL named JEQUEL.

See someone is interested to join we also have a codehaus project at

Small example:

public void testSimpleSql() {
final SqlString sql =

assertEquals("select ARTICLE.OID" +
" and ARTICLE.ARTICLE_NO is not NULL", sql.toString());

For some more material on DSLs have a look at the

DSL-Book (Work in Progress)
and the

thoughtworks podcast

by Martin Fowler.
Have fun


Additional resources by Michael Hunger

Nat Pryce and Steve Freeman did a lot of this java dsl stuff during the jMock and Hamcrest development. Martin Fowler links to the paper regarding jmock at the end of the
expression builder bliki entry.

The blog of Nat Pryce is an invaluable source regarding java DSLs in the domain of testing (i.e. jmock, test data builders, etc.)

There is an ongoing effort of the Quaere Team led by Anders Norås to build a LINQ like DSL for java.


Re: Additional resources by Michael Hunger

Sorry, I forgot: Guice binding setup is also an internal DSL in java.

The DSL booking example is totally confusing. You have inconsistent syntax mixed with imperative methods (add) and I doubt that a user of such an DSL would know which methods to call on which objects and which Parameter objects to fill in created by the (he has to know this) statically imported methods. There is also a missing paren at the last line (in both boxes).

Something more interesting would be:

Trip trip =

What I also miss is the discussion of the concrete usage of interfaces as intermediary builder objects. This is much more important as it is java's only way to support multiple inheritance (and you need this when composing grammars from reusable expressions). See Martins Book for details.


Very Beneficial by Jacob Northey

Great article, I'm sure we will see some if not all of this content in an upcoming DSL book.

Although I would never use an internal DSL in Java as a customer facing DSL, using the builder pattern and method chaining is extremely beneficial for generating test objects in unit tests. It is much more communicative than a simple ObjectMother which hides a lot of the test values inside static factory methods.

Somebody should create a code generation library for generating builders from POJOs. That would reduce a lot of time spent keeping builder code current with the POJOs.

Re: Very Beneficial by Michael Hunger

The book by Martin Fowler is as cited already in progress.
Nat Pryce did quite a lot with the TestDataBuilders you mention.

I agree that an internal DSL in a language like Java is nothing for customers (neither reading nor writing) although reading may be improved by changing the color scheme of the IDE to fade parentheses, semicolons, brackets, language identifiers as suggested by Nat Pryce, Anders Noras and others.

The generic pojo builder generation thing would only work if you could specify the relevant attributes that shall be a part of the language and also define the associations between your POJOs. Choosing the right names for the fluent interfaces (which often don't correspond to the names of the attributes set but are more natural language constructs) is another problem with a generic approach.


Re: A Pointless Exercise by Michael Hunger

I don't agree with you. Please don't forget that there is not only the top notch of the developer crowd you see here at infoq, the other online resources and the conferences. If I look around my workplace there is a lot of people who just do java and have neither heard nor used Scala, Ruby, Groovy and the like. So a DSL in Java is a first step to take in this direction when introducing DSLs and fluent interfaces in an development team/organisation.

The static typing helps a lot when someone learns a new DSL (if you are fluent in your DSL a dynamic language should pose no problem but I don't want to imagine having a customer staring at the error messages of the ruby or groovy runtime due to misspelled syntax).

So editor support is quite important here. You could argue that a tool like the xText facilities of openArchitectureWare ( would be a better choice their with their full fledged text editor generation support. But they don't generate DSLs for executable languages per se but parsers for creating/reading models.


Very nice (but is it a DSL?) by Ron Kersic

Nice article!

But I’d argue that it is more on fluent interfaces then on (internal) DSL’s. I know the issue of is-it-a-DSL-or-not is a bit of a grey area but my personal litmus test is on the errors and warnings thrown at me when creating a program with the DSL. I expect a DSL to be supported by errors and warnings at the level of the DSL and not at the level of the language implementing the DSL.

In case of the of the booking example, I’d guess that if I type booking.add(airline(“united”)) the error would be that the booking.add method expects a BookableItem (instead of an Airline). But for a ‘real’ DSL I want the error message to state that I need to specify a flight to go with that airline. Likewise, if I’d type booking.add(flight(“UA-6868”)) I want a warning message stating that this flight is interpreted to be by United Airlines and I want to be offered a quick fix to add the airline(“united”) bit.

I guess it comes down to the fact that you need to have some processing on top of what the IDE does on processing Java in order to get at a internal DSL in Java. The Spoon framework ( is a relatively easy way of getting such processing in place. I’m building an internal DSL with Spoon (, no code yet alas) and from what I know, the errors and warnings for the booking example above would be very easy to implement with Spoon. Most certainly worth checking out.


Re: Very Beneficial by Jacob Northey

Thanks for pointing out Nat's blog. The entry is exactly what I was looking for in terms of using TestDataBuilders instead of an ObjectMother.

As for the generic pojo builder generation, I would be willing to give up a little readability for a lot less effort. This is especially the case for large generated domain models on small projects where the effort required to maintain TestDataBuilders is not warranted. The main goal I'm looking for in TestDataBuilders is conciseness.

public class Car {
private Color color;
private Model model;
private Manufacturer manufacturer;
// Getters and Setters

Car car = car().color(RED).manufacturer(manufacturer("Ford")).model(model("Fusion")).build();

The above code, similar to the article's dreamCar code, can be generated straight from POJOs where the static builderfactory methods manufacturer and model reflect required constructor parameters. Another approach I like is using variable length argument lists with named parameters instead of chaining with() calls:

Car redFusion = car(color("red"), manufacturer("Ford"), model("Fusion"));
Car blueIon = car(year(2005), color("blue"), manufacturer("Saturn"), model("Ion"));

A Great Article by John DeHope

A fantastic article. I really enjoyed the discussion about how to actually build a fluent API. You see a lot of blog articles about what they are, and what they look like. This was the first article I read that explained some of the thought process that goes into actually building them up from scratch. Nice!

Another nice example of an internal DSL for integration by David Greco

Camel defines an internal DSL based on the enterprise integration patterns. Using this nice DSL it's possible to easily define messaging routes among different endpoints.

A dsl for collection manipulation by Mario Fusco

Another good example of DSL is the one provided by lambdaj ( ) in order to manipulate collection without poorly readable loops.

Fluent builder generator by Jakub Janczak

we've created a generator that can help DSL'ize your process of javabeans creating:

hope you like it

Nice article - a few nitpicks . . . by Peter Bell

Nice article guys! As you pointed out, IDE support (code completion) is definitely a benefit when creating internal DSLs in Java. I also agree that compile time correctness checking is a benefit of internal DSLs in static rather than dynamic languages. However you can get the same benefits in external DSLs. Whether you're using an XML concrete syntax and using a schema to validate or using tooling like Xtext (part of the Eclipse Modeling Framework now - used to be part of openArchitectureWare) you can also get such compile time validations. Of course you get it much more quickly/cheaply in Java, but it's the speed with which you get it that differentiates a Java internal DSL from an external DSL.

I'd also argue against Java being good for end user readability. I find consistently that business users are distracted by the syntactic noise. I think Java can be an option for creating an internal technical DSL (for example Braintree Payment systems used a build based Java DSL to wrap the API for their payment processing service) but would suggest alternate approaches if readability by non-technical users is a key concern.

wonky facts alert by Kevin Wright

You state in both the article and in comment replies that only dynamic languages can offer a cleaner DSL. Implying that the verbosity of your approach is inherent to static typing.

Scala, mirah and Haskell would all like to respectfully disagree... Scala is most definitely *not* dynamically typed.

I also think you'll find that your favourite IDE features, such as code completion, are also well supported for Scala now, so it's unreasonable to claim this as another unique advantage for Java.

Re: Good stuff by Lukas Eder

I have recently published an article about my new DSL called jOOQ (for Java Object Oriented Querying). Here is the full article on dzone:

As a teaser, please consider how you can express this SQL:

-- Select authors with books that are sold out

in jOOQ's fluent DSL:

jOOQ is specifically designed for a developer who:

  • interfaces Java with huge legacy databases.

  • knows SQL well and wants to use it extensively, but not with JDBC.

  • doesn't want to learn any new language (HQL, JPQL, etc)

  • doesn't want to spend one minute fine-tuning some sophisticated XML-configuration.

  • wants little abstraction over SQL, because his software is tightly coupled with his database. Something that I think the guys at Hibernate or JPA seem to have ignored.

  • needs a strong but light-weight library for database access. For instance to develop for mobile devices.

Read the full article on dzone:

Or visit the project homepage directly:


Email me replies to any of my messages in this thread


Email me replies to any of my messages in this thread

19 Discuss
General Feedback
Privacy policy