Commit 890c5543 authored by Marco Konersmann's avatar Marco Konersmann

Generates compiling transformation classes

parent 14af768b
Pipeline #1149 failed with stage
in 2 minutes and 53 seconds
package integrationMechanism.core.generator;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.HashMap;
......@@ -48,6 +50,7 @@ import integrationMechansim.core.utils.Utils;
public class IntegrationMechanismCodeGenerator {
private String ecoreFile;
public String modelName;
IProject metaModelProject = null;
/**
* A map of {@link EClass}es, {@link EAttribute}s, and {@link EReference}s
......@@ -60,6 +63,10 @@ public class IntegrationMechanismCodeGenerator {
modelName = ecoreFile.substring(ecoreFile.lastIndexOf(File.separator) + 1, ecoreFile.lastIndexOf("."));
final Resource ecoreModel = loadEcore();
final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
final String projectName = ecoreModel.getURI().toPlatformString(true).split("/")[1];
metaModelProject = root.getProject(projectName);
startSelectionGUI(ecoreModel, this);
}
......@@ -90,7 +97,7 @@ public class IntegrationMechanismCodeGenerator {
public Resource loadEcore() {
final ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl());
final Resource resource = resSet.getResource(URI.createFileURI(ecoreFile), true);
final Resource resource = resSet.getResource(URI.createPlatformResourceURI(ecoreFile, true), true);
return resource;
}
......@@ -110,15 +117,6 @@ public class IntegrationMechanismCodeGenerator {
String.format(Utils.readFile("/Maven/MANIFESTMM"), modelName + ".mm", modelName + ".mm"));
mm_srcFolder.getJavaProject().getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
createPOM(transformation_srcFolder.getJavaProject().getProject(),
String.format(Utils.readFile("/Maven/POMTemplateTransformation"), modelName + ".transformation",
modelName + ".transformation"));
createMANIFEST(transformation_srcFolder.getJavaProject().getProject(),
String.format(Utils.readFile("/Maven/MANIFESTTransformation"), modelName + ".transformation",
modelName + ".transformation"));
transformation_srcFolder.getJavaProject().getProject().refreshLocal(IResource.DEPTH_INFINITE,
new NullProgressMonitor());
createPOM(runtime_srcFolder.getJavaProject().getProject(), String
.format(Utils.readFile("/Maven/POMTemplateRuntime"), modelName + ".runtime", modelName + ".runtime"));
createMANIFEST(runtime_srcFolder.getJavaProject().getProject(), String
......@@ -126,6 +124,28 @@ public class IntegrationMechanismCodeGenerator {
runtime_srcFolder.getJavaProject().getProject().refreshLocal(IResource.DEPTH_INFINITE,
new NullProgressMonitor());
final String metaModelProjectName = metaModelProject.getName();
String metaModelProjectVersion = "1.0.0.qualifier";
final IFile manifest = metaModelProject.getFile("META-INF/MANIFEST.MF");
final BufferedReader reader = new BufferedReader(new InputStreamReader(manifest.getContents()));
final String bundleVersionKey = "Bundle-Version: ";
String line = "";
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith(bundleVersionKey)) {
metaModelProjectVersion = line.substring(bundleVersionKey.length());
break;
}
}
createMANIFEST(transformation_srcFolder.getJavaProject().getProject(),
String.format(Utils.readFile("/Maven/MANIFESTTransformation"), modelName + ".transformation",
modelName + ".transformation", metaModelProjectName, metaModelProjectVersion));
createPOM(transformation_srcFolder.getJavaProject().getProject(),
String.format(Utils.readFile("/Maven/POMTemplateTransformation"), modelName + ".transformation",
modelName + ".transformation"));
transformation_srcFolder.getJavaProject().getProject().refreshLocal(IResource.DEPTH_INFINITE,
new NullProgressMonitor());
}
private void createMANIFEST(IProject project, String content) throws UnsupportedEncodingException, CoreException {
......
......@@ -22,4 +22,9 @@ public abstract class Mechanism {
public void setMonitor(IProgressMonitor monitor) {
this.monitor = monitor;
}
public String getFQN(IPackageFragment packageFragment) {
return packageFragment.getElementName();
}
}
package org.codeling.mechanisms.classes
import org.codeling.mechanisms.Mechanism
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EClassifier
import org.eclipse.emf.ecore.ENamedElement
import org.eclipse.emf.ecore.EReference
import org.eclipse.jdt.core.IPackageFragment
import integrationMechansim.core.utils.JavaKeyWords
class MarkerInterfaceMechanism extends Mechanism {
......@@ -12,37 +14,74 @@ class MarkerInterfaceMechanism extends Mechanism {
}
override createMetaModelLibrary(IPackageFragment packageFragment, ENamedElement element) {
val packageName = packageFragment.elementName;
var String ifaceName = element.name.toFirstUpper;
if (JavaKeyWords.isKeyword(ifaceName))
ifaceName = '''_«ifaceName»''';
val content = '''
package «packageName»;
package «packageFragment.FQN»;
public interface «ifaceName» {}
'''
public interface «ifaceName» {
}
''';
packageFragment.createCompilationUnit('''«ifaceName».java''', content, true, monitor);
}
override createRuntime(IPackageFragment packageFragment, ENamedElement element) {
val packageName = packageFragment.elementName;
var String ifaceName = element.name.toFirstUpper;
if (JavaKeyWords.isKeyword(ifaceName))
ifaceName = '''_«ifaceName»''';
override createRuntime(IPackageFragment packageFragment, ENamedElement element) {
var String typeName = element.name.toFirstUpper;
val content = '''
package «packageName»;
package «packageFragment.FQN»;
import marker_interface.runtime.MarkerInterfaceRuntime;
public class «ifaceName»Runtime extends MarkerInterfaceRuntime {}
public class «typeName»Runtime extends MarkerInterfaceRuntime {
}
''';
packageFragment.createCompilationUnit('''«typeName»Runtime.java''', content, true, monitor);
}
override createTransformation(IPackageFragment packageFragment, ENamedElement element) {
if (!(element instanceof EClass))
return;
var String typeName = element.name.toFirstUpper;
val EClass eClass = element as EClass;
val String eClassName = eClass.name;
val EClass owningEClass = getOwningEClass(eClass);
val String owningEClassName = owningEClass.name;
val content = '''
package «packageFragment.FQN»;
import org.codeling.mechanisms.transformations.MarkerInterfaceTransformation;
import org.eclipse.jdt.core.IType;
import de.mkonersmann.advert.java.embed.transformation.AbstractModelCodeTransformation;
import de.mkonersmann.ejb31.«eClassName»;
import de.mkonersmann.ejb31.«owningEClassName»;
public class «typeName»Transformation
extends MarkerInterfaceTransformation<«eClassName», «owningEClassName», IType> {
public «typeName»Transformation(AbstractModelCodeTransformation<«eClassName», «owningEClassName», IType> parentTransformation) {
super(parentTransformation);
}
}
'''
packageFragment.createCompilationUnit('''«ifaceName»Runtime.java''', content, true, monitor);
packageFragment.createCompilationUnit('''«typeName»Transformation.java''', content, true, monitor);
}
def getOwningEClass(EClass eClass) {
for (EClassifier c : eClass.getEPackage().getEClassifiers())
if (c instanceof EClass)
for (EReference r : (c as EClass).EAllContainments)
if (r.getEType() == eClass)
return r.EContainingClass;
return eClass;
}
}
......@@ -15,7 +15,11 @@ Require-Bundle: org.eclipse.ui,
org.modelversioning.emfprofile;bundle-version="1.1.0",
org.eclipse.gmf.runtime.notation;bundle-version="1.8.0",
org.eclipse.emf.ecore,
com.google.guava
com.google.guava,
utils;bundle-version="1.0.0",
java.embed.transformation;bundle-version="1.0.0",
integration-mechanisms;bundle-version="0.1.0",
%s;bundle-version="%s"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
package org.codeling.mechanisms.classes;
import integrationMechansim.core.utils.JavaKeyWords;
import com.google.common.base.Objects;
import org.codeling.mechanisms.Mechanism;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Exceptions;
......@@ -18,31 +23,28 @@ public class MarkerInterfaceMechanism extends Mechanism {
@Override
public void createMetaModelLibrary(final IPackageFragment packageFragment, final ENamedElement element) {
try {
final String packageName = packageFragment.getElementName();
String _name = element.getName();
String ifaceName = StringExtensions.toFirstUpper(_name);
boolean _isKeyword = JavaKeyWords.isKeyword(ifaceName);
if (_isKeyword) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("_");
_builder.append(ifaceName, "");
ifaceName = _builder.toString();
}
StringConcatenation _builder = new StringConcatenation();
_builder.append("package ");
String _fQN = this.getFQN(packageFragment);
_builder.append(_fQN, "");
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("public interface ");
_builder.append(ifaceName, "");
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final String content = _builder.toString();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package ");
_builder_1.append(packageName, "");
_builder_1.append(";");
_builder_1.newLineIfNotEmpty();
_builder_1.newLine();
_builder_1.append("public interface ");
_builder_1.append(ifaceName, "");
_builder_1.append(" {}");
_builder_1.newLineIfNotEmpty();
final String content = _builder_1.toString();
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append(ifaceName, "");
_builder_2.append(".java");
packageFragment.createCompilationUnit(_builder_2.toString(), content, true, this.monitor);
_builder_1.append(".java");
packageFragment.createCompilationUnit(_builder_1.toString(), content, true, this.monitor);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
......@@ -51,36 +53,126 @@ public class MarkerInterfaceMechanism extends Mechanism {
@Override
public void createRuntime(final IPackageFragment packageFragment, final ENamedElement element) {
try {
final String packageName = packageFragment.getElementName();
String _name = element.getName();
String ifaceName = StringExtensions.toFirstUpper(_name);
boolean _isKeyword = JavaKeyWords.isKeyword(ifaceName);
if (_isKeyword) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("_");
_builder.append(ifaceName, "");
ifaceName = _builder.toString();
String typeName = StringExtensions.toFirstUpper(_name);
StringConcatenation _builder = new StringConcatenation();
_builder.append("package ");
String _fQN = this.getFQN(packageFragment);
_builder.append(_fQN, "");
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("import marker_interface.runtime.MarkerInterfaceRuntime;");
_builder.newLine();
_builder.newLine();
_builder.append("public class ");
_builder.append(typeName, "");
_builder.append("Runtime extends MarkerInterfaceRuntime {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final String content = _builder.toString();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append(typeName, "");
_builder_1.append("Runtime.java");
packageFragment.createCompilationUnit(_builder_1.toString(), content, true, this.monitor);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
@Override
public void createTransformation(final IPackageFragment packageFragment, final ENamedElement element) {
try {
if ((!(element instanceof EClass))) {
return;
}
String _name = element.getName();
String typeName = StringExtensions.toFirstUpper(_name);
final EClass eClass = ((EClass) element);
final String eClassName = eClass.getName();
final EClass owningEClass = this.getOwningEClass(eClass);
final String owningEClassName = owningEClass.getName();
StringConcatenation _builder = new StringConcatenation();
_builder.append("package ");
String _fQN = this.getFQN(packageFragment);
_builder.append(_fQN, "");
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("import org.codeling.mechanisms.transformations.MarkerInterfaceTransformation;");
_builder.newLine();
_builder.append("import org.eclipse.jdt.core.IType;");
_builder.newLine();
_builder.newLine();
_builder.append("import de.mkonersmann.advert.java.embed.transformation.AbstractModelCodeTransformation;");
_builder.newLine();
_builder.append("import de.mkonersmann.ejb31.");
_builder.append(eClassName, "");
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.append("import de.mkonersmann.ejb31.");
_builder.append(owningEClassName, "");
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("public class ");
_builder.append(typeName, "");
_builder.append("Transformation");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("extends MarkerInterfaceTransformation<");
_builder.append(eClassName, "\t\t");
_builder.append(", ");
_builder.append(owningEClassName, "\t\t");
_builder.append(", IType> {");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t");
_builder.append("public ");
_builder.append(typeName, "\t");
_builder.append("Transformation(AbstractModelCodeTransformation<");
_builder.append(eClassName, "\t");
_builder.append(", ");
_builder.append(owningEClassName, "\t");
_builder.append(", IType> parentTransformation) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("super(parentTransformation);");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("}");
_builder.newLine();
final String content = _builder.toString();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package ");
_builder_1.append(packageName, "");
_builder_1.append(";");
_builder_1.newLineIfNotEmpty();
_builder_1.newLine();
_builder_1.append("import marker_interface.runtime.MarkerInterfaceRuntime;");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("public class ");
_builder_1.append(ifaceName, "");
_builder_1.append("Runtime extends MarkerInterfaceRuntime {}");
_builder_1.newLineIfNotEmpty();
final String content = _builder_1.toString();
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append(ifaceName, "");
_builder_2.append("Runtime.java");
packageFragment.createCompilationUnit(_builder_2.toString(), content, true, this.monitor);
_builder_1.append(typeName, "");
_builder_1.append("Transformation.java");
packageFragment.createCompilationUnit(_builder_1.toString(), content, true, this.monitor);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
public EClass getOwningEClass(final EClass eClass) {
EPackage _ePackage = eClass.getEPackage();
EList<EClassifier> _eClassifiers = _ePackage.getEClassifiers();
for (final EClassifier c : _eClassifiers) {
if ((c instanceof EClass)) {
EList<EReference> _eAllContainments = ((EClass) c).getEAllContainments();
for (final EReference r : _eAllContainments) {
EClassifier _eType = r.getEType();
boolean _equals = Objects.equal(_eType, eClass);
if (_equals) {
return r.getEContainingClass();
}
}
}
}
return eClass;
}
}
......@@ -33,7 +33,7 @@ public class MenuChoiceHandler implements IHandler {
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
final IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
final IFile firstElement = (IFile) selection.getFirstElement();
myTest.loadModel(firstElement.getLocation().toOSString());
myTest.loadModel(firstElement.getFullPath().toPortableString());
return null;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment