SlideShare a Scribd company logo
Code Generation Idioms with
Holger Schill
AST
What is a Code Generator?
Generator Code
What languages are out there…
“Template” written in Java
The default formatting for that…
Template using Velocity
Template using Velocity
// 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
// 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
Template using Velocity
And there are many more…
Xpand
Acceleo
Jet
…
And there are many more…
Xpand
Acceleo
Jet
…
Everywhitespacegoesintotheoutput
And there are many more…
Xpand
Acceleo
Jet
…
Everywhitespacegoesintotheoutput
Badtooling
And there are many more…
Xpand
Acceleo
…
Everywhitespacegoesintotheoutput
Interpreted
Badtooling
Slow
And there are many more…
Jet
…
Everywhitespacegoesintotheoutput
TranslatestoJava
Badtooling
Fast
Let’s see what can do for us…
Let’s see what can do for us…
It feels a bit like Java
StaticTyping
Let’s see what can do for us…
Compiles to Java
SeamlessintegrationIt feels a bit like Java
Let’s see what can do for us…
But can do more than that…
Compiles to Java
It feels a bit like Java
Template Expressions
(aka String Concatenation)
Greyspace
Shows what makes it in the generated code
Makes readable templates possible
Greyspace
Indentation of call is taken into account
Model Navigation
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
filter
map
fold
sortBy
head
reduce
Build your own! It’s just library!
Higher-Order Functions
Extensions
Add new API where you need it
Extensions
Extensions
Dynamic Dispatch
Dynamic Dispatch
Dynamic Dispatch
Generated Java
Dynamic Dispatch
IDE Support
Extract method
RenameRefactoring
Content Assist
Quick Assist
Formatting
Type Hierarchy
Call Hierarchy
Syntax Coloring
Outline
etc…
Debugging
Debugging
Debugging
Debugging
Debugging
Debugging
Modularization
Feature Models?
AOP?
Dependency Injection
’s easy usage
No Engine needed
No ContextObject needed
’s easy usage
No Engine needed
No ContextObject needed
At the end it’s feels like using Java
but better…
Best practices
Write readable and understandable code
Should be self-evident but …
Write readable and understandable code
Should be self-evident but …
Make use of dependency injection
Guice
Use incremental code generators
Use incremental code generators
Fast turnarounds
Use incremental code generators
Fast turnarounds
Just having it run on a server is no option
The m - 1 Problem
BeanBeanObject
Resource
Configuration File
BeanBeanObject
Resource
BeanBeanObject
Resource
ResourceSet
ResourceSet
The m - 1 Problem
BeanBeanObject
Resource
Configuration File
BeanBeanObject
Resource
BeanBeanObject
Resource
It’s worth to think about a
merging strategy
Incremental code generators in Xtext
// generator API
fragment = generator.GeneratorFragment auto-inject {}
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(', '))
}
}
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
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
more than just a code-gen language…
http://www.xtend-lang.org/
Questions ?

More Related Content

PDF
Phẫu thuật nâng ngực.pdf
PDF
Bone-age-book.pdf
PPTX
Ara Kassarjian: MRI of the Rectus Femoris - PRP
PPT
metastatic-bone-tumor.ppt
PDF
Codegeneration With Xtend
PDF
Functional programming with Xtend
PDF
Xtend - better java with -less- noise
PDF
Xtext beyond the defaults - how to tackle performance problems
Phẫu thuật nâng ngực.pdf
Bone-age-book.pdf
Ara Kassarjian: MRI of the Rectus Femoris - PRP
metastatic-bone-tumor.ppt
Codegeneration With Xtend
Functional programming with Xtend
Xtend - better java with -less- noise
Xtext beyond the defaults - how to tackle performance problems

Viewers also liked (20)

PDF
Introduction to Xbase
PDF
Using Xcore with Xtext
PDF
Xtext Webinar
PDF
Building DSLs With Eclipse
PDF
The Xtext Grammar Language
PDF
Recipes to build Code Generators for Non-Xtext Models with Xtend
KEY
EMF - Beyond The Basics
DOC
EMF Tips n Tricks
PDF
DSLs for Java Developers
PDF
Serializing EMF models with Xtext
KEY
Xtend - A Language Made for Java Developers
PDF
Graphical Views For Xtext With FXDiagram
PDF
Graphical Views For Xtext
PDF
Jazoon 2010 - Building DSLs with Eclipse
PPTX
Enhancing Xtext for General Purpose Languages
PDF
Eclipse DemoCamp in Paris: Language Development with Xtext
PDF
ARText - Driving Developments with Xtext
PDF
From Stairway to Heaven onto the Highway to Hell with Xtext
PDF
Xtext, diagrams and ux
PDF
Language Engineering With Xtext
Introduction to Xbase
Using Xcore with Xtext
Xtext Webinar
Building DSLs With Eclipse
The Xtext Grammar Language
Recipes to build Code Generators for Non-Xtext Models with Xtend
EMF - Beyond The Basics
EMF Tips n Tricks
DSLs for Java Developers
Serializing EMF models with Xtext
Xtend - A Language Made for Java Developers
Graphical Views For Xtext With FXDiagram
Graphical Views For Xtext
Jazoon 2010 - Building DSLs with Eclipse
Enhancing Xtext for General Purpose Languages
Eclipse DemoCamp in Paris: Language Development with Xtext
ARText - Driving Developments with Xtext
From Stairway to Heaven onto the Highway to Hell with Xtext
Xtext, diagrams and ux
Language Engineering With Xtext
Ad

