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 {
	@Inject
	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 {
  @UseFunctionpointType(FunctionpointType::EXTERNAL_INQUIRY)
  process p5_custom {
  }
}

I am asking you for a donation.

You liked the content or this article has helped and reduced the amount of time you have struggled with this issue? Please donate a few bucks so I can keep going with solving challenges.


0 Comments

Leave a Reply