Skip to content

Commit

Permalink
Fix #66 : EClass using Map$Entry should not be generated
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Prouvost <olivier.prouvost@opcoach.com>
  • Loading branch information
opcoach committed Nov 25, 2017
1 parent 020d873 commit 20e2bea
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 15 deletions.
Expand Up @@ -66,9 +66,9 @@ protected IProject getSampleProject()

return sampleProject;
}

/**
* This method checks if the specified content is in the file
* This method checks if a string occures in a File.
*
* @param path
* the path expressed relative to project :
Expand All @@ -77,7 +77,7 @@ protected IProject getSampleProject()
* a string like : "ProjectImpl extends MProjectImpl"
* @return
*/
void assertFileContains(String path, String content)
boolean isStringPresentInFile(String path, String content)
{
boolean found = false;
BufferedReader fr = null;
Expand Down Expand Up @@ -113,10 +113,49 @@ void assertFileContains(String path, String content)
}
}

return found;

}


/**
* This method checks if the specified content is in the file
*
* @param path
* the path expressed relative to project :
* "src/impl/ProjectImpl.java" for instance
* @param content
* a string like : "ProjectImpl extends MProjectImpl"
* @return
*/
void assertFileContains(String path, String content)
{
boolean found = isStringPresentInFile(path, content);

if (!found)
fail("The file '" + path + "' should contain the string '" + content + "' but it was not found.");

}

/**
* This method checks if the specified content is not present in the file
*
* @param path
* the path expressed relative to project :
* "src/impl/ProjectImpl.java" for instance
* @param content
* a string like : "ProjectImpl extends MProjectImpl"
* @return
*/
void assertFileDoesNotContain(String path, String content)
{
boolean found = isStringPresentInFile(path, content);

if (found)
fail("The file '" + path + "' should not contain the string '" + content + "' but it was found.");

}


/**
* This method checks a file exists
Expand Down
@@ -1,7 +1,15 @@
package com.opcoach.genmodeladdon.core.test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
import org.eclipse.emf.ecore.EClass;
import org.junit.Test;

import com.opcoach.genmodeladdon.core.GenerateCommon;

public class TestInterfaceGeneration extends GenModelAddonTestCase
{

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


// -----------------------------------------------------------------------
// ------------------- Tests for issue #66 / https://github.com/opcoach/genModelAddon/issues/66
// -----------------------------------------------------------------------
@Test
public void classWithMapInstanceNameMustNotBeGenerated()
{
GenModel gm = getGenModel(PROJECT_GENMODEL);
// Check EClass IntToDoubleMap exists.
GenClass gc = findGenClass(gm, "IntToDoubleMap");
EClass c = (gc == null) ? null : gc.getEcoreClass();

assertNotNull("The EClass IntToDoubleMap is present in test model", c);
assertTrue("The IntToDoubleMap instance type name must be java.util.Map$Entry", GenerateCommon.isMapType(c));

// Now the file for this class must not be generated...
assertFileNotExists("src/com/opcoach/project/IntToDoubleMap.java");
assertFileNotExists("src/com/opcoach/project/impl/IntToDoubleMapImpl.java");

// Check that factory generated file does not contain the IntToDoubleMap class
assertFileDoesNotContain("src/com/opcoach/project/ProjectFactory.java", "IntToDoubleMap");
assertFileDoesNotContain("src/com/opcoach/project/impl/ProjectFactoryImpl.java", "IntToDoubleMap");

}




}
Expand Up @@ -8,8 +8,10 @@ import org.eclipse.core.runtime.Path
import org.eclipse.core.runtime.Platform
import org.eclipse.core.runtime.QualifiedName
import org.eclipse.core.runtime.Status
import org.eclipse.emf.codegen.ecore.genmodel.GenClass
import org.eclipse.emf.codegen.ecore.genmodel.GenModel
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EClassifier
import org.osgi.framework.FrameworkUtil

/** A class to provide some generation common methods */
Expand Down Expand Up @@ -110,4 +112,16 @@ class GenerateCommon implements GMAConstants {
// Path is between projectName and model Name !
return modelDir
}


def static isMapType(EClassifier c) {
// See : GenBaseImpl::isJavaUtilMapEntry(String name)
val name = c.instanceClassName
return "java.util.Map.Entry".equals(name) || "java.util.Map$Entry".equals(name)
}

