Cross reference, SimpleNameProvider, and Package issue [message #1756119] |
Sun, 12 March 2017 01:12  |
Eclipse User |
|
|
|
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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03730 seconds