Skip to content

Commit f178ef4

Browse files
committed
EDIT: Merged EPackageUtil with MetamodelSearcher.
1 parent 97ba21c commit f178ef4

9 files changed

Lines changed: 130 additions & 154 deletions

src/main/java/jce/codemanipulation/ecore/AbstractFactoryRenamer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import jce.codemanipulation.AbstractCodeManipulator;
1515
import jce.properties.EcorificationProperties;
1616
import jce.properties.TextProperty;
17-
import jce.util.MetamodelSearcher;
17+
import jce.util.EcoreUtil;
1818
import jce.util.PathHelper;
1919
import jce.util.jdt.RefactoringUtil;
2020

@@ -98,7 +98,7 @@ private void setNewName(ICompilationUnit unit, String newName, RenameCompilation
9898
*/
9999
protected final boolean isInMetamodel(String fullName) {
100100
String modelName = nameUtil.cutFirstSegment(fullName); // remove leading ecore package
101-
return MetamodelSearcher.findEClass(modelName, metamodel.getRoot()) != null; // search metamodel counterpart
101+
return EcoreUtil.findEClass(modelName, metamodel.getRoot()) != null; // search metamodel counterpart
102102
}
103103

104104
/**

src/main/java/jce/codemanipulation/ecore/EcoreImportManipulator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import jce.codemanipulation.AbstractCodeManipulator;
1717
import jce.properties.EcorificationProperties;
1818
import jce.properties.TextProperty;
19-
import jce.util.MetamodelSearcher;
19+
import jce.util.EcoreUtil;
2020
import jce.util.jdt.ASTUtil;
2121

2222
/**
@@ -130,7 +130,7 @@ private boolean isEcoreImplementation(ICompilationUnit unit) throws JavaModelExc
130130
String typeName = nameUtil.cutFirstSegment(getPackageMemberName(unit));
131131
if (isEcoreImplementationName(typeName)) { // if has Ecore implementation name and package
132132
typeName = getInterfaceName(typeName); // get name of Ecore interface and EClass
133-
return MetamodelSearcher.findEClass(typeName, metamodel.getRoot()) != null; // search metamodel counterpart
133+
return EcoreUtil.findEClass(typeName, metamodel.getRoot()) != null; // search metamodel counterpart
134134
}
135135
return false; // Does not have Ecore implementation name and package
136136
}
@@ -148,7 +148,7 @@ private boolean isEcoreImplementationName(String typeName) {
148148
*/
149149
private boolean isEcoreInterface(ICompilationUnit unit) throws JavaModelException {
150150
String typeName = nameUtil.cutFirstSegment(getPackageMemberName(unit));
151-
EClass potentialEClass = MetamodelSearcher.findEClass(typeName, metamodel.getRoot());
151+
EClass potentialEClass = EcoreUtil.findEClass(typeName, metamodel.getRoot());
152152
return potentialEClass != null && potentialEClass.isInterface();
153153
}
154154

@@ -158,7 +158,7 @@ private boolean isEcoreInterface(ICompilationUnit unit) throws JavaModelExceptio
158158
*/
159159
private boolean isInterfaceOfEcoreClass(ICompilationUnit unit) throws JavaModelException {
160160
String typeName = nameUtil.cutFirstSegment(getPackageMemberName(unit));
161-
EClass potentialEClass = MetamodelSearcher.findEClass(typeName, metamodel.getRoot());
161+
EClass potentialEClass = EcoreUtil.findEClass(typeName, metamodel.getRoot());
162162
// Ensure that the class is not the representation of an Ecore interface but only an interface of an
163163
// implementation class
164164
return potentialEClass != null && !potentialEClass.isInterface();
@@ -175,7 +175,7 @@ private boolean isProblematic(IImportDeclaration importDeclaration) {
175175
return false; // EMF imports the Ecore classes directly, not with .*
176176
}
177177
String typeName = nameUtil.cutFirstSegment(importDeclaration.getElementName());
178-
return MetamodelSearcher.findEClass(typeName, metamodel.getRoot()) != null;
178+
return EcoreUtil.findEClass(typeName, metamodel.getRoot()) != null;
179179
}
180180

181181
/**

src/main/java/jce/codemanipulation/ecore/FactoryRelocator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import jce.codemanipulation.AbstractCodeManipulator;
2424
import jce.properties.EcorificationProperties;
2525
import jce.properties.TextProperty;
26-
import jce.util.MetamodelSearcher;
26+
import jce.util.EcoreUtil;
2727
import jce.util.PathHelper;
2828

2929
/**
@@ -67,7 +67,7 @@ private boolean isEcoreFactory(ICompilationUnit unit) throws JavaModelException
6767
String packageName = nameUtil.getLastSegment(nameUtil.cutLastSegments(fullName, 2));
6868
if (fullName.endsWith(PathHelper.capitalize(packageName) + "FactoryImpl")) { // if has factory name
6969
String modelName = nameUtil.cutFirstSegment(fullName); // without ecore package
70-
return MetamodelSearcher.findEClass(modelName, metamodel.getRoot()) == null; // search metamodel counterpart
70+
return EcoreUtil.findEClass(modelName, metamodel.getRoot()) == null; // search metamodel counterpart
7171
}
7272
return false; // Does not have Ecore implementation name and package
7373
}

src/main/java/jce/codemanipulation/ecore/PackageImplFactoryCorrector.xtend

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import jce.properties.TextProperty
88
import jce.util.jdt.ASTUtil
99
import jce.util.PathHelper
1010
import eme.generator.GeneratedEcoreMetamodel
11-
import static extension jce.util.EPackageUtil.*;
11+
import static extension jce.util.EcoreUtil.*
1212

1313
/**
1414
* Code manipulator that correct references to factories in the package classes generated
@@ -37,7 +37,7 @@ class PackageImplFactoryCorrector extends AbstractCodeManipulator {
3737
} else {
3838
return;
3939
}
40-
val package = metamodel.root.findPackage(packageName);
40+
val package = metamodel.root.findEPackage(packageName);
4141
// All packages containing classes got new factories that have to be referenced in package classes
4242
if (!package.classNames.empty) {
4343
val visitor = new PackageImplFactoryCorrectionVisitor(unit, properties);

src/main/java/jce/codemanipulation/origin/MemberRemovalVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import eme.generator.GeneratedEcoreMetamodel;
1717
import jce.properties.EcorificationProperties;
18-
import jce.util.MetamodelSearcher;
18+
import jce.util.EcoreUtil;
1919
import jce.util.PathHelper;
2020
import jce.util.logging.MonitorFactory;
2121

@@ -72,7 +72,7 @@ private boolean isAccessMethod(MethodDeclaration method) {
7272
private boolean isGenerated(VariableDeclarationFragment fragment, TypeDeclaration type) {
7373
String typeName = type.getName().resolveTypeBinding().getQualifiedName(); // fully qualified name of class
7474
String fieldName = fragment.getName().getIdentifier(); // name of field
75-
return MetamodelSearcher.findEStructuralFeature(fieldName, typeName, metamodel.getRoot()) != null;
75+
return EcoreUtil.findEStructuralFeature(fieldName, typeName, metamodel.getRoot()) != null;
7676
}
7777

7878
/**

src/main/java/jce/generators/EcoreFactoryGenerator.xtend

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.eclipse.emf.ecore.EPackage
1313
import static jce.properties.TextProperty.ECORE_PACKAGE
1414
import static jce.properties.TextProperty.SOURCE_FOLDER
1515

16-
import static extension jce.util.EPackageUtil.*;
16+
import static extension jce.util.EcoreUtil.*;
1717

1818
/**
1919
* Creates and manages custom EFactories. Every EFactory has an interface and an implementation class.
@@ -22,7 +22,7 @@ import static extension jce.util.EPackageUtil.*;
2222
final class EcoreFactoryGenerator {
2323
extension final PathHelper pathUtil
2424
extension final EcorificationProperties properties
25-
25+
2626
static final Logger logger = LogManager.getLogger(EcoreFactoryGenerator.getName)
2727
final EFactoryGenerator factoryGenerator
2828
final EFactoryImplementationGenerator factoryImplementationGenerator

src/main/java/jce/util/EPackageUtil.xtend

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package jce.util
2+
3+
import java.util.List
4+
import org.eclipse.emf.ecore.EClass
5+
import org.eclipse.emf.ecore.EClassifier
6+
import org.eclipse.emf.ecore.ENamedElement
7+
import org.eclipse.emf.ecore.EPackage
8+
import org.eclipse.emf.ecore.EStructuralFeature
9+
10+
/**
11+
* A utility class for searching elements in Ecore metamodels.
12+
* @author Heiko Klare, Timur Saglam
13+
*/
14+
final class EcoreUtil {
15+
static final PathHelper PATH = new PathHelper(Character.valueOf('.').charValue)
16+
17+
private new() {
18+
throw new AssertionError("Suppress default constructor for noninstantiability")
19+
}
20+
21+
/**
22+
* Finds a specific {@link EClass} in a {@link EPackage} and its subpackages.
23+
* @param fullName is the fully qualified name of the desired {@link EClass}.
24+
* @param ePackage is the {@link EPackage} which contains the {@link EClass}.
25+
* @return the {@link EClass} or null if there is none with the specified name.
26+
*/
27+
def static EClass findEClass(String fullName, EPackage ePackage) {
28+
var String eClassName = PATH.getLastSegment(fullName) // get EClass name
29+
var EPackage parent = findESubpackage(PATH.cutLastSegment(fullName), ePackage) // search EPackage.
30+
if (parent !== null) { // if EPackage was found.
31+
for (EClassifier classifier : parent.getEClassifiers()) { // for every EClassifier:
32+
if (classifier instanceof EClass && isSame(classifier, eClassName)) {
33+
return (classifier as EClass) // search for the right EClass.
34+
}
35+
}
36+
}
37+
return null
38+
}
39+
40+
/**
41+
* Finds a specific {@link EPackage} which is directly or indirectly a subpackage of a given {@link EPackage}.
42+
* @param fullName is the fully qualified name of the desired {@link EPackage}.
43+
* @param ePackage is the {@link EPackage} which contains the subpackage.
44+
* @return the subpackage or null if there is none with the specified name.
45+
*/
46+
def static EPackage findESubpackage(String fullName, EPackage ePackage) {
47+
for (EPackage subpackage : ePackage.getESubpackages()) { // for every subpackage
48+
if (isSame(subpackage, PATH.getFirstSegment(fullName))) { // if is the right subpackage
49+
if (!PATH.hasMultipleSegments(fullName)) {
50+
return subpackage // return package if the are no more segments in the path
51+
} else {
52+
return findESubpackage(PATH.cutFirstSegment(fullName), subpackage) // search further for every segment
53+
}
54+
}
55+
}
56+
return null
57+
}
58+
59+
/**
60+
* Returns the package with the given fully qualified name, starting from the given root {@link EPackage}.
61+
* @param rootPackage the root {@link EPackage} of a metamodel to start from
62+
* @param fullyQualifiedName the fully qualified name of the package to search for
63+
* @return the resolved {@link EPackage} or <code>null</code> if none was found
64+
*/
65+
def static EPackage findEPackage(EPackage ePackage, String fullName) {
66+
if (isSame(ePackage, fullName)) {
67+
return ePackage
68+
}
69+
return findESubpackage(PATH.cutFirstSegment(fullName), ePackage)
70+
}
71+
72+
/**
73+
* Finds a specific {@link EStructuralFeature} in an {@link EClass}.
74+
* @param fullName is the fully qualified name of the desired {@link EStructuralFeature}.
75+
* @param eClass is the {@link EClass} which contains the {@link EStructuralFeature}.
76+
* @return the {@link EStructuralFeature} or null if there is none with the specified name.
77+
*/
78+
def static EStructuralFeature findEStructuralFeature(String fullName, EClass eClass) {
79+
for (EStructuralFeature feature : eClass.getEStructuralFeatures()) {
80+
if (isSame(feature, fullName)) {
81+
return feature
82+
}
83+
}
84+
return null
85+
}
86+
87+
/**
88+
* Finds a specific {@link EStructuralFeature} in an {@link EPackage}.
89+
* @param fullName is the fully qualified name of the desired {@link EStructuralFeature}.
90+
* @param eClassName is the fully qualified name of the {@link EClass} that contains the {@link EStructuralFeature}.
91+
* @param ePackage ePackage is the {@link EPackage} which contains the {@link EClass} that contains the{@link EStructuralFeature}.
92+
* @return the {@link EStructuralFeature} or null if there is none with the specified name.
93+
*/
94+
def static EStructuralFeature findEStructuralFeature(String fullName, String eClassName, EPackage ePackage) {
95+
var EClass eClass = findEClass(eClassName, ePackage)
96+
if (eClass !== null) { // if class was found
97+
return findEStructuralFeature(fullName, eClass) // search for feature
98+
}
99+
return null
100+
}
101+
102+
/**
103+
* Returns the list of names of all non-abstract {@link EClass}es in an {@link EPackage}.
104+
*/
105+
def static List<String> getClassNames(EPackage ePackage) {
106+
return ePackage.EClassifiers.filter(EClass).filter[!interface && !abstract].map[name].toList
107+
}
108+
109+
/**
110+
* Compares the name of an ENamedElement with a String.
111+
*/
112+
def private static boolean isSame(ENamedElement element, String elementName) {
113+
return element.getName().equals(elementName)
114+
}
115+
}

src/main/java/jce/util/MetamodelSearcher.java

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)