Eclipse Xcore Cheat Sheet

From emmtrix Wiki
Jump to navigation Jump to search

xcore is an advanced textual modeling language within the Eclipse ecosystem that extends Ecore, the core modeling framework in Eclipse Modeling Framework (EMF). It integrates with Xbase, transforming Ecore into a fully-fledged programming language that supports not only the definition of model structures but also the behavior of operations, derived features, and data type conversions. By combining modeling and programming in a single environment with high-quality tools similar to those found in Java Development Tools (JDT), Xcore eliminates the traditional divide between these disciplines, making it a powerful tool for model-driven development.

The problem with Xcore is that the official documentation (https://wiki.eclipse.org/Xcore) is not very detailed, and the language is not widely used, so it is difficult to find examples and best practices. This page is an attempt to summarize the most important features of Xcore and provide a quick reference for developers.

Variables

Reference Expanation
contains Containment reference
refers Cross reference
container Container reference
Flag Expanation
unsettable The variable gets an additional flag if it is set. The flag can be accessed by the following methods
  • unset<name>
  • isSet<name>
readonly No set<name> methods are generated.
volatile No variable is generated. Instead the following functions must be provided:
  • get<Name>
  • set<Name>
  • basicSet<Name>
transient The attribute is not stored.
derived ?
unique ?
opposite <type>
local Sets resolveProxies flag to false. The generated get() method will assume that the target object is never a proxy, and therefore will not attempt to resolve the target. This improves the performance of the get() method, but it should only be used if you are sure that the target oar a reference will never be stored in a different document from the source.
get { <code> }
set { <code> }

Procedures

op DeclaredType lookupType(String name) {
  lookupTypeLocal(name) ?: parent?.lookupType(name)
}

Expressions

Expression Expanation
this This
it Object in lambda expression
self
super.func Call function in parent class object
x as Type Type cast
myRef?.doStuff() if ( myRef != null ) myRef.doStuff()
val res = new BasicEList<Type> Create an Type[] object
val res = ECollections.singletonEList(t) Create a one-element Type[] object
list as EList<?> as EList<Type> Convert any Object[] to Type[]
ECollections.emptyEList() Create empty Type[]

Streaming

e.g.

types.filter(DeclaredType).findFirst[comp.isEqual(it, t)]
Code Expanation
.filter(Type) Filter by class
.filterNull Filter not null
.findFirst[lambda] Find first by lambda expression
.findLast[lambda] Find last by lambda expression
.map[lambda] Map type by lambda
.exists[lambda] True if one element exists
.head First element
.tail Last element
.forEach[lambda] Execute lambda/function for each function
.asEList
.unmodifiableEList

Other

Last element of List

Old (pre Java 21): my_list.last returns the last element of the list. This is handled internally with utility methods as the List interface did not have getLast()

New: use IterableExtensions.last(my_list) to get the last element of a list. The generated Java code is identical to the old version. Using the old version my_list.last does not work as the List interface now has a getLast() method with conflicting beahvior. When called on an empty list, Xcore, Xtend, Xtext return null, whereas the List interface throws a NoSuchElementException.

External Links