Posts Tagged ‘Xtend2’

Xtend2 Code Generators with Non-Xtext Models

In this blog post i want to show a simple example of how to use Xtend2 to generate code from Non-Xtext but EMF-based model.

Having a simple EMF Model i’ve created the genmodel + Model + Edit + Editor code.
Using the Editor i’ve created a bunch of .sample files and now want to
generate code using Xtend2.

Xtend comes with an IGenerator interface that i implement in my SampleGenerator Xtend file

package sample

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.emf.ecore.EObject

class SampleGenerator implements IGenerator {

	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
		for (EObject o : resource.contents) {

	def dispatch void compile(Model m, IFileSystemAccess fsa) {
		for (e : m.elements) {

	def compile(Element e, IFileSystemAccess fsa) {
		fsa.generateFile(".txt", '''
		this is element «»

	def dispatch void compile(EObject m, IFileSystemAccess fsa) { }


The last step we need is a workflow that reads the model files and invokes the generator

First we need to create some java classes that exposes our .sample to the reader
(resourceseriveprovider) and
do some Guice Binding Stuff (Generator / ResourceSet ….)

package sample;

import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.resource.generic.AbstractGenericResourceRuntimeModule;

public class SampleGeneratorModule extends AbstractGenericResourceRuntimeModule {

	protected String getLanguageName() {
		return "sample.presentation.SampleEditorID";

	protected String getFileExtensions() {
		return "sample";

	public Class<? extends IGenerator> bindIGenerator() {
		return SampleGenerator.class;

	public Class<? extends ResourceSet> bindResourceSet() {
		return ResourceSetImpl.class;



package sample;

import org.eclipse.xtext.ISetup;


public class SampleGeneratorSetup implements ISetup {

	public Injector createInjectorAndDoEMFRegistration() {
		return Guice.createInjector(new SampleGeneratorModule());



package sample;

import org.eclipse.xtext.resource.generic.AbstractGenericResourceSupport;


public class SampleGeneratorSupport extends AbstractGenericResourceSupport {

	protected Module createGuiceModule() {
		return new SampleGeneratorModule();


finally we wire this together in the workflow file

module sample.SampleGenerator

import org.eclipse.emf.mwe.utils.*

var targetDir = "src-gen"
var modelPath = "model"

Workflow {

	bean = StandaloneSetup {
		registerGeneratedEPackage = "sample.SamplePackage"

	component = DirectoryCleaner {
		directory = targetDir

	component = sample.SampleGeneratorSupport {}

	component = org.eclipse.xtext.mwe.Reader {
		path = modelPath
		register = sample.SampleGeneratorSetup {}
		loadResource = {
			slot = "model"

	component = org.eclipse.xtext.generator.GeneratorComponent {
		register = sample.SampleGeneratorSetup {}
		slot = 'model'
		outlet = {
			path = targetDir

running the workflow we get nice files generated

Categories: Xtext Tags: , ,

Customizing Xtext Metamodel Inference using Xtend2

Xtext has nice metamodel inference capabilities. But sometimes you have to do some customizations to the generated ecore metamodel, e.g. adding a derived operation. You have basically two options: (1) move to a manually maintained metamodel (2) use Customized Post Processing as described here

The second possibility uses good old Xpand/Xtend1 extensions to do the postprocessing. But what if i want to use Xtend2 for that? A very simple solution i’d like to show in this post.

So lets start with the gramar

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl ""


	"person" firstname=STRING lastname=STRING

We now want to add a getFullname Operation to our person.
Xtext offers the Interface IXtext2EcorePostProcessor for the postprocessing.
So we write a Xtend2 class for that

package org.xtext.example.mydsl

import org.eclipse.xtext.xtext.ecoreInference.IXtext2EcorePostProcessor
import org.eclipse.xtext.GeneratedMetamodel
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EClassifier
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EcoreFactory
import org.eclipse.emf.ecore.EcorePackage
import org.eclipse.emf.ecore.EcorePackage.Literals
import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage
import org.eclipse.emf.common.util.BasicEMap$Entry
import org.eclipse.emf.ecore.impl.EStringToStringMapEntryImpl

class MyXtext2EcorePostProcessor implements IXtext2EcorePostProcessor {
 	override void process(GeneratedMetamodel metamodel) {
	def process(EPackage p) {
		for (c : p.EClassifiers.filter(typeof(EClass))) {
			if ( == "Person") {
	def handle (EClass c) {
		val op = EcoreFactory::eINSTANCE.createEOperation = "getFullName"
		op.EType = EcorePackage::eINSTANCE.EString
		val body = EcoreFactory::eINSTANCE.createEAnnotation
		body.source = GenModelPackage::eNS_URI
		val map = EcoreFactory::eINSTANCE.create(EcorePackage::eINSTANCE.getEStringToStringMapEntry()) as BasicEMap$Entry<String,String>
		map.key = "body"
		map.value = "return getFirstname() + \" \" + getLastname();"
		op.EAnnotations += body
		c.EOperations += op

The last Problem left is how to make the Generator use this class.
Xtext does not offer a explicit place to change the IXtext2EcorePostProcessor.
Its default Implementation is bound in the XtextRuntimeModule,
that is instantiated in the org.eclipse.xtext.generator.Generator
class. so we have to subclass the Generator to get it changed

package org.xtext.example.mydsl;

import org.eclipse.xtext.XtextRuntimeModule;
import org.eclipse.xtext.XtextStandaloneSetup;
import org.eclipse.xtext.generator.Generator;
import org.eclipse.xtext.xtext.ecoreInference.IXtext2EcorePostProcessor;


public class ExtendedGenerator extends Generator {
	public ExtendedGenerator() {
		new XtextStandaloneSetup() {
			public Injector createInjector() {
				return Guice.createInjector(new XtextRuntimeModule() {
					public Class<? extends IXtext2EcorePostProcessor> bindIXtext2EcorePostProcessor() {
						return MyXtext2EcorePostProcessor.class;


finally we use the in the Generator Workflow

Workflow {
    bean = StandaloneSetup {
        scanClassPath = true
        platformUri = "${runtimeProject}/.."

    component = DirectoryCleaner {
        directory = "${runtimeProject}/src-gen"

    component = DirectoryCleaner {
        directory = "${runtimeProject}.ui/src-gen"

    component = ExtendedGenerator {
        pathRtProject = runtimeProject
        pathUiProject = "${runtimeProject}.ui"
        pathTestProject = "${runtimeProject}.tests"
        projectNameRt = projectName
        projectNameUi = "${projectName}.ui"
        language = {

as a result our person has the getFullname Operation

public interface Person extends EObject

  String getFirstname();

  void setFirstname(String value);

  String getLastname();

  void setLastname(String value);

  String getFullName();

} // Person
public class PersonImpl extends MinimalEObjectImpl.Container implements Person
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
  public String getFullName()
    return getFirstname() + " " + getLastname();

} //PersonImpl