DSLs For Java

Werbung
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 {
Mittwoch, 27. März 13
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;
}
Mittwoch, 27. März 13
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-Specific Languages
Mittwoch, 27. März 13
DSL
entity Customer {
name: String
address: Address
purchase: Item*
}
Code
Generation
Mittwoch, 27. März 13
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;
}
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
JvmModel
MyLanguage
Type System
Interpreter /
Advanced Editor
Eclipse Integration
Debugger
JvmModelInferrer
Mittwoch, 27. März 13
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
Mittwoch, 27. März 13
Model Inference
entity Customer {
name: String
address: Address
purchasedItems: Item*
double sales() {
purchasedItems
.map[price]
.reduce[a,b|a+b]
}
}
Mittwoch, 27. März 13
Method sales
returnType double
body Expression
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
Herunterladen