An introduction to Xtend
The Xtend programming language comes with very nice documentation, which can be found on its website, https://www.eclipse.org/xtend/documentation/. We will give an overview of Xtend in this chapter, but we strongly suggest that you then go through the Xtend documentation thoroughly. Xtend itself is implemented in Xtext and it is a proof of concept of how involved a language implemented in Xtext can be.
We will use Xtend throughout this book to write all parts of a DSL implementation. Namely, we will use it to customize UI features, to write tests, to implement constraint checks, and to write code generators or interpreters for all the example DSLs we will develop in this book. In particular, all the stub classes generated by Xtext for your DSL projects are Xtend classes.
You can still generate Java stub classes by customizing the MWE2 workflow, but in this book, we will always use Xtend classes. Xtend, besides providing useful mechanisms for writing code generators, for example, multiline template expressions, also provides powerful features that make model visiting and traversing really easy, straightforward, and natural to read and maintain. Indeed, besides the grammar definition, for the rest of the time when implementing a DSL, you will have to visit the AST model. Xtend programs are translated into Java, and Xtend code can access all the Java libraries; thus Xtend and Java can cooperate seamlessly.
Note
Xtend is a general-purpose programming language, which can be used independently from Xtext language development. In particular, Xtend can be used as an alternative to Java or together with Java for any kind of Java application development, including web applications and Android applications.
Using Xtend in your projects
All the Eclipse projects generated by the Xtext wizard are already setup to use Xtend. However, in this chapter, in order to give an introduction to the language, we will use Xtend independently from Xtext language development.
You can use Xtend in your Eclipse Java projects (both plain Java and plugin projects). Let's now create an Eclipse plugin project where we will write a few Xtend examples. Perform the following steps:
- Start Eclipse, navigate to File | New | Project…, and select Plug-in Project.
- In the next dialog, you should specify the following Project name:
org.example.xtend.examples.
- Press Next, and unselect the checkboxes Generate an Activator and This plugin will make contributions to the UI.
- Press Finish. If asked to switch to the Plug-in Development perspective, choose Yes.
In order to create a new Xtend file, for example, an Xtend Class, right-click on your source folder and select New | Xtend Class. You will see that this wizard is similar to the standard New Java Class wizard, so you can choose the Package, the class Name, Superclass, and Interfaces. Refer to the following screenshot:
As soon as the class is created, you will get an error marker with the message Couldn't find the mandatory library org.eclipse.xtext.xbase.lib 2.8.0 or higher on the project's classpath
. You just need to use the quickfix Add Xtend libs to classpath
and the required Xtend bundles will be added to your project's dependencies. The quickfix can be accessed by clicking on the error marker in the editor's left ruler.
A new source folder will be created in your plugin project, xtend-gen
, where the Java code corresponding to your Xtend code will be automatically generated as soon as you save an .xtend
file. Just like src-gen
created by the Xtext generator (as seen in the previous chapter), the files in xtend-gen
must not be modified by the programmer, since they will be overwritten by the Xtend compiler.
Tip
The folder xtend-gen
is not automatically added to the build source folders of your plug-in project, and therefore you should add it manually in your build.properties
file. The file has a warning marker and the editor will provide you with a quickfix to add that folder. This is required only for plug-in projects.
You can use the same steps to create an Xtend class in a plain Java project. Also in this case, you will have to use the quickfix to add the Xtend libraries to the classpath. Of course, in this case, there is no build.properties
to adjust in plain Java projects.