Home > Xtext > IQualifiedNameProviders in Xtext 2.0

IQualifiedNameProviders in Xtext 2.0

Xtext 2.0 come with a change to the IQualifiedNameProvider interface. This interface is used to calculate a name for EObjects. The Name is used in Xtext’s index, for cross referencing and much more.

public interface IQualifiedNameProvider extends Function<EObject, QualifiedName> {

	QualifiedName getFullyQualifiedName(EObject obj);


Here we see the API change: There is a rename of the getQualifiedName method to getFullyQualifedName. In Xtext 1.0.x the qualified name was a simple String, now it is a wrapper class that holds the segements of the qualified name.

There are two default implementations for a IQualifiedNameProviderSimpleNameProvider and DefaultDeclarativeQualifiedNameProvider. Consider we have a grammar like

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

	"package" name=ID "{"

	"element" name=ID

and a sample model like

package TestPackage {
	element A
	element B

Then with SimpleNameProvider we would have following qualified names.

  • TestPackage for the package
  • A and B for the elements

And with DefaultDeclarativeQualifiedNameProvider we would have following qualified names.

  • TestPackage for the package
  • TestPackage.A and TestPackage.B for the elements
Both Providers take the name EAttribute of our Package and Element to do the calculation. The DefaultDeclarativeQualifiedNameProvider  uses the Elements parents qualified name too. (a fully qualified name ;-))
But it won’t work e.g. if our grammar would look like
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

	"package" name=ID "{"

	"element" id=ID
with Element having and id and not a name. We can easily change this by creating and binding our own IQualifiedNameProvider e.g. by extending DefaultDeclarativeQualifiedNameProvider
package org.xtext.example.mydsl;

import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.xtext.example.mydsl.myDsl.Element;
import org.xtext.example.mydsl.myDsl.Package;

public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider{

	QualifiedName qualifiedName(Element e) {
		Package p = (Package) e.eContainer();
		return QualifiedName.create(p.getName(), e.getId());


We simply write a method qualifiedName that is called from the polymorpthic dispatcher when calculating the name of an Element

package org.xtext.example.mydsl;

import org.eclipse.xtext.naming.IQualifiedNameProvider;

 * Use this class to register components to be used at runtime / without the Equinox extension registry.
public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {

	public Class<? extends IQualifiedNameProvider> bindIQualifiedNameProvider() {
		return MyDslQNP.class;

About these ads
Categories: Xtext Tags: ,
  1. caner
    July 19, 2011 at 13:47

    Hello Christian
    Thank you so much for this great expression

    Look forward to read more about Xtext 2.0 on your blog

  2. gch
    October 27, 2011 at 05:22

    well written und precise! gch

  3. Mischa
    April 7, 2012 at 21:04

    Hi Chrisitan,

    thanks allot. Your blog help avoiding a lot of trouble casued by ‘out-of-date’.

    One needs to update the XText-Grammar docu which still describes the frist “ID” terminal rule used for as key for linking. As described in your blog it’s always just the “name” feature if available like hard coded in the DefaultDeclarativeQualifiedNameProvider.

    The ‘out-of-date’ docu section can be found here:


    • April 8, 2012 at 05:21

      Hi, i guess the doc is talking only about the left (grammar) side of cross references and wants to state that ref=[Type] is short for ref=[Type|ID]

  4. Stefan
    May 8, 2014 at 12:03

    Hi :)

    I am creating a language from which I want to reference elements of a UML model. You have another post about this situation, however I am having problem with qualified names, so I am asking this here.

    So, I have extended and binded DefaultDeclarativeQualifiedNameProvider and overrided its methods for getting qualified names of UML elements.

    On purpose, from each getQualifiedName() method for UML types, I return the simple name of the element (e.g. Product, and not webstore.Product) as I don’t want to have fully qualified names everywhere in my language, but to use imported namespaces..

    Now this works perfect in editor, but when I want to load a model of my language, I get poblems with loading referred UML elements (some names clash since it calls the getQualifiedName() again which gives only simple names).

    How do I find a way out of this? I have searched so much on internet and in documentation (which is sooo short to my opinion) without any luck..

    Is there a way to have more then one QualifiedNameProvider binded, one for editor and one for loading the model?

    Thank you so much in advance.


    • May 8, 2014 at 18:57

      yes you can bind the iqualified name provider in the runtime module and in the ui module but i dont get your problem. what is your exact problem loading?!?

  5. Stefan
    May 8, 2014 at 13:40

    @Edit of previous post:

    What I now understand is that when I call resource.resolveAll() on the resource containing my model with references to UML (actually, for the purpose of supporting more than just UML I am referring to EObject from Ecore), for each UML element it finds an EObject with a given simple name, which then goes wrong of course..

    So what I would really need is that the reference to the element get serialized as a fully qualified name, and only simple name is presented in the editor.. How to do this?

    I hope my explanation is not to complicated :)

    • May 8, 2014 at 18:58

      Standaline you have to add all resources to the resourceset. the dsl resources and the uml resources.
      and you have to make sure the resourceserviceprovider is initited (e.g. using the umlsupport class)

  6. Stefan
    May 9, 2014 at 09:53

    I think probably I am trying to solve my problem using the wrong approach :)

    This is the problem: When using import statements for referencing elements from another DSL, only references of the top level (those which are at the same level as import statements) have the benefit from those import statements.

    I need to nest references to the other DSL within the elements of my DSL. So for example:

    import somePackage.*

    myDslElement reference SomeClass { attribute SomeClass.someAttribute }

    So in this example I had to use SomeClass.someAttribute, in order for resolving the references to work.. How do I enable nesting the references and then make a qualified name of a reference based on its nesting?

    I could have used instead:

    import somePackage.*
    import somePackage.SomeClass.*

    myDslElement reference SomeClass { attribute someAttribute }

    But this is an ugly solution, and it also doesn’t work if another class, e.g. SomeClassB has an attribute with the same name someAttribute..

    I thought QUalifiedNameProvider can solve this, but maybe I am suppose to do it using some other approach..

    Thanks so much for replying so far! :)

  7. Stefan
    May 9, 2014 at 09:58

    p.s. In the example above I know that someAttribute has to be an attribute of the SomeClass, thus I want to use this knowledge and build the complete qualifiedName of someAttribute, and not to directly specify this in my model..

  8. Stefan
    June 22, 2014 at 11:26

    Hi Christian :)
    I am still stuck with this issue. and I think its an important one..

    As I understood from your example in the post about mixing UML and Xtext DSL,
    it looks like you anyway need to use complete qualified names in order for resource resolving to work..

    How could I reconstruct qualified name and where exactly in the code could I do this, based on the structure of my DSL model, not only by having access to the referenced UML model in the qualified name provider, as you shown here?

    Please help :)

    • June 22, 2014 at 20:59

      i dont really get your problem. you may post all what is needed to reproduce in xtexts eclipse forum

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: