Skip to content

Commit 20e2bea

Browse files
committedNov 25, 2017
Fix #66 : EClass using Map$Entry should not be generated
Signed-off-by: Olivier Prouvost <[email protected]>
1 parent 020d873 commit 20e2bea

File tree

8 files changed

+134
-15
lines changed

8 files changed

+134
-15
lines changed
 

‎com.opcoach.genmodeladdon.core.test/src/com/opcoach/genmodeladdon/core/test/GenModelAddonTestCase.java

+42-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ protected IProject getSampleProject()
6666

6767
return sampleProject;
6868
}
69-
69+
7070
/**
71-
* This method checks if the specified content is in the file
71+
* This method checks if a string occures in a File.
7272
*
7373
* @param path
7474
* the path expressed relative to project :
@@ -77,7 +77,7 @@ protected IProject getSampleProject()
7777
* a string like : "ProjectImpl extends MProjectImpl"
7878
* @return
7979
*/
80-
void assertFileContains(String path, String content)
80+
boolean isStringPresentInFile(String path, String content)
8181
{
8282
boolean found = false;
8383
BufferedReader fr = null;
@@ -113,10 +113,49 @@ void assertFileContains(String path, String content)
113113
}
114114
}
115115

116+
return found;
117+
118+
}
119+
120+
121+
/**
122+
* This method checks if the specified content is in the file
123+
*
124+
* @param path
125+
* the path expressed relative to project :
126+
* "src/impl/ProjectImpl.java" for instance
127+
* @param content
128+
* a string like : "ProjectImpl extends MProjectImpl"
129+
* @return
130+
*/
131+
void assertFileContains(String path, String content)
132+
{
133+
boolean found = isStringPresentInFile(path, content);
134+
116135
if (!found)
117136
fail("The file '" + path + "' should contain the string '" + content + "' but it was not found.");
118137

119138
}
139+
140+
/**
141+
* This method checks if the specified content is not present in the file
142+
*
143+
* @param path
144+
* the path expressed relative to project :
145+
* "src/impl/ProjectImpl.java" for instance
146+
* @param content
147+
* a string like : "ProjectImpl extends MProjectImpl"
148+
* @return
149+
*/
150+
void assertFileDoesNotContain(String path, String content)
151+
{
152+
boolean found = isStringPresentInFile(path, content);
153+
154+
if (found)
155+
fail("The file '" + path + "' should not contain the string '" + content + "' but it was found.");
156+
157+
}
158+
120159

