Skip to content

Commit

Permalink
Factory implementation code must be generated as xtend file if xtend …
Browse files Browse the repository at this point in the history
…option is chosen. (#98)
  • Loading branch information
PhilippeNoel committed Sep 25, 2023
1 parent fa5f994 commit ca85d7d
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 17 deletions.
Expand Up @@ -128,8 +128,8 @@ public void testXtendClassNames() {
assertFileExists("src/com/opcoach/xtend/project/impl/PersonImpl.xtend");
assertFileNotExists("src/com/opcoach/xtend/project/impl/PersonImpl.java");

assertFileExists("src/com/opcoach/xtend/project/impl/ProjectFactoryImpl.java");
assertFileNotExists("src/com/opcoach/xtend/project/impl/ProjectFactoryImpl.xtend");
assertFileExists("src/com/opcoach/xtend/project/impl/ProjectFactoryImpl.xtend");
assertFileNotExists("src/com/opcoach/xtend/project/impl/ProjectFactoryImpl.java");

assertFileExists("src/com/opcoach/xtend/project/impl/ProjectFolderImpl.xtend");
assertFileNotExists("src/com/opcoach/xtend/project/impl/ProjectFolderImpl.java");
Expand Down
Expand Up @@ -92,7 +92,6 @@ class GenerateDevStructure implements IResourceChangeListener {
ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_BUILD)
}


/** Generate the file structure. If genFiles is false just compute the files to be generated */
def generateDevStructure(boolean genFiles) {
generateFiles = genFiles
Expand Down Expand Up @@ -136,8 +135,7 @@ class GenerateDevStructure implements IResourceChangeListener {
// Count nb of EClasses in the Package
val nbClasses = gp.getEcorePackage.EClassifiers.filter(EClass).size

// println("There are " + nbClasses + " classes in package " + gp.getEcorePackage.name + " with URI " + gp.getEcorePackage.nsURI)

// println("There are " + nbClasses + " classes in package " + gp.getEcorePackage.name + " with URI " + gp.getEcorePackage.nsURI)
// Generate factory interface and implementation only if they are classes
if (nbClasses > 0) {
gp.generateOverriddenFactoryInterface(interfaceAbsolutePath)
Expand Down Expand Up @@ -315,7 +313,7 @@ class GenerateDevStructure implements IResourceChangeListener {
e.printStackTrace
} finally {
PluginModelManager.instance.targetReloaded(new NullProgressMonitor())
// PDECore.^default.modelManager.bundleRootChanged(project)
// PDECore.^default.modelManager.bundleRootChanged(project)
}

}
Expand All @@ -331,8 +329,15 @@ class GenerateDevStructure implements IResourceChangeListener {
}

def generateOverriddenFactoryClass(GenPackage gp, String path) {
val filename = path + gp.computeFactoryClassName + ".java"
generateFile(filename, gp.generateClassFactoryContent)
// Generates either xtend or java code
if (gmaGenModel.mustGenerateOverridenImplAsXtendCode) {
val filename = path + gp.computeFactoryClassName + ".xtend"
generateFile(filename, gp.generateXtendClassFactoryContent)
} else {
val filename = path + gp.computeFactoryClassName + ".java"
generateFile(filename, gp.generateClassFactoryContent)

}
}

def generateOverriddenPackageInterface(GenPackage gp, String path) {
Expand All @@ -341,8 +346,8 @@ class GenerateDevStructure implements IResourceChangeListener {
}

def generateOverriddenClass(GenClass gc, String path) {

if( gmaGenModel.mustGenerateOverridenImplAsXtendCode) {
// Generates either xtend or java code
if (gmaGenModel.mustGenerateOverridenImplAsXtendCode) {
generateFile(path + gc.computeClassFilename + ".xtend", gc.generateXtendClassContent)
} else {
generateFile(path + gc.computeClassFilename + ".java", gc.generateClassContent)
Expand Down Expand Up @@ -491,6 +496,7 @@ class GenerateDevStructure implements IResourceChangeListener {
""
}
// Generates java code for class factory
def generateClassFactoryContent(GenPackage gp) '''
«copyright»
package «gp.computePackageNameForClasses»;
Expand Down Expand Up @@ -522,6 +528,38 @@ class GenerateDevStructure implements IResourceChangeListener {
}
'''
// Generates xtend code for class factory
def generateXtendClassFactoryContent(GenPackage gp) '''
«copyright»
package «gp.computePackageNameForClasses»

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

import «gp.computePackageNameForInterfaces».«gp.computeFactoryInterfaceName»


// This factory renames the generated factory interface to use it as an overriden factory
class «gp.computeFactoryClassName» extends «gp.factoryClassName» implements «gp.
computeFactoryInterfaceName»
{

def static «gp.computeFactoryInterfaceName» init() {

try {
var Object fact = «gp.factoryClassName».init()
if ((fact !== null) && (fact instanceof «gp.computeFactoryInterfaceName»))
return (fact as «gp.computeFactoryInterfaceName»)
}
catch (Exception exception) {
EcorePlugin.INSTANCE.log(exception)
}
return new «gp.computeFactoryClassName»()
}


}
'''
def computeCopyrightComment() '''
«IF gmaGenModel.copyrightText !== null && gmaGenModel.copyrightText.length > 0»
/**
Expand Down
Expand Up @@ -381,14 +381,28 @@ public Object generateOverriddenFactoryInterface(final GenPackage gp, final Stri
}

public Object generateOverriddenFactoryClass(final GenPackage gp, final String path) {
Object _xblockexpression = null;
{
String _computeFactoryClassName = this.computeFactoryClassName(gp);
String _plus = (path + _computeFactoryClassName);
final String filename = (_plus + ".java");
_xblockexpression = this.generateFile(filename, this.generateClassFactoryContent(gp));
Object _xifexpression = null;
boolean _mustGenerateOverridenImplAsXtendCode = this.gmaGenModel.mustGenerateOverridenImplAsXtendCode();
if (_mustGenerateOverridenImplAsXtendCode) {
Object _xblockexpression = null;
{
String _computeFactoryClassName = this.computeFactoryClassName(gp);
String _plus = (path + _computeFactoryClassName);
final String filename = (_plus + ".xtend");
_xblockexpression = this.generateFile(filename, this.generateXtendClassFactoryContent(gp));
}
_xifexpression = _xblockexpression;
} else {
Object _xblockexpression_1 = null;
{
String _computeFactoryClassName = this.computeFactoryClassName(gp);
String _plus = (path + _computeFactoryClassName);
final String filename = (_plus + ".java");
_xblockexpression_1 = this.generateFile(filename, this.generateClassFactoryContent(gp));
}
_xifexpression = _xblockexpression_1;
}
return _xblockexpression;
return _xifexpression;
}

public Object generateOverriddenPackageInterface(final GenPackage gp, final String path) {
Expand Down Expand Up @@ -854,6 +868,101 @@ public CharSequence generateClassFactoryContent(final GenPackage gp) {
return _builder;
}

public CharSequence generateXtendClassFactoryContent(final GenPackage gp) {
StringConcatenation _builder = new StringConcatenation();
_builder.append(this.copyright);
_builder.newLineIfNotEmpty();
_builder.append("package ");
String _computePackageNameForClasses = this.computePackageNameForClasses(gp);
_builder.append(_computePackageNameForClasses);
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("import org.eclipse.emf.ecore.plugin.EcorePlugin");
_builder.newLine();
_builder.newLine();
_builder.append("import ");
String _computePackageNameForInterfaces = this.computePackageNameForInterfaces(gp);
_builder.append(_computePackageNameForInterfaces);
_builder.append(".");
String _computeFactoryInterfaceName = this.computeFactoryInterfaceName(gp);
_builder.append(_computeFactoryInterfaceName);
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.newLine();
_builder.append("// This factory renames the generated factory interface to use it as an overriden factory");
_builder.newLine();
_builder.append("class ");
String _computeFactoryClassName = this.computeFactoryClassName(gp);
_builder.append(_computeFactoryClassName);
_builder.append(" extends ");
String _factoryClassName = gp.getFactoryClassName();
_builder.append(_factoryClassName);
_builder.append(" implements ");
String _computeFactoryInterfaceName_1 = this.computeFactoryInterfaceName(gp);
_builder.append(_computeFactoryInterfaceName_1);
_builder.newLineIfNotEmpty();
_builder.append("{");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("def static ");
String _computeFactoryInterfaceName_2 = this.computeFactoryInterfaceName(gp);
_builder.append(_computeFactoryInterfaceName_2, "\t");
_builder.append(" init() {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("var Object fact = ");
String _factoryClassName_1 = gp.getFactoryClassName();
_builder.append(_factoryClassName_1, "\t\t\t");
_builder.append(".init()");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("if ((fact !== null) && (fact instanceof ");
String _computeFactoryInterfaceName_3 = this.computeFactoryInterfaceName(gp);
_builder.append(_computeFactoryInterfaceName_3, "\t\t\t");
_builder.append("))");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t\t\t");
_builder.append("return (fact as ");
String _computeFactoryInterfaceName_4 = this.computeFactoryInterfaceName(gp);
_builder.append(_computeFactoryInterfaceName_4, "\t\t\t\t\t");
_builder.append(")");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("catch (Exception exception) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("EcorePlugin.INSTANCE.log(exception)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return new ");
String _computeFactoryClassName_1 = this.computeFactoryClassName(gp);
_builder.append(_computeFactoryClassName_1, "\t\t");
_builder.append("() ");
_builder.newLineIfNotEmpty();
_builder.append("\t\t ");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder;
}

public CharSequence computeCopyrightComment() {
StringConcatenation _builder = new StringConcatenation();
{
Expand Down

0 comments on commit ca85d7d

Please sign in to comment.