官术网_书友最值得收藏!

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:

  1. Start Eclipse, navigate to File | New | Project…, and select Plug-in Project.
  2. In the next dialog, you should specify the following Project name: org.example.xtend.examples.
  3. Press Next, and unselect the checkboxes Generate an Activator and This plugin will make contributions to the UI.
  4. 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.

主站蜘蛛池模板: 阜南县| 都安| 平远县| 庆城县| 盐亭县| 西盟| 越西县| 天台县| 黄石市| 沁水县| 四子王旗| 阳信县| 无棣县| 迭部县| 大庆市| 庆城县| 贵德县| 杭锦后旗| 西乌珠穆沁旗| 黎川县| 拜城县| 正镶白旗| 麻城市| 灵丘县| 龙川县| 垫江县| 彭阳县| 大新县| 曲周县| 鄄城县| 墨玉县| 巴林右旗| 扎鲁特旗| 原阳县| 萨嘎县| 郁南县| 宁强县| 潞城市| 长治市| 天峨县| 思茅市|