问题
In an Xtext grammar I can include an Xbase code block using the XExpression
/XBlockExpression
types. How would I go to do the same with a Java code block? The included java should later be included in the Java generated from the DSL constructs.
回答1:
You can do this now with JBase (https://github.com/LorenzoBettini/jbase). JBase was created specifically for the use case you described (see https://dl.acm.org/citation.cfm?id=2972217) and can be incorporated with minor changes by extending JBase instead of XBase.
回答2:
It is not possible to directly include Java code blocks (unless you are prepared to write an entire Java grammar and corresponding tooling support, e.g. variable resolution or code generation).
However, Xbase code blocks can be translated into Java code blocks using built-in Xtext services in the model inferrer or using the XbaseCompiler. It is also possible to simply interpret it with the XbaseInterpreter class.
Finally, if you really need the Java code blocks, you could write them in .java files, and refer to them using JvmIdentifiableElements.
来源:https://stackoverflow.com/questions/12196448/how-to-include-java-code-block-in-xtext-dsl