I am trying to do the simplest example I can think of to use Xbase and the JvmModelInferrer, rather than writing a code generator. I've cut down the JVM language tutorial but I can't get correct Java code from an XExpression (or XBlockExpression). I've looked at answers like :-
How to JvmModelInferrer method body from XExpression and append boilerplate code
The specific error I am getting currently is that for an expression like 2+2, the code I generate is :-
return 2./* name is null */;
My grammar is :-
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
functions+=Function*
;
Function:
'function' name=ID 'body' exp=XBlockExpression
;
and my JvmModelInferrer is :-
def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(element.toClass("my.company.Functions")) [
for (function : element.functions) {
members += function.toMethod(function.name, typeRef(Object)) [
body = function.exp
]
}
]
}
For input :-
function TwoPlusTwo body {2+2}
The generated code is :-
package my.company;
public class Functions {
public java.lang.Object TwoPlusTwo() {
return 2./* name is null */;
}
}
Am I making some completely basic error or have some fundamental misunderstanding ?
I'm using Windows 10, Eclipse 2019-12, Xtext 2.20.0, Coretto JVM
Any help would be appreciated.
As suggested by Christian, it needs to have the correct libraries added to the project. The Five Steps to Your JVM Language tutorial says this, I had just forgotten to do it :-
In the new workbench, create a Java project (File → New → Project… → Java Project). Xbase relies on a small runtime library on the class path. To add this, right-click on the project and go to Java Build Path → Libraries → Add Library and choose Xtend Library.