Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Code Generation idioms with Xtend

In this session, you will learn about typical best practices and idioms for building continuously running, extensible and maintainable code generators with Xtend. We think Xtend is the best language for code generation available today.

  • Identifiez-vous pour voir les commentaires

Code Generation idioms with Xtend

  1. 1. Code Generation Idioms with Holger Schill
  2. 2. AST What is a Code Generator? Generator Code
  3. 3. What languages are out there…
  4. 4. “Template” written in Java
  5. 5. The default formatting for that…
  6. 6. Template using Velocity
  7. 7. Template using Velocity
  8. 8. // Generates Java classes source code // by using Apache Velocity GeneratorUtility utility = new GeneratorUtility(); for (int i = 0; i < classes.size(); i++) { VelocityContext context = new VelocityContext(); ClassDescriptor cl = (ClassDescriptor) classes.get(i); context.put("class", cl); context.put("utility", utility); Template template = Velocity.getTemplate(templateFile); BufferedWriter writer = new BufferedWriter(new FileWriter(cl.getName()+”.java”)); template.merge(context, writer); writer.flush(); writer.close(); Template using Velocity
  9. 9. // Generates Java classes source code // by using Apache Velocity GeneratorUtility utility = new GeneratorUtility(); for (int i = 0; i < classes.size(); i++) { VelocityContext context = new VelocityContext(); ClassDescriptor cl = (ClassDescriptor) classes.get(i); context.put("class", cl); context.put("utility", utility); Template template = Velocity.getTemplate(templateFile); BufferedWriter writer = new BufferedWriter(new FileWriter(cl.getName()+”.java”)); template.merge(context, writer); writer.flush(); writer.close(); Template using Velocity
  10. 10. Template using Velocity
  11. 11. And there are many more… Xpand Acceleo Jet …
  12. 12. And there are many more… Xpand Acceleo Jet … Everywhitespacegoesintotheoutput
  13. 13. And there are many more… Xpand Acceleo Jet … Everywhitespacegoesintotheoutput Badtooling
  14. 14. And there are many more… Xpand Acceleo … Everywhitespacegoesintotheoutput Interpreted Badtooling Slow
  15. 15. And there are many more… Jet … Everywhitespacegoesintotheoutput TranslatestoJava Badtooling Fast
  16. 16. Let’s see what can do for us…
  17. 17. Let’s see what can do for us… It feels a bit like Java StaticTyping
  18. 18. Let’s see what can do for us… Compiles to Java SeamlessintegrationIt feels a bit like Java
  19. 19. Let’s see what can do for us… But can do more than that… Compiles to Java It feels a bit like Java
  20. 20. Template Expressions (aka String Concatenation)
  21. 21. Greyspace Shows what makes it in the generated code Makes readable templates possible
  22. 22. Greyspace Indentation of call is taken into account
  23. 23. Model Navigation
  24. 24. 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
  25. 25. filter map fold sortBy head reduce Build your own! It’s just library! Higher-Order Functions
  26. 26. Extensions Add new API where you need it
  27. 27. Extensions
  28. 28. Extensions
  29. 29. Dynamic Dispatch
  30. 30. Dynamic Dispatch
  31. 31. Dynamic Dispatch Generated Java
  32. 32. Dynamic Dispatch
  33. 33. IDE Support Extract method RenameRefactoring Content Assist Quick Assist Formatting Type Hierarchy Call Hierarchy Syntax Coloring Outline etc…
  34. 34. Debugging
  35. 35. Debugging
  36. 36. Debugging
  37. 37. Debugging
  38. 38. Debugging
  39. 39. Debugging
  40. 40. Modularization
  41. 41. Feature Models?
  42. 42. AOP?
  43. 43. Dependency Injection
  44. 44. ’s easy usage No Engine needed No ContextObject needed
  45. 45. ’s easy usage No Engine needed No ContextObject needed At the end it’s feels like using Java but better…
  46. 46. Best practices
  47. 47. Write readable and understandable code Should be self-evident but …
  48. 48. Write readable and understandable code Should be self-evident but …
  49. 49. Make use of dependency injection Guice
  50. 50. Use incremental code generators
  51. 51. Use incremental code generators Fast turnarounds
  52. 52. Use incremental code generators Fast turnarounds Just having it run on a server is no option
  53. 53. The m - 1 Problem BeanBeanObject Resource Configuration File BeanBeanObject Resource BeanBeanObject Resource ResourceSet
  54. 54. ResourceSet The m - 1 Problem BeanBeanObject Resource Configuration File BeanBeanObject Resource BeanBeanObject Resource It’s worth to think about a merging strategy
  55. 55. Incremental code generators in Xtext // generator API fragment = generator.GeneratorFragment auto-inject {}
  56. 56. 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(', ')) } }
  57. 57. 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
  58. 58. 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
  59. 59. more than just a code-gen language… http://www.xtend-lang.org/
  60. 60. Questions ?

×