Specify supertype of JvmTypeReference in Xtext grammar

202 Views Asked by At

Is it possible to specify a required type (e.g. interface) for a JvmTypeReference in an Xtext grammar?

Example

Instantiation:
    ('new' type=JvmTypeReference (params=Params)?)
;

Where type=JvmTypeReference must be an instanceof some specified class.

2

There are 2 best solutions below

0
On

You could implement a reference to a JvmConstructor!

Instantiation:
    'new' type=[JvmConstructor|QualifiedName] params=Params?;

You should use an XimportSection in your grammar then you can write a scope provider where you can scope directly to any constructor.

1
On

No this is not possible in the grammar, but you can achieve the desired behavior by means of a customized scope provider or a proposal provider, see this blog post: https://kthoms.wordpress.com/2012/03/14/how-to-limit-proposed-java-types-to-implementors-of-an-interface/