Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Cross reference, SimpleNameProvider, and Package issue
Cross reference, SimpleNameProvider, and Package issue [message #1756119] Sun, 12 March 2017 06:12 Go to next message
Akira Tanaka is currently offline Akira TanakaFriend
Messages: 98
Registered: March 2010
Member
With Domainmodel example project with the following grammar modifications,

Grammar modifications (addition of CrossReference)
...
Feature:
	Property | CrossReference | Operation;
...
CrossReference:
	'ref' name=QualifiedName ':' type=[Entity|QualifiedName];
...


and IJvmModelInferrer modifications,
...
// added for CrossReference support
import org.eclipse.xtext.example.domainmodel.domainmodel.CrossReference
...
// added for CrossReference support
					// for cross references we create a field, a getter and a setter
					CrossReference : {
					  val field = f.toField(f.name, f.type.fullyQualifiedName.toString(".").typeRef)
					  members += field
					  members += f.toGetter(f.name, f.type.fullyQualifiedName.toString(".").typeRef)
					  members += f.toSetter(f.name, f.type.fullyQualifiedName.toString(".").typeRef)
					}
...


I was able to create the following model, including cross references and packages.
package A {
	entity Person {
		name : String
		ref worksFor : B.Boss
	}
}

package B {
	entity Boss {
		name : String
		ref staff : A.Person
	}
}


The generated code for Person looks like the following.
package A;

import B.Boss;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

@SuppressWarnings("all")
public class Person {
  public Person() {
  }
  
  public Person(final Procedure1<Person> initializer) {
    initializer.apply(this);
  }
  
  private String name;
  
  public String getName() {
    return this.name;
  }
  
  public void setName(final String name) {
    this.name = name;
  }
  
  private Boss worksFor;
  
  public Boss getWorksFor() {
    return this.worksFor;
  }
  
  public void setWorksFor(final Boss worksFor) {
    this.worksFor = worksFor;
  }
  
  @Override
  public String toString() {
    String result = new ToStringBuilder(this).addAllFields().toString();
    return result;
  }
}


We like this generated code including "import B.Boss", but would just like to write "Boss" in the model (i.e. without package name), which currently is "B.Boss." Is it possible?

To see the effect, I did an experiment. I added the following fragment code to DomainmodelRuntimeModule.java.
...
	@Override
	public Class<? extends org.eclipse.xtext.naming.IQualifiedNameProvider> bindIQualifiedNameProvider() {
	    return org.eclipse.xtext.naming.SimpleNameProvider.class;
	}
...


Then, model code became like the following
package A {
	entity Person {
		name : String
		ref worksFor : Boss
	}
}

package B {
	entity Boss {
		name : String
		ref staff : Person
	}
}


The model code was what I expected. But, generated code below was not.
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

@SuppressWarnings("all")
public class Person {
  public Person() {
  }
  
  public Person(final Procedure1<Person> initializer) {
    initializer.apply(this);
  }
  
  private String name;
  
  public String getName() {
    return this.name;
  }
  
  public void setName(final String name) {
    this.name = name;
  }
  
  private Boss worksFor;
  
  public Boss getWorksFor() {
    return this.worksFor;
  }
  
  public void setWorksFor(final Boss worksFor) {
    this.worksFor = worksFor;
  }
  
  @Override
  public String toString() {
    String result = new ToStringBuilder(this).addAllFields().toString();
    return result;
  }
}


That is, package declaration and package import were removed, although I declared packages in the model,

Is there a way to achieve my expectation (just write "Person" in model, and get generated code with package declaration and import statement and ), or is my expectation wrong?

Thank you for advice in advance.
Akira
Re: Cross reference, SimpleNameProvider, and Package issue [message #1756121 is a reply to message #1756119] Sun, 12 March 2017 08:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Well you need to adapt the inferrer to support that

f.type.fullyqualifedName Weill give you the simple name now

=> manually calc the real fqn there (or inject defaultdeclarativequalifiednameprovider instead of iqualifiednameprovider to the inferrer


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Cross reference, SimpleNameProvider, and Package issue [message #1756126 is a reply to message #1756121] Sun, 12 March 2017 15:18 Go to previous message
Akira Tanaka is currently offline Akira TanakaFriend
Messages: 98
Registered: March 2010
Member
Christian san,

Thank you very much! It works now. That was the missing piece.
Previous Topic:Integration in VS Code
Next Topic:Formatting Expressions
Goto Forum:
  


Current Time: Tue Apr 23 12:07:13 GMT 2024

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

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

Back to the top