Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Recipes to build Code Generators for Non-Xtext Models with Xtend

874 views

Published on

This talk was given at EclipseCon Europe 2016 on Oct 26th 2016 in Ludwigsburg

Published in: Engineering
  • Be the first to comment

Recipes to build Code Generators for Non-Xtext Models with Xtend

  1. 1. Karsten Thoms & Holger Schill Recipes to build Code Generators for Non-Xtext Models with Xtend_____ ________
  2. 2. Templates a la Xtend Minimal Generator with JSON Model Generator with Xtext Framework on EMF UML Model 3,50€ 8,70€ 16,20€
  3. 3. Template Expressions (aka String Concatenation)
  4. 4. Greyspace Shows what makes it in the generated code Makes readable templates possible
  5. 5. Greyspace Indentation of call is taken into account
  6. 6. Model Navigation
  7. 7. Model Navigation val eClasses = myEPackage.EClassifiers.filter(EClass)
 val abstractEClasses = eClasses.filter[isAbstract]
 val namesOfAbstractClasses = abstractEClasses.map[name]
 val commaSeparatedNamesOfAbstractClasses = namesOfAbstractClasses.join(',') CommaSeparatedNamesOfAbstractClasses Model Navigation
  8. 8. filter map fold sortBy head reduce Build your own! It’s just library! Higher-Order Functions
  9. 9. Extensions Add new API where you need it
  10. 10. Extensions
  11. 11. Extensions
  12. 12. Dynamic Dispatch
  13. 13. Dynamic Dispatch
  14. 14. Dynamic Dispatch Generated Java
  15. 15. Dynamic Dispatch
  16. 16. IDE Support Extract method RenameRefactoring Content Assist Quick Assist Formatting Type Hierarchy Call Hierarchy Syntax Coloring Outline etc… Debugging
  17. 17. Modularization
  18. 18. Feature Models?
  19. 19. AOP?
  20. 20. Dependency Injection
  21. 21. ’s easy usage No Engine needed No ContextObject needed
  22. 22. ’s easy usage No Engine needed No ContextObject needed At the end it’s feels like using Java but better…
  23. 23. Write readable and understandable code Should be self-evident but …
  24. 24. Write readable and understandable code Should be self-evident but …
  25. 25. Make use of dependency injection Guice
  26. 26. Use incremental code generators
  27. 27. Use incremental code generators Fast turnarounds
  28. 28. Use incremental code generators Fast turnarounds Just having it run on a server is no option
  29. 29. The m - 1 Problem BeanBeanObject Resource Configuration File BeanBeanObject Resource BeanBeanObject Resource ResourceSet
  30. 30. ResourceSet The m - 1 Problem BeanBeanObject Resource Configuration File BeanBeanObject Resource BeanBeanObject Resource It’s worth to think about a merging strategy
  31. 31. Incremental code generators in Xtext // generator API fragment = generator.GeneratorFragment auto-inject {}
  32. 32. Incremental code generators in Xtext // generator API fragment = generator.GeneratorFragment auto-inject {} class MyDslGenerator implements IGenerator { override void doGenerate(Resource resource, IFileSystemAccess fsa) { // fsa.generateFile('greetings.txt', 'People to greet: ' + // resource.allContents // .filter(typeof(Greeting)) // .map[name] // .join(', ')) } }
  33. 33. Incremental code generators in Xtext // generator API fragment = generator.GeneratorFragment auto-inject {} class MyDslGenerator implements IGenerator { override void doGenerate(Resource resource, IFileSystemAccess fsa) { // fsa.generateFile('greetings.txt', 'People to greet: ' + // resource.allContents // .filter(typeof(Greeting)) // .map[name] // .join(', ')) } } Customisable mechanism is taking care to recompile affected resources
  34. 34. Incremental code generators in Xtext // generator API fragment = generator.GeneratorFragment auto-inject {} class MyDslGenerator implements IGenerator { override void doGenerate(Resource resource, IFileSystemAccess fsa) { // fsa.generateFile('greetings.txt', 'People to greet: ' + // resource.allContents // .filter(typeof(Greeting)) // .map[name] // .join(', ')) } } http://www.eclipse.org/Xtext/documentation/ 350_continuous_integration.html Get Maven integration for free
  35. 35. more than just a code-gen language… http://www.xtend-lang.org/
  36. 36. Why „for Non-Xtext Models“? • Xtext provides an infrastructure to generate code from Xtext DSL files • Proper setup of Guide required • Default Xtext Guice modules are only suitable for Xtext DSLs • Not much code generators for non-Xtext models known in open source projects
  37. 37. Generator Process 1.Read Models 2.Validation 3.Execute Templates 4.Write to File-System
  38. 38. 2,1 MB Guava Library 370 kB Xbase/Xtend Library 226 kB JSON Library (Gson) 60 kB CLI Library (JCommander) 4 Metamodel Classes 1 Xtend Code Template Minimal Generator with JSON Model
  39. 39. JSON Input
  40. 40. Meta Classes
  41. 41. Xtend Code Template
  42. 42. Main CLI Class
  43. 43. 1 Xtext Framework 14 MB of Libraries 1 (EMF) UML Model 1 MWE2 Workflow 1 Guice Configuration 1 Standalone Setup 1 Output Configuration some Xtend Templates Generator with Xtext Framework on EMF UML Model
  44. 44. Xtext’s Generator Support • IGenerator Interface • MWE components for reading models and executing the generator • Dependency Injection support • Eclipse build integration, Incremental build • Different file access for plain Java and EFS • Outlets
  45. 45. Generic Resource Service • Package org.eclipse.xtext.resource.generic contains support for generic resources • Requires EMF based models • Makes resources for non-Xtext based EMF models available for the Xtext infrastructure • Index • compute IResourceDescription • create IEObjectDescription
  46. 46. Xtend Code Template
  47. 47. Guice Runtime Module
  48. 48. Guice Runtime Module
  49. 49. Guice Runtime Module
  50. 50. Guice Runtime Module ! !
  51. 51. Guice Runtime Module
  52. 52. Guice Runtime Module
  53. 53. Standalone Setup
  54. 54. Generator Workflow
  55. 55. Generator Workflow
  56. 56. UI Integration
  57. 57. plugin.xml
  58. 58. Extension Factory
  59. 59. UI Builder Integration Generate on file save
  60. 60. Build Integration
  61. 61. Import Organizing
  62. 62. Bon Appetit!
  63. 63. KEEP INFORMED itemis.com / Blogs / Xtext @kthoms @holgerschill @itemis @chrdietrich @franzbecker_io Find the examples here: https://github.com/kthoms/xtend-codegen-examples

×