Skip to content

Commit

Permalink
Fix issue #9 : Should call organize import or generate the correct
Browse files Browse the repository at this point in the history
import

Finally imports are well generated in src-gen ! 

Change-Id: I37aa6594d16a52749698b6c9c4ab82da5484e138
Signed-off-by: Olivier Prouvost <olivier.prouvost@opcoach.com>
  • Loading branch information
opcoach committed Apr 10, 2016
1 parent 93cd679 commit 5175c33
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
@@ -0,0 +1,35 @@
package com.opcoach.genmodeladdon.core.test;

import org.junit.Test;

public class TestImports extends GenModelAddonTestCase
{


// --------------------------------------------------------------------
// ------------------- Test imports of dev class in the gen classses --
// --------------------------------------------------------------------

@Test
public void folderImplementationMustImportFolder()
{
assertFileContains("src-gen/com/opcoach/project/impl/MFolderImpl.java",
"import com.opcoach.project.Folder");
}


@Test
public void projectImplementationMustImportPersonProjectAndTask()
{
assertFileContains("src-gen/com/opcoach/project/impl/MProjectImpl.java",
"import com.opcoach.project.Project");
assertFileContains("src-gen/com/opcoach/project/impl/MProjectImpl.java",
"import com.opcoach.project.Person");
assertFileContains("src-gen/com/opcoach/project/impl/MProjectImpl.java",
"import com.opcoach.project.Task");
}




}
48 changes: 28 additions & 20 deletions com.opcoach.genmodeladdon.core/templates/model/Class.javajet
@@ -1,4 +1,4 @@
<%@ jet package="org.eclipse.emf.codegen.ecore.templates.model" imports="java.util.* org.eclipse.emf.codegen.ecore.genmodel.* org.eclipse.emf.ecore.* org.eclipse.emf.codegen.util.CodeGenUtil" class="Class"%>
<%@ jet package="org.eclipse.emf.codegen.ecore.templates.model" imports="java.util.* java.io.* org.eclipse.emf.codegen.ecore.genmodel.* org.eclipse.emf.ecore.* org.eclipse.emf.codegen.util.CodeGenUtil" class="Class"%>
<%
/**
* Copyright (c) 2002-2011 IBM Corporation and others.
Expand All @@ -16,20 +16,7 @@ class DevTransform {

// The map of devnames : key = implementation name, value = devName
private Map<String, String> devNames;
private String devInterfaceNamePattern, genInterfaceNamePattern, devClassNamePattern, genClassNamePattern;
//private static DevTransform instance = null;
//private static GenModel lastGm = null;

/* public static DevTransform getInstance(GenModel gm, String devInterfaceNamePattern, String genInterfaceNamePattern, String devClassNamePattern, String genClassNamePattern)
{
if (gm != lastGm)
{
instance = new DevTransform(gm, devInterfaceNamePattern, genInterfaceNamePattern, devClassNamePattern, genClassNamePattern);
lastGm = gm;
}
return instance;
} */

private String devInterfaceNamePattern, genInterfaceNamePattern, devClassNamePattern, genClassNamePattern;

public DevTransform(GenModel gm, String devInterfaceNamePattern, String genInterfaceNamePattern, String devClassNamePattern, String genClassNamePattern)
{
Expand Down Expand Up @@ -62,8 +49,6 @@ class DevTransform {
String devClassName = java.text.MessageFormat.format(devClassNamePattern, c.getName());
// System.out.println("Put : " + genClassName + "," + devClassName);
devNames.put(genClassName, devClassName);


}
}
for (EPackage childPackage : p.getESubpackages())
Expand All @@ -77,7 +62,7 @@ class DevTransform {
{
if (stringToTranslate.contains(key))
{
// System.out.println("String : " + stringToTranslate + " contains " + key + " will replace with " + devNames.get(key) );
// System.out.println("String : " + stringToTranslate + " contains " + key + " will replace with " + devNames.get(key) );
stringToTranslate = stringToTranslate.replaceAll(key, devNames.get(key));
}
}
Expand Down Expand Up @@ -115,7 +100,8 @@ package <%=genPackage.getInterfacePackageName()%>;
package <%=genPackage.getClassPackageName()%>;
<%}%>

<%genModel.markImportLocation(stringBuffer, genPackage);%>
<% final int insertPointForChangedImport = stringBuffer.length();
genModel.markImportLocation(stringBuffer, genPackage);%>
<%if (isImplementation) { genClass.addClassPsuedoImports(); }%>

<%if (isInterface) {%>
Expand Down Expand Up @@ -2574,4 +2560,26 @@ if (index != -1) { head = typeName.substring(0, index); tail = typeName.substrin
<%@ include file="Class/insert.javajetinc" fail="silent" %>
} //<%=isInterface ? " " + genClass.getInterfaceName() : genClass.getClassName()%>
<%// TODO fix the space above%>
<%genModel.emitSortedImports();%>
<% // Update imports before insertion
String oldImports = genModel.getImportManager().computeSortedImports();
String thisLine = null;
Collection<String> listImport = new ArrayList<String>();
try{
BufferedReader br = new BufferedReader(new StringReader(oldImports));
while ((thisLine = br.readLine()) != null) {
String newImport = devTransform.replaceDevName(thisLine);
if ("\n".equals(newImport) || (!listImport.contains(newImport)))
listImport.add(newImport);
// Must keep the previous package (both are used)
if (thisLine.endsWith("Package;") && (!listImport.contains(thisLine)))
listImport.add(thisLine);

}
}catch(Exception e){
e.printStackTrace();
}
StringBuffer bufImport = new StringBuffer();
for (String s: listImport)
bufImport.append(s).append("\n");
String newImports = bufImport.toString();
stringBuffer.insert(insertPointForChangedImport, newImports.toString());%>
Expand Up @@ -160,10 +160,6 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
e.printStackTrace();
}

// Then should reorganize imports in the project in package explorer view !
MessageDialog.openInformation(parentShell, "Don't forget to reorganize imports",
"Generation is finished. \n\n"
+ "The last manual step is to reorganize imports on " + gm.getModelDirectory() + " directory ! ");
}
}

Expand Down

0 comments on commit 5175c33

Please sign in to comment.