问题
In the Inferring a JVM Model section of the Xtext documentation (http://www.eclipse.org/Xtext/documentation.html#_17) it starts by saying: "In many cases, you will want your DSLs concepts to be usable as Java elements. E.g. an Entity will become a Java class and should be usable as such".
In the example above, how can I use the generated Entity class outside of xbase, i.e. in real Java code in a different project to the xtext one?
What I am essentially asking is if the Java classes created my by the model Inferrer can actually be used as real java classes, which can have their methods called and fields accessed from java code, in an altogether different project, and if so how this can be done?
My going through the documentation has lead me to fear that the generated "Java classes" are only Xbase types, only referenceabe in an xtext context, and are not therefore real java classes...
回答1:
The Xbase compiler can compile all Xbase expressions to plain Java code usable everywhere where Java codes are available.
If you add your own elements to the language, you have to extend the generator to also support these elements - for this reason you define your own JVMModelInferrer.
The basic Xtext compiler then executes the JVMModelInferrer, calculates the JVM model that might (or might not) contain Xbase expressions as well; then this JVM model can be generated into a Java-compilable (thus Java-reusable) code.
If you want to test this functionality, simply generate the Xtext Domain Model example (available from the New... wizards in Xtext/Examples category), and evaluate the results: when you edit your domain model, Xtext automatically generates the usable Java code (if the required dependencies are set).
来源:https://stackoverflow.com/questions/11260216/how-to-use-xtext-inferred-jvm-model-outside-of-xtext-project