Merge pull request #369 from eclipse/cd_issue161

fix problem with bad naming convention epackages
This commit is contained in:
Christian Dietrich 2017-06-29 13:22:43 +02:00 committed by GitHub
commit 43d30f2699
4 changed files with 92 additions and 23 deletions

View file

@ -11,6 +11,7 @@ import org.eclipse.xtext.xtext.generator.model.TypeReference
import org.eclipse.xtext.xtext.generator.util.GenModelUtil2 import org.eclipse.xtext.xtext.generator.util.GenModelUtil2
import static extension org.eclipse.xtext.GrammarUtil.* import static extension org.eclipse.xtext.GrammarUtil.*
import org.eclipse.emf.ecore.EClass
class Junit4Fragment2 extends AbstractStubGeneratingFragment { class Junit4Fragment2 extends AbstractStubGeneratingFragment {
@ -105,7 +106,7 @@ class Junit4Fragment2 extends AbstractStubGeneratingFragment {
val parseHelper = new TypeReference(testingPackage + ".util.ParseHelper") val parseHelper = new TypeReference(testingPackage + ".util.ParseHelper")
val test = new TypeReference("org.junit.Test") val test = new TypeReference("org.junit.Test")
val assert = new TypeReference("org.junit.Assert") val assert = new TypeReference("org.junit.Assert")
val rootType = new TypeReference(GenModelUtil2.getJavaTypeName(grammar.rules.head.type.classifier, grammar.eResource.resourceSet)) val rootType = new TypeReference(grammar.rules.head.type.classifier as EClass, grammar.eResource.resourceSet)
return fileAccessFactory.createXtendFile(exampleRuntimeTest, ''' return fileAccessFactory.createXtendFile(exampleRuntimeTest, '''
@«runWith»(«xtextRunner») @«runWith»(«xtextRunner»)
@«injectWith»(«injectorProvider») @«injectWith»(«injectorProvider»)

View file

@ -17,6 +17,7 @@ import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.EqualsHashCode import org.eclipse.xtend.lib.annotations.EqualsHashCode
import org.eclipse.xtext.xtext.generator.IXtextGeneratorLanguage import org.eclipse.xtext.xtext.generator.IXtextGeneratorLanguage
import org.eclipse.xtext.xtext.generator.util.GenModelUtil2 import org.eclipse.xtext.xtext.generator.util.GenModelUtil2
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
/** /**
* Reference to a Java type. Use this for automatic import of types in {@link JavaFileAccess} * Reference to a Java type. Use this for automatic import of types in {@link JavaFileAccess}
@ -102,7 +103,11 @@ class TypeReference {
new(EClass clazz, ResourceSet resourceSet) { new(EClass clazz, ResourceSet resourceSet) {
// the qualified name might be a nested type, e.g. jav.util.Map.Entry // the qualified name might be a nested type, e.g. jav.util.Map.Entry
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=483088 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=483088
this(getQualifiedName(clazz, resourceSet), null, false) this(getQualifiedName(clazz, resourceSet))
}
new(QualifiedClazzName qualifiedClazzName) {
this(qualifiedClazzName.packageName, qualifiedClazzName.className, null)
} }
new(EPackage epackage, ResourceSet resourceSet) { new(EPackage epackage, ResourceSet resourceSet) {
@ -139,18 +144,26 @@ class TypeReference {
qualifiedName.substring(packageName.length + 1, qualifiedName.length) qualifiedName.substring(packageName.length + 1, qualifiedName.length)
} }
private static def getQualifiedName(EClass clazz, ResourceSet resourceSet) { private static def QualifiedClazzName getQualifiedName(EClass clazz, ResourceSet resourceSet) {
if (clazz.EPackage.nsURI == 'http://www.eclipse.org/2008/Xtext') { if (clazz.EPackage.nsURI == 'http://www.eclipse.org/2008/Xtext') {
'org.eclipse.xtext.' + clazz.name new QualifiedClazzName('org.eclipse.xtext', clazz.name)
} else if (clazz.EPackage.nsURI == 'http://www.eclipse.org/emf/2002/Ecore') { } else if (clazz.EPackage.nsURI == 'http://www.eclipse.org/emf/2002/Ecore') {
if (clazz.instanceTypeName !== null) clazz.instanceTypeName.replace('$', '.') else 'org.eclipse.emf.ecore.' + clazz.name if (clazz.instanceTypeName !== null) {
val itn = clazz.instanceTypeName;
new QualifiedClazzName(itn.substring(0, itn.lastIndexOf('.')),
itn.substring(itn.lastIndexOf(".") + 1).replace("$", "."))
} else {
new QualifiedClazzName('org.eclipse.emf.ecore', clazz.name)
}
} else { } else {
GenModelUtil2.getGenClass(clazz, resourceSet).qualifiedInterfaceName new QualifiedClazzName(GenModelUtil2.getGenClass(clazz, resourceSet).genPackage.qualifiedPackageName,
GenModelUtil2.getGenClass(clazz, resourceSet).interfaceName)
} }
} }
private static def getQualifiedName(EPackage epackage, ResourceSet resourceSet) { private static def QualifiedClazzName getQualifiedName(EPackage epackage, ResourceSet resourceSet) {
GenModelUtil2.getGenPackage(epackage, resourceSet).qualifiedPackageInterfaceName new QualifiedClazzName(GenModelUtil2.getGenPackage(epackage, resourceSet).qualifiedPackageName,
GenModelUtil2.getGenPackage(epackage, resourceSet).packageInterfaceName)
} }
override toString() { override toString() {
@ -177,4 +190,12 @@ class TypeReference {
path + ".xtend" path + ".xtend"
} }
@FinalFieldsConstructor
static class QualifiedClazzName {
@Accessors(PUBLIC_GETTER)
val String packageName
@Accessors(PUBLIC_GETTER)
val String className
}
} }

View file

@ -4,6 +4,9 @@ import com.google.inject.Inject;
import com.google.inject.Injector; import com.google.inject.Injector;
import java.util.Collections; import java.util.Collections;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtend.lib.annotations.AccessorType; import org.eclipse.xtend.lib.annotations.AccessorType;
import org.eclipse.xtend.lib.annotations.Accessors; import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtend2.lib.StringConcatenationClient; import org.eclipse.xtend2.lib.StringConcatenationClient;
@ -22,7 +25,6 @@ import org.eclipse.xtext.xtext.generator.model.IXtextGeneratorFileSystemAccess;
import org.eclipse.xtext.xtext.generator.model.JavaFileAccess; import org.eclipse.xtext.xtext.generator.model.JavaFileAccess;
import org.eclipse.xtext.xtext.generator.model.ManifestAccess; import org.eclipse.xtext.xtext.generator.model.ManifestAccess;
import org.eclipse.xtext.xtext.generator.model.TypeReference; import org.eclipse.xtext.xtext.generator.model.TypeReference;
import org.eclipse.xtext.xtext.generator.util.GenModelUtil2;
@SuppressWarnings("all") @SuppressWarnings("all")
public class Junit4Fragment2 extends AbstractStubGeneratingFragment { public class Junit4Fragment2 extends AbstractStubGeneratingFragment {
@ -159,8 +161,9 @@ public class Junit4Fragment2 extends AbstractStubGeneratingFragment {
final TypeReference parseHelper = new TypeReference(_plus_2); final TypeReference parseHelper = new TypeReference(_plus_2);
final TypeReference test = new TypeReference("org.junit.Test"); final TypeReference test = new TypeReference("org.junit.Test");
final TypeReference assert_ = new TypeReference("org.junit.Assert"); final TypeReference assert_ = new TypeReference("org.junit.Assert");
String _javaTypeName = GenModelUtil2.getJavaTypeName(IterableExtensions.<AbstractRule>head(this.getGrammar().getRules()).getType().getClassifier(), this.getGrammar().eResource().getResourceSet()); EClassifier _classifier = IterableExtensions.<AbstractRule>head(this.getGrammar().getRules()).getType().getClassifier();
final TypeReference rootType = new TypeReference(_javaTypeName); ResourceSet _resourceSet = this.getGrammar().eResource().getResourceSet();
final TypeReference rootType = new TypeReference(((EClass) _classifier), _resourceSet);
TypeReference _exampleRuntimeTest = this.exampleRuntimeTest(); TypeReference _exampleRuntimeTest = this.exampleRuntimeTest();
StringConcatenationClient _client = new StringConcatenationClient() { StringConcatenationClient _client = new StringConcatenationClient() {
@Override @Override

View file

@ -14,8 +14,10 @@ import java.util.List;
import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EPackage; import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtend.lib.annotations.AccessorType;
import org.eclipse.xtend.lib.annotations.Accessors; import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtend.lib.annotations.EqualsHashCode; import org.eclipse.xtend.lib.annotations.EqualsHashCode;
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
import org.eclipse.xtext.xbase.lib.CollectionLiterals; import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Conversions; import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.Functions.Function1; import org.eclipse.xtext.xbase.lib.Functions.Function1;
@ -32,6 +34,31 @@ import org.eclipse.xtext.xtext.generator.util.GenModelUtil2;
@EqualsHashCode @EqualsHashCode
@SuppressWarnings("all") @SuppressWarnings("all")
public class TypeReference { public class TypeReference {
@FinalFieldsConstructor
public static class QualifiedClazzName {
@Accessors(AccessorType.PUBLIC_GETTER)
private final String packageName;
@Accessors(AccessorType.PUBLIC_GETTER)
private final String className;
@Pure
public String getPackageName() {
return this.packageName;
}
@Pure
public String getClassName() {
return this.className;
}
public QualifiedClazzName(final String packageName, final String className) {
super();
this.packageName = packageName;
this.className = className;
}
}
public static TypeReference typeRef(final String name, final TypeReference... arguments) { public static TypeReference typeRef(final String name, final TypeReference... arguments) {
return new TypeReference(name, (List<TypeReference>)Conversions.doWrapArray(arguments)); return new TypeReference(name, (List<TypeReference>)Conversions.doWrapArray(arguments));
} }
@ -137,7 +164,11 @@ public class TypeReference {
} }
public TypeReference(final EClass clazz, final ResourceSet resourceSet) { public TypeReference(final EClass clazz, final ResourceSet resourceSet) {
this(TypeReference.getQualifiedName(clazz, resourceSet), null, false); this(TypeReference.getQualifiedName(clazz, resourceSet));
}
public TypeReference(final TypeReference.QualifiedClazzName qualifiedClazzName) {
this(qualifiedClazzName.packageName, qualifiedClazzName.className, null);
} }
public TypeReference(final EPackage epackage, final ResourceSet resourceSet) { public TypeReference(final EPackage epackage, final ResourceSet resourceSet) {
@ -201,38 +232,51 @@ public class TypeReference {
return _xblockexpression; return _xblockexpression;
} }
private static String getQualifiedName(final EClass clazz, final ResourceSet resourceSet) { private static TypeReference.QualifiedClazzName getQualifiedName(final EClass clazz, final ResourceSet resourceSet) {
String _xifexpression = null; TypeReference.QualifiedClazzName _xifexpression = null;
String _nsURI = clazz.getEPackage().getNsURI(); String _nsURI = clazz.getEPackage().getNsURI();
boolean _equals = Objects.equal(_nsURI, "http://www.eclipse.org/2008/Xtext"); boolean _equals = Objects.equal(_nsURI, "http://www.eclipse.org/2008/Xtext");
if (_equals) { if (_equals) {
String _name = clazz.getName(); String _name = clazz.getName();
_xifexpression = ("org.eclipse.xtext." + _name); _xifexpression = new TypeReference.QualifiedClazzName("org.eclipse.xtext", _name);
} else { } else {
String _xifexpression_1 = null; TypeReference.QualifiedClazzName _xifexpression_1 = null;
String _nsURI_1 = clazz.getEPackage().getNsURI(); String _nsURI_1 = clazz.getEPackage().getNsURI();
boolean _equals_1 = Objects.equal(_nsURI_1, "http://www.eclipse.org/emf/2002/Ecore"); boolean _equals_1 = Objects.equal(_nsURI_1, "http://www.eclipse.org/emf/2002/Ecore");
if (_equals_1) { if (_equals_1) {
String _xifexpression_2 = null; TypeReference.QualifiedClazzName _xifexpression_2 = null;
String _instanceTypeName = clazz.getInstanceTypeName(); String _instanceTypeName = clazz.getInstanceTypeName();
boolean _tripleNotEquals = (_instanceTypeName != null); boolean _tripleNotEquals = (_instanceTypeName != null);
if (_tripleNotEquals) { if (_tripleNotEquals) {
_xifexpression_2 = clazz.getInstanceTypeName().replace("$", "."); TypeReference.QualifiedClazzName _xblockexpression = null;
{
final String itn = clazz.getInstanceTypeName();
String _substring = itn.substring(0, itn.lastIndexOf("."));
int _lastIndexOf = itn.lastIndexOf(".");
int _plus = (_lastIndexOf + 1);
String _replace = itn.substring(_plus).replace("$", ".");
_xblockexpression = new TypeReference.QualifiedClazzName(_substring, _replace);
}
_xifexpression_2 = _xblockexpression;
} else { } else {
String _name_1 = clazz.getName(); String _name_1 = clazz.getName();
_xifexpression_2 = ("org.eclipse.emf.ecore." + _name_1); _xifexpression_2 = new TypeReference.QualifiedClazzName("org.eclipse.emf.ecore", _name_1);
} }
_xifexpression_1 = _xifexpression_2; _xifexpression_1 = _xifexpression_2;
} else { } else {
_xifexpression_1 = GenModelUtil2.getGenClass(clazz, resourceSet).getQualifiedInterfaceName(); String _qualifiedPackageName = GenModelUtil2.getGenClass(clazz, resourceSet).getGenPackage().getQualifiedPackageName();
String _interfaceName = GenModelUtil2.getGenClass(clazz, resourceSet).getInterfaceName();
_xifexpression_1 = new TypeReference.QualifiedClazzName(_qualifiedPackageName, _interfaceName);
} }
_xifexpression = _xifexpression_1; _xifexpression = _xifexpression_1;
} }
return _xifexpression; return _xifexpression;
} }
private static String getQualifiedName(final EPackage epackage, final ResourceSet resourceSet) { private static TypeReference.QualifiedClazzName getQualifiedName(final EPackage epackage, final ResourceSet resourceSet) {
return GenModelUtil2.getGenPackage(epackage, resourceSet).getQualifiedPackageInterfaceName(); String _qualifiedPackageName = GenModelUtil2.getGenPackage(epackage, resourceSet).getQualifiedPackageName();
String _packageInterfaceName = GenModelUtil2.getGenPackage(epackage, resourceSet).getPackageInterfaceName();
return new TypeReference.QualifiedClazzName(_qualifiedPackageName, _packageInterfaceName);
} }
@Override @Override