Java DSLs
                        with Xtext
                           Jan Koehnlein




Mittwoch, 27. März 13
Mittwoch, 27. März 13
Mittwoch, 27. März 13
public class Customer implements Serializable {

                        	   private String name;
                        	   private Address address;
                        	   private List<Item> purchase;

                        	   public String getName() {
                        	   	   return name;
                        	   }
                        	   public void setName(String name) {
                        	   	   this.name = name;
                        	   }

                        	   public Address getAddress() {
                        	   	   return address;
                        	   }
                        	   public void setAddress(Address address) {
                        	   	   this.address = address;
                        	   }
                        	
                        	   public List<Item> getPurchase() {
                        	   	   return purchase;
                        	   }
                        	   public void setPurchase(List<Item> items) {
                        	   	   this.purchase = purchase;
                        	   }
                        }




Mittwoch, 27. März 13
@Entity
                        public class Customer implements Serializable {
                        	   @Id
                        	   private String name;
                        	   private Address address;
                        	   private List<Item> purchase;

                        	   public String getName() {
                        	   	   return name;
                        	   }
                        	   public void setName(String name) {
                        	   	   this.name = name;
                        	   }
                        	   @Column(name="ADDR", nullable=false)
                        	   public Address getAddress()
                        	   	   return address;
                        	   }
                        	   public void setAddress(Address address) {
                        	   	   this.address = address;
                        	   }
                        	   @ManyToMany
                        	   public List<Item> getPurchase() {
                        	   	   return purchase;
                        	   }
                        	   public void setPurchase(List<Item> items) {
                        	   	   this.purchase = purchase;
                        	   }
                        }




Mittwoch, 27. März 13
Domain-Specific Languages




Mittwoch, 27. März 13
DSL                        Java
            entity Customer {
                                 @Entity
              name: String       public class Customer implements Serializable {
                                 	   @Id
              address: Address   	   private String name;
              purchase: Item*    	
                                 	
                                     private Address address;
                                     private List<Item> purchase;
            }
                                 	   public String getName() {
                                 	   	   return name;
                                 	   }
                                 	   public void setName(String name) {
                                 	   	   this.name = name;
                                 	   }
                                 	   @Column(name="ADDR", nullable=false)


            Code                 	
                                 	
                                 	
                                 	
                                     public Address getAddress() {
                                     	
                                     }
                                         return address;

                                     public void setAddress(Address address) {



          Generation
                                 	   	   this.address = address;
                                 	   }
                                 	   @ManyToMany
                                 	   public List<Item> getPurchase() {
                                 	   	   return purchase;
                                 	   }
                                 	   public void setPurchase(List<Item> items) {
                                 	   	   this.purchase = purchase;
                                 	   }
                                 }



Mittwoch, 27. März 13
?
                         Methods
                           Logic
                        Arithmetics
                         Behavior



Mittwoch, 27. März 13
entity Customer {
                          name: String
                          address: Address
                          purchase: Item*
                          double sales() {
                            double result = 0.0;
                            for(Item item: purchase)
                              result += item.getPrice();
                            return result;
                          }
                        }




Mittwoch, 27. März 13
Expressions



                          Complex Syntax
                             Typesystem
                        Compiler / Interpreter

Mittwoch, 27. März 13
Integration Patterns
                         Manually
                         written
                           code




                            Generated
                              code



Mittwoch, 27. März 13
Reusable powerful expression
                                    library language


                        Java‘s Typesystem


                            Access to Java-elements

                                       Compile to Java Code


Mittwoch, 27. März 13
DSLs
                         for
                        Java
Mittwoch, 27. März 13
grammar inheritance

                               Grammar (Parser, Lexer)
                                           Linker
                                           Type System




                                JvmModel
              MyLanguage                   Interpreter /
                                           Advanced Editor
                                           Eclipse Integration
                                           Debugger


                                      JvmModelInferrer



Mittwoch, 27. März 13
Model Inference
                                             Class Customer
                   entity Customer {
                     name: String               Field name
                     address: Address
                     purchasedItems: Item*      Method getName
                     double sales() {
                       purchasedItems
                                                   returnType String
                         .map[price]
                         .reduce[a,b|a+b]
                     }                         Method setName
                   }
                                                   parameter name

                                                       type String


Mittwoch, 27. März 13
Model Inference
                   entity Customer {
                     name: String
                     address: Address
                     purchasedItems: Item*
                     double sales() {        Method sales
                       purchasedItems
                         .map[price]           returnType double
                         .reduce[a,b|a+b]
                     }
                                                body Expression
                   }




