Xtext and Java

The following chapter demonstrates how to integrate your own DSL with Java. We will do this in four stages: First, you will learn how to refer to existing Java elements from within your language. Then you will use Xbase to refer to generic types. In the third step, you will map your own DSL's concepts to Java concepts. Last but not least, you will use both Java types and your concepts within Xbase expressions and execute it.

Throughout this chapter, we will step by step improve the domain model example from the tutorial.

Top

Plug-in Setup

In the following, we are going to use the JVM types model and the Xbase language library. To make these available to your language, you have to add a couple of dependencies. In the runtime plug-in of your language, add dependencies to

  • org.eclipse.xtext.common.types
  • org.eclipse.xtext.xbase and
  • org.eclipse.xtext.xtend2.lib if you are using Xtend to implement components.

Your UI plug-in needs dependencies to

  • org.eclipse.xbase.ui
  • org.eclipse.xtext.common.types.ui

Additionally, you have to register the genmodel of Xbase to the standalone setup of your MWE2 workflow and make sure the TypesGeneratorFragment (src) and the XbaseGeneratorFragment (src) are included:

bean = StandaloneSetup {
  registerGenModelFile = 
     "platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel"
}
...
fragment = types.TypesGeneratorFragment {}
fragment = xbase.XbaseGeneratorFragment {}

To avoid running out of memory when regenerating, you increase the maximum heap size and the permanent generation space in the run configuration of your workflow. We recommend at least

-Xmx512m -XX:MaxPermSize=128m

in the VM Arguments section of the Arguments tab. If you are experiencing ambiguity warnings from Antlr, the usual countermeasures apply.