121160
/**
122161
* This method checks a file exists

‎com.opcoach.genmodeladdon.core.test/src/com/opcoach/genmodeladdon/core/test/TestInterfaceGeneration.java

+34
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package com.opcoach.genmodeladdon.core.test;
22

3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
7+
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
8+
import org.eclipse.emf.ecore.EClass;
39
import org.junit.Test;
410

11+
import com.opcoach.genmodeladdon.core.GenerateCommon;
12+
513
public class TestInterfaceGeneration extends GenModelAddonTestCase
614
{
715

@@ -244,6 +252,32 @@ public void enumTypeMustBeCastInESetMethod()
244252
assertFileContains("src-gen/com/opcoach/project/impl/MProjectImpl.java", "setType((TypeProject)newValue);");
245253
}
246254

255+
256+
// -----------------------------------------------------------------------
257+
// ------------------- Tests for issue #66 / https://github.com/opcoach/genModelAddon/issues/66
258+
// -----------------------------------------------------------------------
259+
@Test
260+
public void classWithMapInstanceNameMustNotBeGenerated()
261+
{
262+
GenModel gm = getGenModel(PROJECT_GENMODEL);
263+
// Check EClass IntToDoubleMap exists.
264+
GenClass gc = findGenClass(gm, "IntToDoubleMap");
265+
EClass c = (gc == null) ? null : gc.getEcoreClass();
266+
267+
assertNotNull("The EClass IntToDoubleMap is present in test model", c);
268+
assertTrue("The IntToDoubleMap instance type name must be java.util.Map$Entry", GenerateCommon.isMapType(c));
269+
270+
// Now the file for this class must not be generated...
271+
assertFileNotExists("src/com/opcoach/project/IntToDoubleMap.java");
272+
assertFileNotExists("src/com/opcoach/project/impl/IntToDoubleMapImpl.java");
273+
274+
// Check that factory generated file does not contain the IntToDoubleMap class
275+
assertFileDoesNotContain("src/com/opcoach/project/ProjectFactory.java", "IntToDoubleMap");
276+
assertFileDoesNotContain("src/com/opcoach/project/impl/ProjectFactoryImpl.java", "IntToDoubleMap");
277+
278+
}
279+
280+
247281

248282

249283
}

‎com.opcoach.genmodeladdon.core/src/com/opcoach/genmodeladdon/core/GenerateCommon.xtend

+14
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import org.eclipse.core.runtime.Path
88
import org.eclipse.core.runtime.Platform
99
import org.eclipse.core.runtime.QualifiedName
1010
import org.eclipse.core.runtime.Status
11+
import org.eclipse.emf.codegen.ecore.genmodel.GenClass
1112
import org.eclipse.emf.codegen.ecore.genmodel.GenModel
1213
import org.eclipse.emf.common.util.URI
14+
import org.eclipse.emf.ecore.EClassifier
1315
import org.osgi.framework.FrameworkUtil
1416

1517
/** A class to provide some generation common methods */
@@ -110,4 +112,16 @@ class GenerateCommon implements GMAConstants {
110112
// Path is between projectName and model Name !
111113
return modelDir
112114
}
115+
116+
117+
def static isMapType(EClassifier c) {
118+
// See : GenBaseImpl::isJavaUtilMapEntry(String name)
119+
val name = c.instanceClassName
120+
return "java.util.Map.Entry".equals(name) || "java.util.Map$Entry".equals(name)
121+
}
122+
123+
def static isMapType(GenClass c) {
124+
// See : GenBaseImpl::isJavaUtilMapEntry(String name)
125+
isMapType(c.ecoreClass)
126+
}
113127
}

‎com.opcoach.genmodeladdon.core/src/com/opcoach/genmodeladdon/core/GenerateDevStructure.xtend