Similar to Code Generation idioms with Xtend (20)

PPT
Java Basics
PPTX
Curso de Programación Java Básico
PPTX
Real World MVC
DOCX
Java interview questions and answers
PPTX
Javascript And CSS Libraries
PPTX
Introduction to Core Java Programming
PPT
Basic java part_ii
PDF
If you want to automate, you learn to code
PDF
Handlebars & Require JS
PDF
[2015/2016] Require JS and Handlebars JS
PPTX
Codeigniter
PDF
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
PPT
Java basic
PDF
Handlebars and Require.js
PDF
Practices and tools for building better APIs
PDF
Practices and tools for building better API (JFall 2013)
PPT
Developing Java Web Applications
PDF
How to train the jdt dragon
PPT
An Introduction to Websphere sMash for PHP Programmers
PPT
A begineers guide of JAVA - Getting Started
Java Basics
Curso de Programación Java Básico
Real World MVC
Java interview questions and answers
Javascript And CSS Libraries
Introduction to Core Java Programming
Basic java part_ii
If you want to automate, you learn to code
Handlebars & Require JS
[2015/2016] Require JS and Handlebars JS
Codeigniter
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
Java basic
Handlebars and Require.js
Practices and tools for building better APIs
Practices and tools for building better API (JFall 2013)
Developing Java Web Applications
How to train the jdt dragon
An Introduction to Websphere sMash for PHP Programmers
A begineers guide of JAVA - Getting Started
Ad

Recently uploaded (20)

PPTX
Module_4_Updated_Presentation CORRUPTION AND GRAFT IN THE PHILIPPINES.pptx
PDF
Presentation1 [Autosaved].pdf diagnosiss
PDF
6.-propertise of noble gases, uses and isolation in noble gases
PPTX
ANICK 6 BIRTHDAY....................................................
PDF
natwest.pdf company description and business model
PPTX
An Unlikely Response 08 10 2025.pptx
PPTX
3RD-Q 2022_EMPLOYEE RELATION - Copy.pptx
PPTX
PurpoaiveCommunication for students 02.pptx
PPT
The Effect of Human Resource Management Practice on Organizational Performanc...
PPTX
Tour Presentation Educational Activity.pptx
PPTX
NORMAN_RESEARCH_PRESENTATION.in education
PDF
COLEAD A2F approach and Theory of Change
PPTX
Relationship Management Presentation In Banking.pptx
DOCX
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
PDF
PM Narendra Modi's speech from Red Fort on 79th Independence Day.pdf
PPTX
Tablets And Capsule Preformulation Of Paracetamol
PPTX
Hydrogel Based delivery Cancer Treatment
PPTX
BIOLOGY TISSUE PPT CLASS 9 PROJECT PUBLIC
PPTX
Intro to ISO 9001 2015.pptx wareness raising
PPTX
chapter8-180915055454bycuufucdghrwtrt.pptx
Module_4_Updated_Presentation CORRUPTION AND GRAFT IN THE PHILIPPINES.pptx
Presentation1 [Autosaved].pdf diagnosiss
6.-propertise of noble gases, uses and isolation in noble gases
ANICK 6 BIRTHDAY....................................................
natwest.pdf company description and business model
An Unlikely Response 08 10 2025.pptx
3RD-Q 2022_EMPLOYEE RELATION - Copy.pptx
PurpoaiveCommunication for students 02.pptx
The Effect of Human Resource Management Practice on Organizational Performanc...
Tour Presentation Educational Activity.pptx
NORMAN_RESEARCH_PRESENTATION.in education
COLEAD A2F approach and Theory of Change
Relationship Management Presentation In Banking.pptx
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
PM Narendra Modi's speech from Red Fort on 79th Independence Day.pdf
Tablets And Capsule Preformulation Of Paracetamol
Hydrogel Based delivery Cancer Treatment
BIOLOGY TISSUE PPT CLASS 9 PROJECT PUBLIC
Intro to ISO 9001 2015.pptx wareness raising
chapter8-180915055454bycuufucdghrwtrt.pptx

Code Generation idioms with Xtend