Writing a code generator in Xtend

A generator stub is automatically created by Xtext. In our example, the stub is created in the org.example.entities.generator package:

class EntitiesGenerator extends AbstractGenerator {
  override
 void doGenerate(Resource res,
                            IFileSystemAccess2 fsa, IGeneratorContext context) {
    // TODO implement me
  }
}

Before writing the actual code, let's recall that Xtext is a framework; thus, the overall flow of control is dictated by the framework, not by the programmer. This is also known as the Hollywood Principle: Don't call us, we'll call you. This means that you do not have to manually run the generator. Your DSL Xtext editor is already integrated in the automatic building infrastructure of Eclipse, and the generator ...

Get Implementing Domain-Specific Languages with Xtext and Xtend - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.