Skip to content

Commit 1f4d2cc

Browse files
committed
EDIT: Implemented scoping for other parts of the pipeline.
1 parent 6f04597 commit 1f4d2cc

5 files changed

Lines changed: 56 additions & 34 deletions

File tree

src/main/java/jce/JavaCodeEcorification.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@
4141
*/
4242
public class JavaCodeEcorification {
4343
private static final Logger logger = LogManager.getLogger(JavaCodeEcorification.class.getName());
44-
private final FieldEncapsulator fieldEncapsulator;
4544
private final GenModelGenerator genModelGenerator;
4645
private final ImportOrganizer importOrganizer;
47-
private final InheritanceManipulator inheritanceManipulator;
4846
private final EcoreMetamodelExtraction metamodelGenerator;
4947
private final EcorificationProperties properties;
5048
private final WrapperGenerator wrapperGenerator;
@@ -57,14 +55,13 @@ public JavaCodeEcorification() {
5755
metamodelGenerator = new EcorificationExtraction(properties);
5856
genModelGenerator = new GenModelGenerator(properties);
5957
wrapperGenerator = new WrapperGenerator(properties);
60-
fieldEncapsulator = new FieldEncapsulator(properties);
6158
importOrganizer = new ImportOrganizer(properties);
62-
inheritanceManipulator = new InheritanceManipulator(properties);
6359
}
6460

6561
/**
66-
* Starts the ecorification for a specific Java project. Initializes the different steps of the Ecorification
67-
* pipeline: The extraction of an Ecore metamodel, the Ecore model code generation, the wrapper generation and the
62+
* Starts the ecorification for a specific Java project. Initializes the
63+
* different steps of the Ecorification pipeline: The extraction of an Ecore
64+
* metamodel, the Ecore model code generation, the wrapper generation and the
6865
* origin code adaption.
6966
* @param originalProject is the specific Java project as {@link IProject}.
7067
*/
@@ -81,15 +78,16 @@ public void start(IProject originalProject) {
8178
}
8279

8380
/**
84-
* 5. Encapsulates all fields of the origin code. Removes public, non-static fields and their access methods,
85-
* organizes all imports, manipulates the inheritance relations to extend the wrappers, creates default constructors
81+
* 5. Encapsulates all fields of the origin code. Removes public, non-static
82+
* fields and their access methods, organizes all imports, manipulates the
83+
* inheritance relations to extend the wrappers, creates default constructors
8684
* where they are missing.
8785
*/
8886
private void adaptOriginCode(GeneratedEcoreMetamodel metamodel, IProject project) {
89-
fieldEncapsulator.manipulate(project);
87+
new FieldEncapsulator(metamodel.getIntermediateModel(), properties).manipulate(project);
9088
new MemberRemover(metamodel, properties).manipulate(project);
9189
importOrganizer.manipulate(project);
92-
inheritanceManipulator.manipulate(project);
90+
new InheritanceManipulator(metamodel.getIntermediateModel(), properties).manipulate(project);
9391
}
9492

9593
/**
@@ -98,15 +96,15 @@ private void adaptOriginCode(GeneratedEcoreMetamodel metamodel, IProject project
9896
private void buildFactories(GeneratedEcoreMetamodel metamodel, IProject project) {
9997
new FactoryRenamer(metamodel, properties).manipulate(project);
10098
new FactoryImplementationRenamer(metamodel, properties).manipulate(project);
101-
new DefaultConstructorGenerator(properties).manipulate(project);
99+
new DefaultConstructorGenerator(properties, metamodel.getIntermediateModel()).manipulate(project);
102100
new EcoreFactoryGenerator(properties).buildFactories(metamodel, project);
103101
new PackageImplFactoryCorrector(metamodel, properties).manipulate(project);
104102
new ClassExposer(properties).manipulate(project);
105103
}
106104

107105
/**
108-
* 1. Extracts a Ecore metamodel in form of an {@link GeneratedEcoreMetamodel} from the original {@link IProject}.
109-
* Generates a {@link GenModel}.
106+
* 1. Extracts a Ecore metamodel in form of an {@link GeneratedEcoreMetamodel}
107+
* from the original {@link IProject}. Generates a {@link GenModel}.
110108
*/
111109
private GeneratedEcoreMetamodel extractMetamodel(IProject originalProject) {
112110
GeneratedEcoreMetamodel metamodel = metamodelGenerator.extract(originalProject);
@@ -116,7 +114,8 @@ private GeneratedEcoreMetamodel extractMetamodel(IProject originalProject) {
116114
}
117115

118116
/**
119-
* 6. Finishes the ecorification: Organizes all imports, rebuilds the project and notifies the user.
117+
* 6. Finishes the ecorification: Organizes all imports, rebuilds the project
118+
* and notifies the user.
120119
*/
121120
private void finish(IProject project) {
122121
importOrganizer.manipulate(project);
@@ -125,7 +124,8 @@ private void finish(IProject project) {
125124
}
126125

127126
/**
128-
* 3. Generates the wrappers, which are the classes that unify the origin code with the Ecore code.
127+
* 3. Generates the wrappers, which are the classes that unify the origin code
128+
* with the Ecore code.
129129
*/
130130
private void generateWrappers(GeneratedEcoreMetamodel metamodel, IProject project) {
131131
XtendLibraryHelper.addXtendLibs(project, properties);
@@ -149,8 +149,9 @@ private IProject getProject(SavingInformation information) {
149149
}
150150

151151
/**
152-
* 4. Manipulates the imports of the Ecore code. Every Ecore interface and every correlating implementation class
153-
* will use the origin code types instead of ecore code types.
152+
* 4. Manipulates the imports of the Ecore code. Every Ecore interface and every
153+
* correlating implementation class will use the origin code types instead of
154+
* ecore code types.
154155
*/
155156
private void manipulateEcoreImports(GeneratedEcoreMetamodel metamodel, IProject project) {
156157
new EcoreImportManipulator(metamodel, properties).manipulate(project); // 4. adapt imports

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import jce.util.jdt.ASTUtil;
1111

1212
/**
13-
* Changes the visibility of default types and default inner classes of the origin code to public to make them visible.
13+
* Changes the visibility of default types and default inner classes of the
14+
* origin code to public to make them visible.
1415
* @author Timur Saglam
1516
*/
1617
public class ClassExposer extends AbstractCodeManipulator {
@@ -25,7 +26,7 @@ public ClassExposer(EcorificationProperties properties) {
2526

2627
@Override
2728
protected void manipulate(ICompilationUnit unit) throws JavaModelException {
28-
if (properties.get(BinaryProperty.EXPOSE_CLASSES)) {
29+
if (properties.get(BinaryProperty.EXPOSE_CLASSES)) { // TODO (HIGH) Apply scope here to be minimally intrusive
2930
ASTUtil.applyVisitorModifications(unit, new ClassExpositionVisitor(properties), monitor);
3031
}
3132
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,43 @@
1010
import org.eclipse.jdt.core.dom.Modifier;
1111
import org.eclipse.jdt.core.dom.TypeDeclaration;
1212

13+
import eme.model.IntermediateModel;
1314
import jce.codemanipulation.AbstractCodeManipulator;
1415
import jce.properties.EcorificationProperties;
1516
import jce.properties.TextProperty;
1617
import jce.util.jdt.ASTUtil;
1718

1819
/**
19-
* Encapsulates the fields of the origin code. This is necessary for the removal of the fields.
20+
* Encapsulates the fields of the origin code. This is necessary for the removal
21+
* of the fields.
2022
* @author Timur Saglam
2123
*/
2224
public class FieldEncapsulator extends AbstractCodeManipulator {
25+
private IntermediateModel model;
2326

2427
/**
2528
* Simple constructor that sets the properties.
29+
* @param model is the {@link IntermediateModel} needed for the Ecorification
30+
* scope.
2631
* @param properties are the {@link EcorificationProperties}.
2732
*/
28-
public FieldEncapsulator(EcorificationProperties properties) {
33+
public FieldEncapsulator(IntermediateModel model, EcorificationProperties properties) {
2934
super(properties, properties.get(TextProperty.ECORE_PACKAGE), properties.get(TextProperty.WRAPPER_PACKAGE));
35+
this.model = model;
3036
}
3137

3238
@Override
3339
protected void manipulate(ICompilationUnit unit) throws JavaModelException {
34-
ASTUtil.applyVisitorModifications(unit, new FieldUnfinalizationVisitor(), monitor); // make fields not final
35-
CompilationUnit parsedUnit = ASTUtil.parse(unit, monitor);
36-
parsedUnit.accept(new FieldEncapsulationVisitor(properties));
40+
if (model.isTypeSelected(getPackageMemberName(unit))) { // only apply on origin type in scope
41+
ASTUtil.applyVisitorModifications(unit, new FieldUnfinalizationVisitor(), monitor); // make fields not final
42+
CompilationUnit parsedUnit = ASTUtil.parse(unit, monitor); // do not use applyVisitorModifications() here
43+
parsedUnit.accept(new FieldEncapsulationVisitor(properties)); // because refactorings are applied, not modifications
44+
}
3745
}
3846

3947
/**
40-
* {@link ASTVisitor} class that removes the modifier keyword final from all of its final fields.
48+
* {@link ASTVisitor} class that removes the modifier keyword final from all of
49+
* its final fields.
4150
*/
4251
private class FieldUnfinalizationVisitor extends ASTVisitor {
4352
@Override

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,36 @@
44
import org.eclipse.jdt.core.JavaModelException;
55
import org.eclipse.jdt.core.dom.ASTVisitor;
66

7+
import eme.model.IntermediateModel;
78
import jce.codemanipulation.AbstractCodeManipulator;
89
import jce.properties.EcorificationProperties;
910
import jce.properties.TextProperty;
1011
import jce.util.jdt.ASTUtil;
1112

1213
/**
13-
* Changes the inheritance of the origin code to let the original classes inherit from the generated unification
14-
* classes.
14+
* Changes the inheritance of the origin code to let the original classes
15+
* inherit from the generated unification classes.
1516
* @author Timur Saglam
1617
*/
1718
public class InheritanceManipulator extends AbstractCodeManipulator {
19+
private final IntermediateModel model;
1820

1921
/**
2022
* Simple constructor that sets the properties.
23+
* @param model is the {@link IntermediateModel} needed for the Ecorification
24+
* scope.
2125
* @param properties are the {@link EcorificationProperties}.
2226
*/
23-
public InheritanceManipulator(EcorificationProperties properties) {
27+
public InheritanceManipulator(IntermediateModel model, EcorificationProperties properties) {
2428
super(properties, properties.get(TextProperty.ECORE_PACKAGE), properties.get(TextProperty.WRAPPER_PACKAGE));
29+
this.model = model;
2530
}
2631

2732
@Override
2833
protected void manipulate(ICompilationUnit unit) throws JavaModelException {
29-
ASTVisitor visitor = new InheritanceManipulationVisitor(unit.getParent().getElementName(), properties);
30-
ASTUtil.applyVisitorModifications(unit, visitor, monitor);
34+
if (model.isTypeSelected(getPackageMemberName(unit))) { // only apply on origin type in scope
35+
ASTVisitor visitor = new InheritanceManipulationVisitor(unit.getParent().getElementName(), properties);
36+
ASTUtil.applyVisitorModifications(unit, visitor, monitor);
37+
}
3138
}
3239
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
import jce.util.jdt.ASTUtil;
1111

1212
/**
13-
* Removes all private non-static fields and their access methods from the origin code that have a counterpart in the
14-
* Ecore metamodel which was extracted from the origin code.
13+
* Removes all private non-static fields and their access methods from the
14+
* origin code that have a counterpart in the Ecore metamodel which was
15+
* extracted from the origin code.
1516
* @author Timur Saglam
1617
*/
1718
public class MemberRemover extends AbstractCodeManipulator {
1819
private GeneratedEcoreMetamodel metamodel;
1920

2021
/**
2122
* Simple constructor that sets the properties.
22-
* @param metamodel is the Ecore metamodel which was extracted from the origin code.
23+
* @param metamodel is the Ecore metamodel which was extracted from the origin
24+
* code.
2325
* @param properties are the {@link EcorificationProperties}.
2426
*/
2527
public MemberRemover(GeneratedEcoreMetamodel metamodel, EcorificationProperties properties) {
@@ -29,6 +31,8 @@ public MemberRemover(GeneratedEcoreMetamodel metamodel, EcorificationProperties
2931

3032
@Override
3133
protected void manipulate(ICompilationUnit unit) throws JavaModelException {
32-
ASTUtil.applyVisitorModifications(unit, new MemberRemovalVisitor(metamodel, properties), monitor);
34+
if (metamodel.getIntermediateModel().isTypeSelected(getPackageMemberName(unit))) { // only apply on origin type in scope
35+
ASTUtil.applyVisitorModifications(unit, new MemberRemovalVisitor(metamodel, properties), monitor);
36+
}
3337
}
3438
}

0 commit comments

Comments
 (0)