Mittwoch, 27. März 13
class ScriptingJvmModelInferrer extends AbstractModelInferrer {
                        	
                           	
                           @Inject extension JvmTypesBuilder

                             	
                             def dispatch void infer(Script script,
                                    IJvmDeclaredTypeAcceptor acceptor,
                                    boolean isPreIndexingPhase) {
                             	
                             	 acceptor.accept(script.toClass('MyClass'))
                                    .initializeLater [
                             	
                             	 	 // the class gets one main method
                             	
                             	 	 members += script.toMethod(
                                           'main',
                                           script.newTypeRef(Void::TYPE)) [
                             	
                             	 	 	 parameters += script.toParameter("args",
                                           script.newTypeRef(typeof(String))
                                                  .addArrayTypeDimension)
                             	
                             	 	 	 static = true
                             	
                             	 	 	 varArgs = true
                             	
                             	 	 	 // Associate the script as the body of the main method
                             	
                             	 	 	 body = script
                             	
                             	 	 ]	
                             	
                             	 ]
                            	}
                        }




Mittwoch, 27. März 13
The 7 Languages
                            Scripting Language
                           DSL for MongoDB
                          Http Routing Language
                           Templates Language
                             DSL for Guice
                             Build Language
                                Tortoise

Mittwoch, 27. März 13
Find more at
                        www.eclipse.org/Xtext/7languages.html




Mittwoch, 27. März 13

Java DSLs with Xtext

  • 1.
    Java DSLs with Xtext Jan Koehnlein Mittwoch, 27. März 13
  • 2.
  • 3.
  • 4.
    public class Customerimplements Serializable { private String name; private Address address; private List<Item> purchase; public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public List<Item> getPurchase() { return purchase; } public void setPurchase(List<Item> items) { this.purchase = purchase; } } Mittwoch, 27. März 13
  • 5.
    @Entity public class Customer implements Serializable { @Id private String name; private Address address; private List<Item> purchase; public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="ADDR", nullable=false) public Address getAddress() return address; } public void setAddress(Address address) { this.address = address; } @ManyToMany public List<Item> getPurchase() { return purchase; } public void setPurchase(List<Item> items) { this.purchase = purchase; } } Mittwoch, 27. März 13
  • 6.
  • 7.
    DSL Java entity Customer { @Entity name: String public class Customer implements Serializable { @Id address: Address private String name; purchase: Item* private Address address; private List<Item> purchase; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="ADDR", nullable=false) Code public Address getAddress() { } return address; public void setAddress(Address address) { Generation this.address = address; } @ManyToMany public List<Item> getPurchase() { return purchase; } public void setPurchase(List<Item> items) { this.purchase = purchase; } } Mittwoch, 27. März 13
  • 8.
    ? Methods Logic Arithmetics Behavior Mittwoch, 27. März 13
  • 9.
    entity Customer { name: String address: Address purchase: Item* double sales() { double result = 0.0; for(Item item: purchase) result += item.getPrice(); return result; } } Mittwoch, 27. März 13
  • 10.
    Expressions Complex Syntax Typesystem Compiler / Interpreter Mittwoch, 27. März 13
  • 11.
    Integration Patterns Manually written code Generated code Mittwoch, 27. März 13
  • 12.
    Reusable powerful expression library language Java‘s Typesystem Access to Java-elements Compile to Java Code Mittwoch, 27. März 13
  • 13.
    DSLs for Java Mittwoch, 27. März 13
  • 14.
    grammar inheritance Grammar (Parser, Lexer) Linker Type System JvmModel MyLanguage Interpreter / Advanced Editor Eclipse Integration Debugger JvmModelInferrer Mittwoch, 27. März 13
  • 15.
    Model Inference Class Customer entity Customer { name: String Field name address: Address purchasedItems: Item* Method getName double sales() { purchasedItems returnType String .map[price] .reduce[a,b|a+b] } Method setName } parameter name type String Mittwoch, 27. März 13
  • 16.
    Model Inference entity Customer { name: String address: Address purchasedItems: Item* double sales() { Method sales purchasedItems .map[price] returnType double .reduce[a,b|a+b] } body Expression } Mittwoch, 27. März 13
  • 17.
    class ScriptingJvmModelInferrer extendsAbstractModelInferrer { @Inject extension JvmTypesBuilder def dispatch void infer(Script script, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) { acceptor.accept(script.toClass('MyClass')) .initializeLater [ // the class gets one main method members += script.toMethod( 'main', script.newTypeRef(Void::TYPE)) [ parameters += script.toParameter("args", script.newTypeRef(typeof(String)) .addArrayTypeDimension) static = true varArgs = true // Associate the script as the body of the main method body = script ] ] } } Mittwoch, 27. März 13
  • 18.
    The 7 Languages Scripting Language DSL for MongoDB Http Routing Language Templates Language DSL for Guice Build Language Tortoise Mittwoch, 27. März 13
  • 19.
    Find more at www.eclipse.org/Xtext/7languages.html Mittwoch, 27. März 13