diff --git a/models/designPattern/MediatorInsertionTest.Json b/models/designPattern/MediatorInsertionTest.Json deleted file mode 100644 index 404698f..0000000 --- a/models/designPattern/MediatorInsertionTest.Json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name" : "MediatorInsertionTest", - "contexts" : [ - { - "relations": [], - "transfer": { - "src": "A.String", - "dst": "B.String", - "data": { "name": "string" }, - "style": "PUSH", - "multiplicity": "1:1" - } - } - ] -} \ No newline at end of file diff --git a/src/main/java/designPatternExtensions/PresenterInsertion.java b/src/main/java/designPatternExtensions/PresenterInsertion.java deleted file mode 100644 index 311518c..0000000 --- a/src/main/java/designPatternExtensions/PresenterInsertion.java +++ /dev/null @@ -1,38 +0,0 @@ -package designPatternExtensions; - -import models.deltaAlgebra.ObjectNode; -import models.dataSynchronizationModel.PushPullValue; - -public class PresenterInsertion { - private ObjectNode src; - private ObjectNode dst; - private ObjectNode presenter; - private PushPullValue pushPullValue; - private boolean removeDirectDependency = false; - - public PresenterInsertion(ObjectNode src, ObjectNode dst, ObjectNode presenter, - PushPullValue pushPullValue, boolean removeDirectDependency) { - this.src = src; - this.dst = dst; - this.presenter = presenter; - this.pushPullValue = pushPullValue; - this.removeDirectDependency = removeDirectDependency; - } - - public ObjectNode getSrc() { - return src; - } - - public ObjectNode getDst() { - return dst; - } - - public ObjectNode getPresenter() { - return presenter; - } - - public PushPullValue getPushPullValue() { return pushPullValue; } - - public boolean getRemoveDirectDependency() { return removeDirectDependency; } - -} \ No newline at end of file diff --git a/src/main/java/designPatternExtensions/designPattern/Builder.java b/src/main/java/designPatternExtensions/designPattern/Builder.java index 3e6957b..504bfcf 100644 --- a/src/main/java/designPatternExtensions/designPattern/Builder.java +++ b/src/main/java/designPatternExtensions/designPattern/Builder.java @@ -17,12 +17,6 @@ DataSynchronizationContext dtc = design.getSynchronizationContexts().getFirst(); ObjectNode builder = dtc.getSrcTopObject(); ObjectNode hero = dtc.getDstTopObject(); -// -// MediatorInsertion mi = new MediatorInsertion(main, builder, new ObjectNode("Hero")); -// design.addMediatorInsertion(mi); - -// PresenterInsertion pi = new PresenterInsertion(hero, builder, new ObjectNode("Presenter"), dtc.getTransferStyle(), true); -// design.addPresenterInsertion(pi); return design; } diff --git a/src/main/java/designPatternExtensions/designPattern/MediatorInsertionTest.java b/src/main/java/designPatternExtensions/designPattern/MediatorInsertionTest.java deleted file mode 100644 index aef2750..0000000 --- a/src/main/java/designPatternExtensions/designPattern/MediatorInsertionTest.java +++ /dev/null @@ -1,38 +0,0 @@ - -package designPatternExtensions.designPattern; - - import designPatternExtensions.*; - import models.deltaAlgebra.ObjectNode; - import models.dataSynchronizationModel.*; - -public class MediatorInsertionTest implements DesignPattern { - - public DataSynchronizationDesign create() throws IllegalRelationException { - DataSynchronizationDesign design = ModelLoader.load("models/designPattern/MediatorInsertionTest.Json"); - DataSynchronizationContext dtc = design.getSynchronizationContexts().getFirst(); - ObjectNode a = dtc.getSrcTopObject(); - ObjectNode b = dtc.getDstTopObject(); - -// ObjectNode me = new ObjectNode("Me"); -// MediatorInsertion mi = new MediatorInsertion(a, b, me, dtc.getTransferStyle()); -// design.addMediatorInsertion(mi); - - ObjectNode presenter = new ObjectNode("Presenter"); - PresenterInsertion pi = new PresenterInsertion(a, b, presenter, dtc.getTransferStyle(), false); - design.addPresenterInsertion(pi); - - return design; - } -} - - -//A側Mediatorのsetter作成 - -//PresenterのSetDataをupdateDataとして命名されるように - -//Contextを持たせる理由 -//DataTransferDesignにはContextのリストがあり、その時に、いずれかのコンテキストのデータ転送かを -//判別する必要があると思うので、それを区別するためにMediatorInsertionの引数に渡している - -// Meが主導に時はPULL->PUSHになる -> PresenterInsertion or ControllerInsertionとして扱い -//PULLPULL PUSHPUSHはMediatorInsertionとして扱う \ No newline at end of file diff --git a/src/main/java/generators/ASTGenerator.java b/src/main/java/generators/ASTGenerator.java index 109e402..5d49090 100644 --- a/src/main/java/generators/ASTGenerator.java +++ b/src/main/java/generators/ASTGenerator.java @@ -9,11 +9,12 @@ import designPatternExtensions.DependencyInversion; import designPatternExtensions.InterfaceNode; import designPatternExtensions.MediatorInsertion; -import designPatternExtensions.PresenterInsertion; import models.dataConstraintModel.MapType; import models.deltaAlgebra.*; import models.dataSynchronizationModel.*; +import javax.print.attribute.standard.Media; + public class ASTGenerator { public static final String getterPrefix = "get"; public static final String setterPrefix = "set"; @@ -44,9 +45,6 @@ for (MediatorInsertion mi: dataSynchronizationDesign.getMediatorInsertions()) { codebase = weaveMediatorInsertion(codebase, mi); } - for (PresenterInsertion pi: dataSynchronizationDesign.getPresenterInsertions()) { - codebase = weavePresenterInsertion(codebase, pi); - } for (DependencyInversion di: dataSynchronizationDesign.getDependencyInversions()) { codebase = weaveDependencyInversion(codebase, di); } @@ -383,221 +381,258 @@ } + private static class MediatorContext { + final Codebase codebase; + final String srcName, dstName, mediatorName; + String mediatorFieldName; + final String dstFieldName; + final models.algebra.Type dstType, mediatorType; + final TypeDeclaration srcClass, dstClass, mediatorClass; + + boolean toMany; + FieldDeclaration srcDstMapField; + FieldDeclaration dstField; + FieldDeclaration srcFieldToReplace; + String oldFieldName; + + MediatorContext(Codebase codebase, ObjectNode srcNode, ObjectNode dstNode, ObjectNode mediatorNode){ + this.codebase = codebase; + this.srcName = toComponentName(srcNode.getName()); + this.dstName = toComponentName(dstNode.getName()); + this.mediatorName = toComponentName(mediatorNode.getName()); + this.mediatorFieldName = toVariableName(mediatorName); + this.dstFieldName = toVariableName(dstName); + this.dstType = codebase.getComponentType(dstName); + this.mediatorType = codebase.getComponentType(mediatorName); + this.mediatorClass = createClass(codebase, mediatorName); + this.srcClass = createClass(codebase, srcName); + this.dstClass = createClass(codebase, dstName); + } + } + public static Codebase weaveMediatorInsertion(Codebase codebase, MediatorInsertion mediatorInsertion) { - PushPullValue style = mediatorInsertion.getPushPullValue(); ObjectNode srcNode, dstNode; - if (style == PushPullValue.PUSH) { + if (mediatorInsertion.getPushPullValue() == PushPullValue.PUSH) { srcNode = mediatorInsertion.getSrc(); dstNode = mediatorInsertion.getDst(); } else { srcNode = mediatorInsertion.getDst(); dstNode = mediatorInsertion.getSrc(); } - ObjectNode mediatorNode = mediatorInsertion.getMediator(); - String srcName = toComponentName(srcNode.getName()); - String dstName = toComponentName(dstNode.getName()); - String mediatorName = toComponentName(mediatorNode.getName()); - String mediatorFieldName = toVariableName(mediatorName); - String dstFieldName = toVariableName(dstName); + MediatorContext mediatorContext = new MediatorContext(codebase, srcNode, dstNode, mediatorInsertion.getMediator()); - models.algebra.Type dstType = codebase.getComponentType(dstName); + determineMediatorField(mediatorContext); + addMediatorConstructor(mediatorContext); + addMediatorDelegationMethods(mediatorContext); + findSrcFieldToReplace(mediatorContext); + if (mediatorContext.srcFieldToReplace != null) { + replaceSrcDependencyWithMediator(mediatorContext); + moveSrcMethodsToMediator(mediatorContext); + replaceRemainingFieldReferences(mediatorContext); + } - TypeDeclaration mediatorClass = createClass(codebase, mediatorName); - models.algebra.Type mediatorType = codebase.getComponentType(mediatorName); - TypeDeclaration srcClass = createClass(codebase, srcName); - TypeDeclaration dstClass = createClass(codebase, dstName); + return codebase; + } - FieldDeclaration srcDstMapField = null; - for (FieldDeclaration field: srcClass.getFields()) { + private static void determineMediatorField(MediatorContext mediatorContext) { + mediatorContext.srcDstMapField = null; + for (FieldDeclaration field: mediatorContext.srcClass.getFields()) { if (field.getType() instanceof MapType) { MapType mapType = (MapType) field.getType(); - if (mapType.getValueType() != null && mapType.getValueType().getTypeName().equals(dstType.getTypeName())) { - srcDstMapField = field; + if (mapType.getValueType() != null && mapType.getValueType().getTypeName().equals(mediatorContext.dstType.getTypeName())) { + mediatorContext.srcDstMapField = field; break; } } } - boolean toMany = srcDstMapField != null; + mediatorContext.toMany = mediatorContext.srcDstMapField != null; - FieldDeclaration dstField; - if (toMany) { - MapType srcMapType = (MapType) srcDstMapField.getType(); + if (mediatorContext.toMany) { + MapType srcMapType = (MapType) mediatorContext.srcDstMapField.getType(); FieldDeclaration existingMediatorMapField = null; - for (FieldDeclaration field: mediatorClass.getFields()) { + for (FieldDeclaration field: mediatorContext.mediatorClass.getFields()) { if (field.getType() instanceof MapType) { MapType mapType = (MapType) field.getType(); - if (mapType.getValueType() != null && mapType.getValueType().getTypeName().equals(dstType.getTypeName()) + if (mapType.getValueType() != null && mapType.getValueType().getTypeName().equals(mediatorContext.dstType.getTypeName()) && mapType.getKeyType().getTypeName().equals(srcMapType.getKeyType().getTypeName())) { existingMediatorMapField = field; break; } } } - dstField = (existingMediatorMapField != null) + mediatorContext.dstField = (existingMediatorMapField != null) ? existingMediatorMapField - : createField(mediatorClass, srcDstMapField.getName(), srcMapType); + : createField(mediatorContext.mediatorClass, mediatorContext.srcDstMapField.getName(), srcMapType); } else { - dstField = createField(mediatorClass, dstFieldName, dstType); + mediatorContext.dstField = createField(mediatorContext.mediatorClass, mediatorContext.dstFieldName, mediatorContext.dstType); } - - // Add Constructor - MethodDeclaration constructor = createConstructor(mediatorClass); - if (!hasParameterNamed(constructor, dstField.getName())) { - VariableDeclaration dstParam = new VariableDeclaration(dstField.getType(), dstField.getName()); - constructor.addParameter(dstParam); - FieldAccess dstFieldAccess = new FieldAccess(new ThisExpression(), dstField.getName()); - Assignment dstAssignment = new Assignment(dstFieldAccess, new Variable(dstField.getName())); - constructor.addUniqueStatement(new ExpressionStatement(dstAssignment)); - } - - if (!toMany) { - for (MethodDeclaration method: dstClass.getMethods()) { - if (method.isConstructor()) continue; - MethodDeclaration delegateMethod = createMethod(mediatorClass, method.getName()); - delegateMethod.setReturnType(method.getReturnType()); - List args = new ArrayList<>(); - if (method.getParameters() != null) { - for (VariableDeclaration param: method.getParameters()) { - delegateMethod.addParameter(param); - args.add(new Variable(param.getName())); - } - } - FieldAccess dstAccess = new FieldAccess(dstField.getName()); - MethodInvocation callDst = new MethodInvocation(dstAccess, method.getName(), args); - if (method.getReturnType() != null) { - ReturnStatement returnStatement = new ReturnStatement(); - returnStatement.setExpression(callDst); - delegateMethod.addUniqueStatement(returnStatement); - } else { - delegateMethod.addUniqueStatement(new ExpressionStatement(callDst)); - } - } - } - - FieldDeclaration srcFieldToReplace = toMany ? srcDstMapField : null; - if (!toMany) { - for (FieldDeclaration field: srcClass.getFields()) { - if (field.getType() != null && field.getType().getTypeName().equals(dstType.getTypeName())) { - srcFieldToReplace = field; - break; - } - } - } - - if (srcFieldToReplace != null) { - FieldDeclaration existingMediatorField = null; - for (FieldDeclaration field: srcClass.getFields()) { - if (field != srcFieldToReplace && field.getType() != null - && field.getType().getTypeName().equals(mediatorType.getTypeName())) { - existingMediatorField = field; - break; - } - } - String oldFieldName = srcFieldToReplace.getName(); - if (existingMediatorField != null) { - srcClass.removeField(srcFieldToReplace); - mediatorFieldName = existingMediatorField.getName(); - } else { - srcFieldToReplace.setType(mediatorType); - srcFieldToReplace.setName(mediatorFieldName); - } - - // Add Mediator Setter - MethodDeclaration mediatorSetter = createMethod(srcClass, setterPrefix + mediatorName); - if (mediatorSetter.getParameters() == null || mediatorSetter.getParameters().isEmpty()) { - VariableDeclaration mediatorParam = new VariableDeclaration(mediatorType, mediatorFieldName); - mediatorSetter.addParameter(mediatorParam); - FieldAccess mediatorFieldAccess = new FieldAccess(new ThisExpression(), mediatorFieldName); - Assignment mediatorAssignment = new Assignment(mediatorFieldAccess, new Variable(mediatorFieldName)); - mediatorSetter.addUniqueStatement(new ExpressionStatement(mediatorAssignment)); - } - - // Change the constructor parameter type in src from dst to Mediator, and rename the parameter accordingly - for (MethodDeclaration method: srcClass.getMethods()) { - if (method.isConstructor() && method.getParameters() != null) { - for (VariableDeclaration param: method.getParameters()) { - if (param.getType() != null && param.getType().getTypeName().equals(dstType.getTypeName())) { - param.setType(mediatorType); - param.setName(mediatorFieldName); - } - } - } - } - - List methodsToMove = new ArrayList<>(); - for (MethodDeclaration method: srcClass.getMethods()) { - if (method.isConstructor()) continue; - if (methodReferencesField(method, oldFieldName)) { - methodsToMove.add(method); - } - } - for (MethodDeclaration method: methodsToMove) { - srcClass.removeMethod(method); - List danglingFields = new ArrayList<>(); - if (method.getBody() != null) { - for (Statement statement: method.getBody().getStatements2()) { - collectDanglingValueFields(statement, srcClass, mediatorClass, oldFieldName, danglingFields); - } - } - Set danglingFieldNames = new HashSet<>(); - for (FieldDeclaration extra: danglingFields) { - danglingFieldNames.add(extra.getName()); - if (!hasParameterNamed(method, extra.getName())) { - method.addParameter(new VariableDeclaration(extra.getType(), extra.getName())); - } - } - if (!danglingFieldNames.isEmpty() && method.getBody() != null) { - for (Statement statement: method.getBody().getStatements2()) { - rewriteDanglingFieldsAsParams(statement, danglingFieldNames); - } - } - - MethodDeclaration existingOnMediator = null; - for (MethodDeclaration existing: mediatorClass.getMethods()) { - if (existing.getName().equals(method.getName())) { existingOnMediator = existing; break; } - } - if (existingOnMediator == null) { - mediatorClass.addMethod(method); - } - - MethodDeclaration stub = createMethod(srcClass, method.getName()); - stub.setReturnType(method.getReturnType()); - List args = new ArrayList<>(); - if (method.getParameters() != null) { - for (VariableDeclaration param: method.getParameters()) { - if (danglingFieldNames.contains(param.getName())) { - args.add(new FieldAccess(param.getName())); - } else { - stub.addParameter(param); - args.add(new Variable(param.getName())); - } - } - } - FieldAccess mediatorAccess = new FieldAccess(mediatorFieldName); - MethodInvocation callMediator = new MethodInvocation(mediatorAccess, method.getName(), args); - if (method.getReturnType() != null) { - ReturnStatement returnStatement = new ReturnStatement(); - returnStatement.setExpression(callMediator); - stub.addUniqueStatement(returnStatement); - } else { - stub.addUniqueStatement(new ExpressionStatement(callMediator)); - } - } - - for (MethodDeclaration method: srcClass.getMethods()) { - if (method.getBody() == null) continue; - for (Statement statement: method.getBody().getStatements2()) { - replaceFieldNameInStatement(statement, oldFieldName, mediatorFieldName); - } - } - } - - return codebase; } - private static void replaceFieldNameInStatement(Statement statement, String oldName, String newName) { - if (statement instanceof ExpressionStatement) { - replaceFieldNameInExpression(((ExpressionStatement) statement).getExpression(), oldName, newName); + private static void addMediatorConstructor(MediatorContext mediatorContext) { + MethodDeclaration constructor = createConstructor(mediatorContext.mediatorClass); + if (!hasParameterNamed(constructor, mediatorContext.dstField.getName())) { + VariableDeclaration dstParam = new VariableDeclaration(mediatorContext.dstField.getType(), mediatorContext.dstField.getName()); + constructor.addParameter(dstParam); + FieldAccess dstFieldAccess = new FieldAccess(new ThisExpression(), mediatorContext.dstField.getName()); + Assignment dstAssignment = new Assignment(dstFieldAccess, new Variable(mediatorContext.dstField.getName())); + constructor.addUniqueStatement(new ExpressionStatement(dstAssignment)); + } + } + + private static void addMediatorDelegationMethods(MediatorContext mediatorContext) { + if (mediatorContext.toMany) return; + for (MethodDeclaration method: mediatorContext.dstClass.getMethods()) { + if (method.isConstructor()) continue; + MethodDeclaration delegateMethod = createMethod(mediatorContext.mediatorClass, method.getName()); + delegateMethod.setReturnType(method.getReturnType()); + List args = new ArrayList<>(); + if (method.getParameters() != null) { + for (VariableDeclaration param: method.getParameters()) { + delegateMethod.addParameter(param); + args.add(new Variable(param.getName())); + } + } + MethodInvocation callDst = new MethodInvocation(new FieldAccess(mediatorContext.dstField.getName()), method.getName(), args); + if (method.getReturnType() != null) { + ReturnStatement returnStatement = new ReturnStatement(); + returnStatement.setExpression(callDst); + delegateMethod.addUniqueStatement(returnStatement); + } else { + delegateMethod.addUniqueStatement(new ExpressionStatement(callDst)); + } + } + } + + private static void findSrcFieldToReplace(MediatorContext mediatorContext) { + if (mediatorContext.toMany) { + mediatorContext.srcFieldToReplace = mediatorContext.srcDstMapField; + return; + } + for (FieldDeclaration field: mediatorContext.srcClass.getFields()) { + if (field.getType() != null && field.getType().getTypeName().equals(mediatorContext.dstType.getTypeName())) { + mediatorContext.srcFieldToReplace = field; + return; + } + } + mediatorContext.srcFieldToReplace = null; + } + + private static void replaceSrcDependencyWithMediator(MediatorContext mediatorContext) { + FieldDeclaration existingMediatorField = null; + for (FieldDeclaration field: mediatorContext.srcClass.getFields()) { + if (field != mediatorContext.srcFieldToReplace && field.getType() != null + && field.getType().getTypeName().equals(mediatorContext.mediatorType.getTypeName())) { + existingMediatorField = field; + break; + } + } + mediatorContext.oldFieldName = mediatorContext.srcFieldToReplace.getName(); + if (existingMediatorField != null) { + mediatorContext.srcClass.removeField(mediatorContext.srcFieldToReplace); + mediatorContext.mediatorFieldName = existingMediatorField.getName(); + } else { + mediatorContext.srcFieldToReplace.setType(mediatorContext.mediatorType); + mediatorContext.srcFieldToReplace.setName(mediatorContext.mediatorFieldName); + } + + MethodDeclaration mediatorSetter = createMethod(mediatorContext.srcClass, setterPrefix + mediatorContext.mediatorName); + if (mediatorSetter.getParameters() == null || mediatorSetter.getParameters().isEmpty()) { + VariableDeclaration mediatorParam = new VariableDeclaration(mediatorContext.mediatorType, mediatorContext.mediatorFieldName); + mediatorSetter.addParameter(mediatorParam); + FieldAccess mediatorFieldAccess = new FieldAccess(new ThisExpression(), mediatorContext.mediatorFieldName); + Assignment mediatorAssignment = new Assignment(mediatorFieldAccess, new Variable(mediatorContext.mediatorFieldName)); + mediatorSetter.addUniqueStatement(new ExpressionStatement(mediatorAssignment)); + } + + for (MethodDeclaration method: mediatorContext.srcClass.getMethods()) { + if (method.isConstructor() && method.getParameters() != null) { + for (VariableDeclaration param: method.getParameters()) { + if (param.getType() != null && param.getType().getTypeName().equals(mediatorContext.dstType.getTypeName())) { + param.setType(mediatorContext.mediatorType); + param.setName(mediatorContext.mediatorFieldName); + } + } + } + } + } + + private static void moveSrcMethodsToMediator(MediatorContext ctx) { + List methodsToMove = new ArrayList<>(); + for (MethodDeclaration method: ctx.srcClass.getMethods()) { + if (method.isConstructor()) continue; + if (methodReferencesField(method, ctx.oldFieldName)) { + methodsToMove.add(method); + } + } + for (MethodDeclaration method: methodsToMove) { + moveSingleMethodToMediator(ctx, method); + } + } + + private static void moveSingleMethodToMediator(MediatorContext ctx, MethodDeclaration method) { + ctx.srcClass.removeMethod(method); + List danglingFields = new ArrayList<>(); + if (method.getBody() != null) { + for (Statement statement: method.getBody().getStatements2()) { + collectDanglingValueFields(statement, ctx.srcClass, ctx.mediatorClass, ctx.oldFieldName, danglingFields); + } + } + Set danglingFieldNames = new HashSet<>(); + for (FieldDeclaration extra: danglingFields) { + danglingFieldNames.add(extra.getName()); + if (!hasParameterNamed(method, extra.getName())) { + method.addParameter(new VariableDeclaration(extra.getType(), extra.getName())); + } + } + if (!danglingFieldNames.isEmpty() && method.getBody() != null) { + for (Statement statement: method.getBody().getStatements2()) { + rewriteDanglingFieldsAsParams(statement, danglingFieldNames); + } + } + + MethodDeclaration existingOnMediator = null; + for (MethodDeclaration existing: ctx.mediatorClass.getMethods()) { + if (existing.getName().equals(method.getName())) { existingOnMediator = existing; break; } + } + if (existingOnMediator == null) { + ctx.mediatorClass.addMethod(method); + } + + createSrcStubForMovedMethod(ctx, method, danglingFieldNames); + } + + private static void createSrcStubForMovedMethod(MediatorContext ctx, MethodDeclaration method, Set danglingFieldNames) { + MethodDeclaration stub = createMethod(ctx.srcClass, method.getName()); + stub.setReturnType(method.getReturnType()); + List args = new ArrayList<>(); + if (method.getParameters() != null) { + for (VariableDeclaration param: method.getParameters()) { + if (danglingFieldNames.contains(param.getName())) { + args.add(new FieldAccess(param.getName())); + } else { + stub.addParameter(param); + args.add(new Variable(param.getName())); + } + } + } + MethodInvocation callMediator = new MethodInvocation(new FieldAccess(ctx.mediatorFieldName), method.getName(), args); + if (method.getReturnType() != null) { + ReturnStatement returnStatement = new ReturnStatement(); + returnStatement.setExpression(callMediator); + stub.addUniqueStatement(returnStatement); + } else { + stub.addUniqueStatement(new ExpressionStatement(callMediator)); + } + } + + private static void replaceRemainingFieldReferences(MediatorContext ctx) { + for (MethodDeclaration method: ctx.srcClass.getMethods()) { + if (method.getBody() == null) continue; + for (Statement statement: method.getBody().getStatements2()) { + if (statement instanceof ExpressionStatement) { + replaceFieldNameInExpression(((ExpressionStatement) statement).getExpression(), ctx.oldFieldName, ctx.mediatorFieldName); + } + } } } @@ -632,107 +667,6 @@ } } - - public static Codebase weavePresenterInsertion(Codebase codebase, PresenterInsertion presenterInsertion) { - ObjectNode srcNode = presenterInsertion.getSrc(); - ObjectNode dstNode = presenterInsertion.getDst(); - ObjectNode presenterNode = presenterInsertion.getPresenter(); - String srcName = toComponentName(srcNode.getName()); - String dstName = toComponentName(dstNode.getName()); - String presenterName = toComponentName(presenterNode.getName()); - String srcFieldName = toVariableName(srcName); - String dstFieldName = toVariableName(dstName); - - models.algebra.Type srcType = codebase.getComponentType(srcName); - models.algebra.Type dstType = codebase.getComponentType(dstName); - - TypeDeclaration presenterClass = createClass(codebase, presenterName); - - FieldDeclaration srcField = createField(presenterClass, srcFieldName, srcType); - FieldDeclaration dstField = createField(presenterClass, dstFieldName, dstType); - - MethodDeclaration constructor = createConstructor(presenterClass); - - if (!hasParameterNamed(constructor, srcFieldName)) { - constructor.addParameter(new VariableDeclaration(srcType, srcFieldName)); - constructor.addUniqueStatement(new ExpressionStatement( - new Assignment(new FieldAccess(new ThisExpression(), srcField.getName()), new Variable(srcFieldName)))); - } - if (!hasParameterNamed(constructor, dstFieldName)) { - constructor.addParameter(new VariableDeclaration(dstType, dstFieldName)); - constructor.addUniqueStatement(new ExpressionStatement( - new Assignment(new FieldAccess(new ThisExpression(), dstField.getName()), new Variable(dstFieldName)))); - } - - TypeDeclaration srcClass = createClass(codebase, srcName); - TypeDeclaration dstClass = createClass(codebase, dstName); - - List getters = new ArrayList<>(); - List boundProperties = new ArrayList<>(); - - for (MethodDeclaration getter: srcClass.getMethods()) { - if (getter.isConstructor() || getter.getReturnType() == null) continue; - if (!getter.getName().startsWith(getterPrefix)) continue; - getters.add(getter); - boundProperties.add(getter.getName().substring(getterPrefix.length())); - } - - for (FieldDeclaration field: srcClass.getFields()) { - if (field.getType() == null) continue; - if (field.getType().getTypeName().equals(dstType.getTypeName())) continue; - - String propertyName = toComponentName(field.getType().getTypeName()); - if (boundProperties.contains(propertyName)) continue; - - MethodDeclaration newGetter = createMethod(srcClass, getterPrefix + propertyName); - if (newGetter.getReturnType() == null) { - ReturnStatement returnStatement = new ReturnStatement(); - returnStatement.setExpression(new FieldAccess(new ThisExpression(), field.getName())); - newGetter.addUniqueStatement(returnStatement); - newGetter.setReturnType(field.getType()); - } - getters.add(newGetter); - boundProperties.add(propertyName); - } - - for (MethodDeclaration getter: getters) { - String propertyName = getter.getName().substring(getterPrefix.length()); - String setterName = setterPrefix + propertyName; - - MethodDeclaration setter = createMethod(dstClass, setterName); - if (setter.getReturnType() == null && (setter.getParameters() == null || setter.getParameters().isEmpty())) { - models.algebra.Type propertyType = getter.getReturnType(); - VariableDeclaration setterParam = new VariableDeclaration(propertyType, toVariableName(propertyName)); - setter.addParameter(setterParam); - FieldDeclaration propertyField = createField(dstClass, toVariableName(propertyName), propertyType); - setter.addUniqueStatement(new ExpressionStatement( - new Assignment(new FieldAccess(new ThisExpression(), propertyField.getName()), new Variable(toVariableName(propertyName))))); - } - - String presenterMethodName = updateMethodPrefix + dstName + propertyName; - - MethodDeclaration coordinator = createMethod(presenterClass, presenterMethodName); - if (coordinator.getBody() == null || coordinator.getBody().getStatements2().isEmpty()) { - List getterArgs = new ArrayList<>(); - MethodInvocation callGetter = new MethodInvocation(new FieldAccess(srcField.getName()), getter.getName(), getterArgs); - List setterArgs = new ArrayList<>(); - setterArgs.add(callGetter); - MethodInvocation callSetter = new MethodInvocation(new FieldAccess(dstField.getName()), setter.getName(), setterArgs); - coordinator.addUniqueStatement(new ExpressionStatement(callSetter)); - } - } - - if (presenterInsertion.getRemoveDirectDependency()) { - if (presenterInsertion.getPushPullValue() == PushPullValue.PUSH) { - removeDirectDependency(srcClass, dstType); - } else { - removeDirectDependency(dstClass, srcType); - } - } - - return codebase; - } - private static boolean hasParameterNamed(MethodDeclaration method, String name) { if (method.getParameters() == null) return false; for (VariableDeclaration param: method.getParameters()) { @@ -741,33 +675,6 @@ return false; } - private static void removeDirectDependency(TypeDeclaration typeDecl, models.algebra.Type otherType) { - List fieldsToRemove = new ArrayList<>(); - for (FieldDeclaration field: typeDecl.getFields()) { - if (field.getType() != null && field.getType().getTypeName().equals(otherType.getTypeName())) { - fieldsToRemove.add(field); - } - } - if (fieldsToRemove.isEmpty()) return; - - List methodsToRemove = new ArrayList<>(); - for (MethodDeclaration method: typeDecl.getMethods()) { - if (method.isConstructor()) continue; - for (FieldDeclaration field: fieldsToRemove) { - if (methodReferencesField(method, field.getName())) { - methodsToRemove.add(method); - break; - } - } - } - for (MethodDeclaration method: methodsToRemove) { - typeDecl.removeMethod(method); - } - for (FieldDeclaration field: fieldsToRemove) { - typeDecl.removeField(field); - } - } - private static boolean methodReferencesField(MethodDeclaration method, String fieldName) { if (method.getBody() == null) return false; for (Statement statement: method.getBody().getStatements2()) { diff --git a/src/main/java/models/dataSynchronizationModel/DataSynchronizationDesign.java b/src/main/java/models/dataSynchronizationModel/DataSynchronizationDesign.java index e21fedf..be6abac 100644 --- a/src/main/java/models/dataSynchronizationModel/DataSynchronizationDesign.java +++ b/src/main/java/models/dataSynchronizationModel/DataSynchronizationDesign.java @@ -3,7 +3,6 @@ import designPatternExtensions.DependencyInversion; import designPatternExtensions.MediatorInsertion; -import designPatternExtensions.PresenterInsertion; import models.deltaAlgebra.*; import parser.*; import parser.exceptions.ExpectedJsonValue; @@ -23,7 +22,6 @@ private DeltaComplex deltaComplex; private List dependencyInversions = new ArrayList<>(); private List mediatorInsertions = new ArrayList<>(); - private List presenterInsertions = new ArrayList<>(); public DataSynchronizationDesign(DataSynchronizationContext synchronizationContexts) throws IllegalRelationException { this.synchronizationContexts = new ArrayList<>(); @@ -58,14 +56,6 @@ return mediatorInsertions; } - public void addPresenterInsertion(PresenterInsertion presenterInsertion) { - presenterInsertions.add(presenterInsertion); - } - - public List getPresenterInsertions() { - return presenterInsertions; - } - public DeltaComplex createDefaultDeltaComplex(DataSynchronizationContext transferContext) throws IllegalRelationException { Stack deltaStack = constructDefaultDeltas(transferContext); // Add all deltas.