SlideShare a Scribd company logo
Code Generation with
Sven Efftinge - itemis
What is it?
Successor to Xpand
Built with
Why ?
much faster (as fast as Java)
much better IDE support
generalized set of features
simpler
Code Generation Essentials
Template Expression
(aka string concatenation)
same syntax as in Xpand
just an expression
greyspace support!
Codegeneration With Xtend
static typing
simply Java
no pluggable type system
can be emulated with
active annotations
longitude & latitude
Codegeneration With Xtend
Codegeneration With Xtend
Codegeneration With Xtend
Codegeneration With Xtend
Codegeneration With Xtend
higher-order functions
map, filter, flatten, etc.
it’s just library!
class TypeProviderExample {
	
	 def getHourlyData() {
	 	 data.weather.hourlys
	 }
	
	 def getTimeWithHighestSwell() {
	 	 hourlyData.sortBy[swellheightM].head.time
	 }
	
	 def getAverageWindSpeed() {
	 	 hourlyData.map[windspeedkmph].reduce[a,b|a+b] / hourlyData.size
	 }
}
dynamic dispatch
dynamic dispatch
dynamic dispatch
easily debuggable & extendable
dynamic dispatch
	 def generate(ETypedElement e) {
	 	 switch e {
	 	 	 EAttribute : '''
	 	 	 	 attribute
	 	 	 '''
	 	 	 EReference case e.isContainment: '''
	 	 	 	 containment reference
	 	 	 '''
	 	 	 EReference : '''
	 	 	 	 reference
	 	 	 '''
	 	 	 EOperation : '''
	 	 	 	 operation
	 	 	 '''
	 	 	 default : throw new IllegalArgumentException("not handled : "+e)
	 	 }
	 }
dispatch over type and state
first come
first serve
modularization
AOP?
Dependency Injection!
class MainGenerator {
	
	 @Inject extension EcoreGen ecoreGenerator
	
	 def doGenerate(EClass it) '''
	 	 class «name» {
	 	 	 «FOR f : EStructuralFeatures»
	 	 	 	 «f.generate»
	 	 	 «ENDFOR»
	 	 }
	 '''
}
class EcoreGen {
	
	 def dispatch generate(EAttribute attribute) '''
	 	 generate attribute
	 '''
	
	 def dispatch generate(EReference reference) '''
	 	 generate reference
	 '''
	
	 def dispatch generate(EOperation operation) '''
	 	 generate operation
	 '''
}
declare the dependency
class MySpecialEcoreGen extends EcoreGen {
	
	 override dispatch generate(EAttribute attribute) '''
	 	 special EAttribute
	 '''
	
	 def dispatch generate(EParameter param) '''
	 	 parameter
	 '''
}
class CustomizedGenerator {
	 def static void main(String[] args) {
	 	 val injector = Guice::createInjector [
	 	 	 bind(typeof(EcoreGen)).to(typeof(MySpecialEcoreGen))
	 	 ]
	 	 val mainGenerator = injector.getInstance(typeof(MainGenerator))
	 	 mainGenerator.doGenerate(EcorePackage::eINSTANCE.EClass)
	 }
}
Extend the class you want to modify
Use Guice to wire up the generator
content assist
syntax coloring
quick assist outline
rename refactoring
formating
call hierarchy
type hierarchy
extract method
debugging
etc...
IDE Support
But why always a DSL?
MyBean
String value
class
MyBean
String value
class {
}
@Observable
Codegeneration With Xtend
Codegeneration With Xtend
Codegeneration With Xtend
Codegeneration With Xtend
Codegeneration With Xtend
Codegeneration With Xtend
Codegeneration With Xtend
Codegeneration With Xtend
future work
tooling via library
more macro support
embedded languages
snappiness!
Thank you!
@svenefftinge / @xtext / @xtendlang (c) 2013 by

More Related Content

Codegeneration With Xtend