In my dsl I want to create a field which type is a type of generic argument of an XOrExpression.
Here is a simplified example of what I want to achieve.
Using this grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
greetings+=Greeting*
;
Greeting:
'test' name = ID '=' constructor = XOrExpression
;
For the following input:
test a = new java.util.ArrayList<Integer>()
I want to get the following generated code:
public class MyGreetings {
private ArrayList<Integer> a = new ArrayList<Integer>();
private Integer gen__a;
}
I.e. if expression is of type ArrayList<Integer> I want to create a uninitialized field of type Integer.
I use typeReferences.getArgument() call in inferrer to retrieve type arguments.
For the simplicity of an example I assume we always have one generic argument.
My inferrer looks like this:
class MyDslJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
@Inject TypeReferences typeReferences;
def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(element.toClass("my.company.greeting.MyGreetings")) [
for (greeting : element.greetings) {
members += greeting.toField(greeting.name, greeting.constructor.inferredType) [
initializer = greeting.constructor
]
val genericsType = typeReferences.getArgument(greeting.constructor.inferredType, 0)
members += greeting.toField("gen__" + greeting.name, genericsType)
}
]
}
}
The problem is, exception occurs when I try to access inferred type to get arguments in this line:
val genericsType = typeReferences.getArgument(greeting.constructor.inferredType, 0)
Here is the log of exception:
java.lang.IllegalStateException: equivalent could not be computed
at org.eclipse.xtext.xbase.typesystem.InferredTypeIndicator.getTypeReference(InferredTypeIndicator.java:83)
at org.eclipse.xtext.xtype.impl.XComputedTypeReferenceImplCustom.getEquivalent(XComputedTypeReferenceImplCustom.java:46)
at org.eclipse.xtext.common.types.impl.JvmSpecializedTypeReferenceImplCustom.getType(JvmSpecializedTypeReferenceImplCustom.java:23)
at org.eclipse.xtext.common.types.util.TypeReferences.getArgument(TypeReferences.java:146)
at org.xtext.example.mydsl.jvmmodel.MyDslJvmModelInferrer.lambda$0(MyDslJvmModelInferrer.java:48)
I don't quite get why equivalent could not be computed. Without the second part my model infers successfully and I get the following:
private ArrayList<Integer> a = new ArrayList<Integer>();
So the type is computed ok. But somehow I cannot yet access it.
Can anyone please explain what am I doing wrong? Is it possible to achieve what I want at all?
Thanks in advance.
[Updated on: Wed, 27 April 2016 08:35]
Report message to a moderator