DSLs For Java - 0
DSLs For Java - 0
Jan Koehnlein
Mittwoch, 27. Mrz 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; }
@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; }
Domain-Specic Languages
DSL
entity Customer { name: String address: Address purchase: Item* }
Java
@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; }
Code Generation
entity Customer { name: String address: Address purchase: Item* double sales() { double result = 0.0; for(Item item: purchase) result += item.getPrice(); return result; } }
Expressions
Integration Patterns
Manually written code
Generated code
Reusable powerful expression library language Javas Typesystem Access to Java-elements Compile to Java Code
grammar inheritance
Grammar (Parser, Lexer) Linker Type System Interpreter / Advanced Editor Eclipse Integration Debugger
JvmModel
MyLanguage
JvmModelInferrer
Model Inference
Class Customer
entity Customer { name: String address: Address purchasedItems: Item* double sales() { purchasedItems .map[price] .reduce[a,b|a+b] } }
Field name Method getName returnType String Method setName parameter name type String
Model Inference
entity Customer { name: String address: Address purchasedItems: Item* double sales() { purchasedItems .map[price] .reduce[a,b|a+b] } }
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 ! ! ! ]! ! ! ] !} }
The 7 Languages
Scripting Language DSL for MongoDB Http Routing Language Templates Language DSL for Guice Build Language Tortoise
Mittwoch, 27. Mrz 13
Find more at
www.eclipse.org/Xtext/7languages.html