0

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...

Marcus Mathioudakis
  • 837
  • 1
  • 6
  • 19

1 Answers1

2

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).

Zoltán Ujhelyi
  • 13,788
  • 2
  • 32
  • 37
  • Thanks for your response, What do you mean by "it automatically generates usable java code if the required dependencies are set"? Which dependencies? Are you suggesting it will generate code without using the generator.xtend (but just using the JVMModelInferrer),and if so where will this code be generated? – Marcus Mathioudakis Jul 02 '12 at 08:10
  • You have to do a minimal setup as Xbase is only usable as an expression language, so classes are not set up. The only thing you have to do is to determine what are your model classes that represent a class, or a method in the generated code - the method body can be created from Xbase expressions. See http://www.eclipse.org/Xtext/documentation.html##_8 for details. – Zoltán Ujhelyi Jul 02 '12 at 08:13
  • Many thanks for your answer. although I had seen this approach, I wasn't aware that it was also generating code to the src-gen folder. So all I need to do is load the classes from this folder. – Marcus Mathioudakis Jul 02 '12 at 13:32