Accessing the values of an annotation inside your Xtext-DSL

Today I implemented the last feature of my bachelor thesis and struggled upon a small problem: My DSL makes use of XAnnotation to annotate various grammar elements. The generators can extend the DSL by introducing new annotations which can be used for modifying the generation process of the resulting fragments. Checking, whether an annotation is present or not is easy but retrieving the value of a named parameters in a really easy way was not so obvious at first. Thankfully Xtext/Xbase provides the class XbaseInterpreter for evaulating XExpression objects on the fly. This is the helper method:

class AnnotationExtension {
	XbaseInterpreter interpreter;

	public Object getValue(XAnnotation annotation, String simpleName) {
		IEvaluationResult result = null;
		if (simpleName.equals("value")) {
			result = interpreter.evaluate(annotation.getValue());

		if (result != null) {
			for (XAnnotationElementValuePair pair : annotation
					.getElementValuePairs()) {
				if (pair.getElement().getSimpleName().equals(simpleName)) {
					result = interpreter.evaluate(pair.getValue());

		return (result != null) ? result.getResult() : null;

And here is the snippet of the usage inside the DSL:

import de.schakko.rapid.artifact.fpanalysis.runtime.annotation.UseFunctionpointType
import de.schakko.rapid.artifact.fpanalysis.runtime.types.FunctionpointType

// ...

domain d5_fixture {
  process p5_custom {

Leave a reply

Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>