Upload
sven-efftinge
View
12.212
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Why you should implement Code Generators with Xtend.
Citation preview
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!
static typingsimply Java
no pluggable type system
can be emulated withactive annotations
longitude & latitude
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 assistsyntax coloring
quick assist outline
rename refactoring
formating
call hierarchytype hierarchy
extract method
debugging
etc...
IDE Support
But why always a DSL?
MyBean
String value
class
MyBeanString value
class {
}
@Observable
future work
tooling via librarymore macro supportembedded languages
snappiness!
Thank you!
@svenefftinge / @xtext / @xtendlang (c) 2013 by