Codegeneration With Xtend

14,984 views

Published on

Why you should implement Code Generators with Xtend.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
14,984
On SlideShare
0
From Embeds
0
Number of Embeds
11,438
Actions
Shares
0
Downloads
41
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Codegeneration With Xtend

  1. 1. Code Generation withSven Efftinge - itemis
  2. 2. What is it?
  3. 3. Successor to XpandBuilt with
  4. 4. Why ?much faster (as fast as Java)much better IDE supportgeneralized set of featuressimpler
  5. 5. Code Generation Essentials
  6. 6. Template Expression(aka string concatenation)same syntax as in Xpandjust an expressiongreyspace support!
  7. 7. static typingsimply Javano pluggable type systemcan be emulated withactive annotations
  8. 8. longitude & latitude
  9. 9. higher-order functionsmap, filter, flatten, etc.it’s just library!
  10. 10. 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 }}
  11. 11. dynamic dispatch
  12. 12. dynamic dispatch
  13. 13. dynamic dispatcheasily debuggable & extendable
  14. 14. 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 statefirst comefirst serve
  15. 15. modularization
  16. 16. AOP?
  17. 17. Dependency Injection!
  18. 18. 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
  19. 19. 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 modifyUse Guice to wire up the generator
  20. 20. content assistsyntax coloringquick assist outlinerename refactoringformatingcall hierarchytype hierarchyextract methoddebuggingetc...IDE Support
  21. 21. But why always a DSL?
  22. 22. MyBeanString valueclass
  23. 23. MyBeanString valueclass {}@Observable
  24. 24. future worktooling via librarymore macro supportembedded languagessnappiness!
  25. 25. Thank you!@svenefftinge / @xtext / @xtendlang (c) 2013 by

×