+6-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class GenerateDevStructure {
128128
// println("Generate classes in : " + srcAbsolutePath)
129129
// println("Generate interfaces in : " + interfaceAbsolutePath)
130130

131-
for (c : gp.genClasses.filter[!isDynamic]) {
131+
for (c : gp.genClasses.filter[!isDynamic].filter[p | !GenerateCommon.isMapType(p)]) {
132132
if (!c.isInterface)
133133
generateOverriddenClass(c, srcAbsolutePath) // Can still generate abstract classes
134134
generateOverriddenInterface(c, interfaceAbsolutePath)
@@ -153,6 +153,8 @@ class GenerateDevStructure {
153153
for (sp : gp.subGenPackages)
154154
sp.generateDevStructure
155155
}
156+
157+
156158

157159
/** add the srcDir as a source directory in the java project, if it is not yet added */
158160
def private setFolderAsSourceFolder(IProject proj, String srcDir) {
@@ -385,7 +387,7 @@ class GenerateDevStructure {
385387
*/
386388
«gp.computeFactoryInterfaceName» eINSTANCE = «gp.computeFactoryClassName».init();
387389

388-
«FOR gc : gp.genClasses.filter[!isDynamic].filter[!isAbstract]»
390+
«FOR gc : gp.genClasses.filter[!isDynamic].filter[!isAbstract].filter[p | !GenerateCommon.isMapType(p)]»
389391
«gc.generateFactoryDef»
390392
«ENDFOR»
391393
}
@@ -425,7 +427,7 @@ class GenerateDevStructure {
425427

426428
import org.eclipse.emf.ecore.plugin.EcorePlugin;
427429

428-
«FOR gc : gp.genClasses.filter[!isDynamic].filter[!isAbstract]»
430+
«FOR gc : gp.genClasses.filter[!isDynamic].filter[!isAbstract].filter[p | !GenerateCommon.isMapType(p)]»
429431
import «gp.computePackageNameForInterfaces».«gc.computeInterfaceFilename»;
430432
«ENDFOR»
431433
import «gp.computePackageNameForInterfaces».«gp.computeFactoryInterfaceName»;
@@ -449,7 +451,7 @@ class GenerateDevStructure {
449451
return new «gp.computeFactoryClassName»();
450452
}
451453

452-
«FOR gc : gp.genClasses.filter[!isDynamic].filter[!isAbstract]»
454+
«FOR gc : gp.genClasses.filter[!isDynamic].filter[!isAbstract].filter[p | !GenerateCommon.isMapType(p)]»
453455
«gc.generateCreateMethod»
454456
«ENDFOR»
455457
}

‎com.opcoach.genmodeladdon.core/src/com/opcoach/genmodeladdon/core/genmodel/GMATransform.xtend

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.util.function.Consumer
99
import org.eclipse.emf.codegen.ecore.genmodel.GenBase
1010
import org.eclipse.emf.codegen.ecore.genmodel.GenModel
1111
import org.eclipse.emf.ecore.EClass
12+
import org.eclipse.emf.ecore.EClassifier
1213
import org.eclipse.emf.ecore.EPackage
1314
import org.eclipse.emf.ecore.EcorePackage
1415

@@ -87,11 +88,13 @@ class GMATransform implements GMAConstants {
8788
isInit = false
8889
}
8990

91+
92+
9093
def void computeNames(EPackage p) {
9194
// Do not compute names for emf2002Ecore
9295
if (!EcorePackage::eNS_URI.equals(p.nsURI)) {
9396
for (c : p.getEClassifiers()) {
94-
if ((c instanceof EClass) && !c.name.endsWith("Package")) {
97+
if ((c instanceof EClass) && !c.name.endsWith("Package") && !GenerateCommon.isMapType(c)) {
9598
val devIntName = MessageFormat.format(devInterfaceNamePattern, c.name)
9699
val genIntName = MessageFormat.format(genInterfaceNamePattern, c.name)
97100
// println("Put : " + genIntName + "," + devIntName);

‎com.opcoach.genmodeladdon.core/xtend-gen/com/opcoach/genmodeladdon/core/GenerateCommon.java

+11
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import org.eclipse.core.runtime.Platform;
1313
import org.eclipse.core.runtime.QualifiedName;
1414
import org.eclipse.core.runtime.Status;
15+
import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
1516
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
1617
import org.eclipse.emf.common.util.URI;
18+
import org.eclipse.emf.ecore.EClassifier;
1719
import org.eclipse.emf.ecore.resource.Resource;
1820
import org.eclipse.xtext.xbase.lib.Exceptions;
1921
import org.osgi.framework.Bundle;
@@ -144,4 +146,13 @@ public static String getModelPathFromStringURI(final String projectName, final S
144146
}
145147
return modelDir;
146148
}
149+
150+
public static boolean isMapType(final EClassifier c) {
151+
final String name = c.getInstanceClassName();
152+
return ("java.util.Map.Entry".equals(name) || "java.util.Map$Entry".equals(name));
153+
}
154+
155+
public static boolean isMapType(final GenClass c) {
156+
return GenerateCommon.isMapType(c.getEcoreClass());
157+
}
147158
}

‎com.opcoach.genmodeladdon.core/xtend-gen/com/opcoach/genmodeladdon/core/GenerateDevStructure.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ public void generateDevStructure(final GenPackage gp) {
164164
boolean _isDynamic = it.isDynamic();
165165
return Boolean.valueOf((!_isDynamic));
166166
};
167-
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function);
167+
final Function1<GenClass, Boolean> _function_1 = (GenClass p) -> {
168+
boolean _isMapType = GenerateCommon.isMapType(p);
169+
return Boolean.valueOf((!_isMapType));
170+
};
171+
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function), _function_1);
168172
for (final GenClass c : _filter) {
169173
{
170174
boolean _isInterface = c.isInterface();
@@ -553,7 +557,11 @@ public CharSequence generateInterfaceFactoryContent(final GenPackage gp) {
553557
boolean _isAbstract = it.isAbstract();
554558
return Boolean.valueOf((!_isAbstract));
555559
};
556-
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function), _function_1);
560+
final Function1<GenClass, Boolean> _function_2 = (GenClass p) -> {
561+
boolean _isMapType = GenerateCommon.isMapType(p);
562+
return Boolean.valueOf((!_isMapType));
563+
};
564+
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function), _function_1), _function_2);
557565
for(final GenClass gc : _filter) {
558566
_builder.append("\t");
559567
CharSequence _generateFactoryDef = this.generateFactoryDef(gc);
@@ -659,7 +667,11 @@ public CharSequence generateClassFactoryContent(final GenPackage gp) {
659667
boolean _isAbstract = it.isAbstract();
660668
return Boolean.valueOf((!_isAbstract));
661669
};
662-
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function), _function_1);
670+
final Function1<GenClass, Boolean> _function_2 = (GenClass p) -> {
671+
boolean _isMapType = GenerateCommon.isMapType(p);
672+
return Boolean.valueOf((!_isMapType));
673+
};
674+
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function), _function_1), _function_2);
663675
for(final GenClass gc : _filter) {
664676
_builder.append("import ");
665677
String _computePackageNameForInterfaces = this.computePackageNameForInterfaces(gp);
@@ -750,15 +762,19 @@ public CharSequence generateClassFactoryContent(final GenPackage gp) {
750762
_builder.append("\t");
751763
_builder.newLine();
752764
{
753-
final Function1<GenClass, Boolean> _function_2 = (GenClass it) -> {
765+
final Function1<GenClass, Boolean> _function_3 = (GenClass it) -> {
754766
boolean _isDynamic = it.isDynamic();
755767
return Boolean.valueOf((!_isDynamic));
756768
};
757-
final Function1<GenClass, Boolean> _function_3 = (GenClass it) -> {
769+
final Function1<GenClass, Boolean> _function_4 = (GenClass it) -> {
758770
boolean _isAbstract = it.isAbstract();
759771
return Boolean.valueOf((!_isAbstract));
760772
};
761-
Iterable<GenClass> _filter_1 = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function_2), _function_3);
773+
final Function1<GenClass, Boolean> _function_5 = (GenClass p) -> {
774+
boolean _isMapType = GenerateCommon.isMapType(p);
775+
return Boolean.valueOf((!_isMapType));
776+
};
777+
Iterable<GenClass> _filter_1 = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function_3), _function_4), _function_5);
762778
for(final GenClass gc_1 : _filter_1) {
763779
_builder.append("\t");
764780
CharSequence _generateCreateMethod = this.generateCreateMethod(gc_1);

‎com.opcoach.genmodeladdon.core/xtend-gen/com/opcoach/genmodeladdon/core/genmodel/GMATransform.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void computeNames(final EPackage p) {
119119
if (_not) {
120120
EList<EClassifier> _eClassifiers = p.getEClassifiers();
121121
for (final EClassifier c : _eClassifiers) {
122-
if (((c instanceof EClass) && (!c.getName().endsWith("Package")))) {
122+
if ((((c instanceof EClass) && (!c.getName().endsWith("Package"))) && (!GenerateCommon.isMapType(c)))) {
123123
final String devIntName = MessageFormat.format(this.devInterfaceNamePattern, c.getName());
124124
final String genIntName = MessageFormat.format(this.genInterfaceNamePattern, c.getName());
125125
this.devNames.put(genIntName, devIntName);

0 commit comments

Comments
 (0)
Please sign in to comment.