def static isMapType(GenClass c) {
// See : GenBaseImpl::isJavaUtilMapEntry(String name)
isMapType(c.ecoreClass)
}
}
Expand Up @@ -128,7 +128,7 @@ class GenerateDevStructure {
// println("Generate classes in : " + srcAbsolutePath)
// println("Generate interfaces in : " + interfaceAbsolutePath)

for (c : gp.genClasses.filter[!isDynamic]) {
for (c : gp.genClasses.filter[!isDynamic].filter[p | !GenerateCommon.isMapType(p)]) {
if (!c.isInterface)
generateOverriddenClass(c, srcAbsolutePath) // Can still generate abstract classes
generateOverriddenInterface(c, interfaceAbsolutePath)
Expand All @@ -153,6 +153,8 @@ class GenerateDevStructure {
for (sp : gp.subGenPackages)
sp.generateDevStructure
}



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

«FOR gc : gp.genClasses.filter[!isDynamic].filter[!isAbstract]»
«FOR gc : gp.genClasses.filter[!isDynamic].filter[!isAbstract].filter[p | !GenerateCommon.isMapType(p)]»
«gc.generateFactoryDef»
«ENDFOR»
}
Expand Down Expand Up @@ -425,7 +427,7 @@ class GenerateDevStructure {

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

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

«FOR gc : gp.genClasses.filter[!isDynamic].filter[!isAbstract]»
«FOR gc : gp.genClasses.filter[!isDynamic].filter[!isAbstract].filter[p | !GenerateCommon.isMapType(p)]»
«gc.generateCreateMethod»
«ENDFOR»
}
Expand Down
Expand Up @@ -9,6 +9,7 @@ import java.util.function.Consumer
import org.eclipse.emf.codegen.ecore.genmodel.GenBase
import org.eclipse.emf.codegen.ecore.genmodel.GenModel
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EClassifier
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EcorePackage

Expand Down Expand Up @@ -87,11 +88,13 @@ class GMATransform implements GMAConstants {
isInit = false
}



def void computeNames(EPackage p) {
// Do not compute names for emf2002Ecore
if (!EcorePackage::eNS_URI.equals(p.nsURI)) {
for (c : p.getEClassifiers()) {
if ((c instanceof EClass) && !c.name.endsWith("Package")) {
if ((c instanceof EClass) && !c.name.endsWith("Package") && !GenerateCommon.isMapType(c)) {
val devIntName = MessageFormat.format(devInterfaceNamePattern, c.name)
val genIntName = MessageFormat.format(genInterfaceNamePattern, c.name)
// println("Put : " + genIntName + "," + devIntName);
Expand Down
Expand Up @@ -12,8 +12,10 @@
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.osgi.framework.Bundle;
Expand Down Expand Up @@ -144,4 +146,13 @@ public static String getModelPathFromStringURI(final String projectName, final S
}
return modelDir;
}

public static boolean isMapType(final EClassifier c) {
final String name = c.getInstanceClassName();
return ("java.util.Map.Entry".equals(name) || "java.util.Map$Entry".equals(name));
}

public static boolean isMapType(final GenClass c) {
return GenerateCommon.isMapType(c.getEcoreClass());
}
}
Expand Up @@ -164,7 +164,11 @@ public void generateDevStructure(final GenPackage gp) {
boolean _isDynamic = it.isDynamic();
return Boolean.valueOf((!_isDynamic));
};
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function);
final Function1<GenClass, Boolean> _function_1 = (GenClass p) -> {
boolean _isMapType = GenerateCommon.isMapType(p);
return Boolean.valueOf((!_isMapType));
};
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function), _function_1);
for (final GenClass c : _filter) {
{
boolean _isInterface = c.isInterface();
Expand Down Expand Up @@ -553,7 +557,11 @@ public CharSequence generateInterfaceFactoryContent(final GenPackage gp) {
boolean _isAbstract = it.isAbstract();
return Boolean.valueOf((!_isAbstract));
};
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function), _function_1);
final Function1<GenClass, Boolean> _function_2 = (GenClass p) -> {
boolean _isMapType = GenerateCommon.isMapType(p);
return Boolean.valueOf((!_isMapType));
};
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function), _function_1), _function_2);
for(final GenClass gc : _filter) {
_builder.append("\t");
CharSequence _generateFactoryDef = this.generateFactoryDef(gc);
Expand Down Expand Up @@ -659,7 +667,11 @@ public CharSequence generateClassFactoryContent(final GenPackage gp) {
boolean _isAbstract = it.isAbstract();
return Boolean.valueOf((!_isAbstract));
};
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function), _function_1);
final Function1<GenClass, Boolean> _function_2 = (GenClass p) -> {
boolean _isMapType = GenerateCommon.isMapType(p);
return Boolean.valueOf((!_isMapType));
};
Iterable<GenClass> _filter = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function), _function_1), _function_2);
for(final GenClass gc : _filter) {
_builder.append("import ");
String _computePackageNameForInterfaces = this.computePackageNameForInterfaces(gp);
Expand Down Expand Up @@ -750,15 +762,19 @@ public CharSequence generateClassFactoryContent(final GenPackage gp) {
_builder.append("\t");
_builder.newLine();
{
final Function1<GenClass, Boolean> _function_2 = (GenClass it) -> {
final Function1<GenClass, Boolean> _function_3 = (GenClass it) -> {
boolean _isDynamic = it.isDynamic();
return Boolean.valueOf((!_isDynamic));
};
final Function1<GenClass, Boolean> _function_3 = (GenClass it) -> {
final Function1<GenClass, Boolean> _function_4 = (GenClass it) -> {
boolean _isAbstract = it.isAbstract();
return Boolean.valueOf((!_isAbstract));
};
Iterable<GenClass> _filter_1 = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function_2), _function_3);
final Function1<GenClass, Boolean> _function_5 = (GenClass p) -> {
boolean _isMapType = GenerateCommon.isMapType(p);
return Boolean.valueOf((!_isMapType));
};
Iterable<GenClass> _filter_1 = IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(IterableExtensions.<GenClass>filter(gp.getGenClasses(), _function_3), _function_4), _function_5);
for(final GenClass gc_1 : _filter_1) {
_builder.append("\t");
CharSequence _generateCreateMethod = this.generateCreateMethod(gc_1);
Expand Down
Expand Up @@ -119,7 +119,7 @@ public void computeNames(final EPackage p) {
if (_not) {
EList<EClassifier> _eClassifiers = p.getEClassifiers();
for (final EClassifier c : _eClassifiers) {
if (((c instanceof EClass) && (!c.getName().endsWith("Package")))) {
if ((((c instanceof EClass) && (!c.getName().endsWith("Package"))) && (!GenerateCommon.isMapType(c)))) {
final String devIntName = MessageFormat.format(this.devInterfaceNamePattern, c.getName());
final String genIntName = MessageFormat.format(this.genInterfaceNamePattern, c.getName());
this.devNames.put(genIntName, devIntName);
Expand Down

0 comments on commit 20e2bea

Please sign in to comment.