Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to get type of generic arguments of inferred type?
How to get type of generic arguments of inferred type? [message #1730629] Wed, 27 April 2016 08:32 Go to next message
Pavel Bogachev is currently offline Pavel BogachevFriend
Messages: 3
Registered: November 2014
Junior Member
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 ""


	'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("")) [

   			for (greeting : element.greetings) {
   				members +=  greeting.toField(, greeting.constructor.inferredType) [
   					initializer = greeting.constructor

				val genericsType = typeReferences.getArgument(greeting.constructor.inferredType, 0)
				members += greeting.toField("gen__" +, 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(
	at org.eclipse.xtext.xtype.impl.XComputedTypeReferenceImplCustom.getEquivalent(
	at org.eclipse.xtext.common.types.impl.JvmSpecializedTypeReferenceImplCustom.getType(
	at org.eclipse.xtext.common.types.util.TypeReferences.getArgument(
	at org.xtext.example.mydsl.jvmmodel.MyDslJvmModelInferrer.lambda$0(

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

Re: How to get type of generic arguments of inferred type? [message #1730830 is a reply to message #1730629] Thu, 28 April 2016 19:12 Go to previous message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
You receive the error because you cannot resolve types (which happens when you call getArgument, as this is an eager operation) while building the JVM model.

Did you try to implement my own InferredTypeIndicator and override getTypeReference to return the type argument? The use this in your own variant of inferredType. Not sure whether this works...

Get professional support from the Xtext committers at
Previous Topic:How to launch web editor from eclipse?
Next Topic:Understand grammar changes to address content assist issue.
Goto Forum:

Current Time: Sun Sep 27 19:40:45 GMT 2020

Powered by FUDForum. Page generated in 0.02072 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top