From 7964efe0368e4977d3019f734a25afd8b8549ab5 Mon Sep 17 00:00:00 2001 From: Robert Doczi Date: Fri, 11 Dec 2015 18:05:38 +0100 Subject: [PATCH] #21 package renaming (stil bugged) --- .../UMLToJavaTransformation.xtend | 5 +-- .../transformation/queries/umlQueries.eiq | 25 +++++++---- .../rules/AssociationRules.xtend | 30 +++++++++---- .../transformation/rules/ClassRules.xtend | 43 ++++++++++--------- .../transformation/rules/PackageRules.xtend | 35 ++++++++++++--- .../jdt/jdtmanipulator/IJDTManipulator.xtend | 2 +- .../jdtmanipulator/impl/JDTManipulator.xtend | 13 +++++- 7 files changed, 102 insertions(+), 51 deletions(-) diff --git a/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/UMLToJavaTransformation.xtend b/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/UMLToJavaTransformation.xtend index bb7afbd..88d2fcf 100644 --- a/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/UMLToJavaTransformation.xtend +++ b/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/UMLToJavaTransformation.xtend @@ -34,8 +34,7 @@ class UMLToJavaTransformation { JDTManipulator manipulator - boolean initialized = false - + boolean initialized = false new(IJavaProject project, Model model) { manipulator = new JDTManipulator(new JDTElementLocator(project)) @@ -54,7 +53,7 @@ class UMLToJavaTransformation { val ruleProviders = newArrayList ruleProviders += new PackageRules ruleProviders += new ClassRules - ruleProviders += new AssociationRules + //ruleProviders += new AssociationRules ruleProviders.forEach[initialize(manipulator, elementNameRegistry)] diff --git a/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/queries/umlQueries.eiq b/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/queries/umlQueries.eiq index c3c6c02..c94d120 100644 --- a/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/queries/umlQueries.eiq +++ b/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/queries/umlQueries.eiq @@ -12,12 +12,8 @@ pattern umlClass(umlClass: Class) { neg find stereotype(umlClass); neg find templateParameterClass(umlClass); neg find templateBindingClass(umlClass); -} - -pattern packageOfClass(umlClass, umlPackage) { - find umlClass(umlClass); - find umlPackage(umlPackage); - Package.packagedElement(umlPackage, umlClass); + + Class.name(umlClass, _); } private pattern umlAssociationClass(associationClass) { @@ -67,16 +63,19 @@ pattern umlPackage(umlPackage) { neg find profile(umlPackage); } -pattern leafPackage(umlPackage, qualifiedName) { +pattern leafPackage(umlPackage) { find umlPackage(umlPackage); neg find packageInPackage(umlPackage, _); - Package.qualifiedName(umlPackage, qualifiedName); } -private pattern packageInPackage(parent, child) { +pattern packageInPackage(parent, child) { find umlPackage(parent); find umlPackage(child); Package.packagedElement(parent, child); +} or { + find model(parent); + find umlPackage(child); + Package.packagedElement(parent, child); } private pattern model(model) { @@ -87,6 +86,14 @@ private pattern profile(it) { Profile(it); } +pattern umlAssociation(association: Association, srcEnd : Property, trgEnd : Property) { + Association.memberEnd(association, srcEnd); + Property.type(srcEnd, _); + Association.navigableOwnedEnd(association, trgEnd); + Property.type(trgEnd, _); + srcEnd != trgEnd; +} + pattern associationOfClass(trgType : Type, srcType : Type, srcQn, association: Association, srcEnd : Property, trgEnd : Property) { Association.memberEnd(association, srcEnd); Property.type(srcEnd, srcType); diff --git a/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/AssociationRules.xtend b/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/AssociationRules.xtend index f5b1a6b..74a6054 100644 --- a/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/AssociationRules.xtend +++ b/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/AssociationRules.xtend @@ -10,6 +10,7 @@ import org.eclipse.incquery.runtime.evm.specific.event.IncQueryActivationStateEn import org.eclipse.uml2.uml.Element import static extension com.incquerylabs.evm.jdt.java.transformation.util.QualifiedNameUtil.* +import com.incquerylabs.evm.jdt.java.transformation.queries.util.UmlAssociationQuerySpecification class AssociationRules extends RuleProvider { @@ -18,29 +19,40 @@ class AssociationRules extends RuleProvider { ] override initialize(JDTManipulator manipulator, Map elementNameRegistry) { - addRule( - ruleFactory.createRule.precondition(AssociationOfClassQuerySpecification::instance).action( - IncQueryActivationStateEnum.APPEARED) [ - debug('''Association appeared: <«it.association.qualifiedName»> from:<«it.srcType.qualifiedName»> to:<«it.trgType.qualifiedName»>''') + addRule(ruleFactory.createRule.precondition(UmlAssociationQuerySpecification::instance) + .action(IncQueryActivationStateEnum::APPEARED) [ + debug('''Association appeared: <«it.association.qualifiedName»> from:<«it.srcEnd.type.qualifiedName»> to:<«it.trgEnd.type.qualifiedName»>''') val containingClassQN = it.srcEnd.type.qualifiedName.toJDTQN val fieldName = it.trgEnd.name val typeQN = it.trgEnd.type.qualifiedName.toJDTQN manipulator.createField(containingClassQN, fieldName, typeQN) elementNameRegistry.put(it.trgEnd, it.trgEnd.name) - ].action(IncQueryActivationStateEnum.UPDATED) [ - debug('''Association updated: <«it.association.qualifiedName»> from:<«it.srcType.qualifiedName»> to:<«it.trgType.qualifiedName»>''') + ].action(IncQueryActivationStateEnum::UPDATED) [ + debug('''Association updated: <«it.association.qualifiedName»> from:<«it.srcEnd.type.qualifiedName»> to:<«it.trgEnd.type.qualifiedName»>''') val fieldName = elementNameRegistry.get(it.trgEnd) manipulator.updateField((it.srcEnd.type.qualifiedName + "::" + fieldName).toJDTQN, it.trgEnd.type.qualifiedName.toJDTQN, it.trgEnd.name) elementNameRegistry.put(it.trgEnd, it.trgEnd.name) - ].action(IncQueryActivationStateEnum.DISAPPEARED) [ + ].addLifeCycle(Lifecycles::getDefault(true, false)).build, 4 + ) + + addRule( + ruleFactory.createRule.precondition(AssociationOfClassQuerySpecification::instance) + .action(IncQueryActivationStateEnum.APPEARED) [ + debug('''Association appeared: <«it.association.qualifiedName»> from:<«it.srcEnd.type.qualifiedName»> to:<«it.trgEnd.type.qualifiedName»>''') + val containingClassQN = it.srcEnd.type.qualifiedName.toJDTQN + val fieldName = it.trgEnd.name + val typeQN = it.trgEnd.type.qualifiedName.toJDTQN + manipulator.createField(containingClassQN, fieldName, typeQN) + elementNameRegistry.put(it.trgEnd, it.trgEnd.name) + ] + .action(IncQueryActivationStateEnum.DISAPPEARED) [ debug('''Association disappeared: <«it.association.qualifiedName»> from:<«it.srcQn»> to:<«it.trgType.qualifiedName»>''') val fieldName = it.trgEnd.name - // TODO: if a type gets deleted and an association points at it, this event gets triggered and the it.srcEnd.type will be null -> not so good behavior val fieldQN = (it.srcQn + "::" + fieldName).toJDTQN manipulator.deleteField(fieldQN) elementNameRegistry.remove(it.trgEnd.name) - ].addLifeCycle(Lifecycles::getDefault(true, true)).build, 2) + ].addLifeCycle(Lifecycles::getDefault(false, true)).build, 5) } } \ No newline at end of file diff --git a/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/ClassRules.xtend b/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/ClassRules.xtend index 7c5b1d6..ca5172f 100644 --- a/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/ClassRules.xtend +++ b/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/ClassRules.xtend @@ -11,6 +11,7 @@ import org.eclipse.incquery.runtime.evm.specific.event.IncQueryActivationStateEn import org.eclipse.uml2.uml.Element import static extension com.incquerylabs.evm.jdt.java.transformation.util.QualifiedNameUtil.* +import com.incquerylabs.evm.jdt.java.transformation.queries.util.UmlClassQuerySpecification class ClassRules extends RuleProvider { @@ -19,40 +20,40 @@ class ClassRules extends RuleProvider { ] override initialize(JDTManipulator manipulator, Map elementNameRegistry) { - addRule(ruleFactory.createRule.precondition(UmlClassInModelQuerySpecification::instance) - .action(IncQueryActivationStateEnum.APPEARED) [ - debug('''Class in model appeared: <«it.umlClass.qualifiedName»>''') + addRule(ruleFactory.createRule.precondition(UmlClassQuerySpecification::instance) + .action(IncQueryActivationStateEnum::APPEARED) [ + debug('''Class appeared: <«it.umlClass.qualifiedName»>''') val qualifiedName = it.umlClass.qualifiedName.toJDTQN manipulator.createClass(qualifiedName) elementNameRegistry.put(it.umlClass, it.umlClass.name) - ].action(IncQueryActivationStateEnum.UPDATED) [ - debug('''Class in model updated: <«it.umlClass.qualifiedName»>''') + ] + .action(IncQueryActivationStateEnum::UPDATED) [ val clazzName = elementNameRegistry.get(it.umlClass) - manipulator.updateClass((it.model.qualifiedName + "::" + clazzName).toJDTQN, it.umlClass.name) + val qualifiedName = (it.umlClass.package.qualifiedName + "::" + clazzName) + debug('''Class updated: <«qualifiedName»>''') + manipulator.updateClass(qualifiedName.toJDTQN, it.umlClass.name) elementNameRegistry.put(it.umlClass, it.umlClass.name) - ].action(IncQueryActivationStateEnum.DISAPPEARED) [ - debug('''Class in model disappeared: <«it.umlClass.qualifiedName»>''') + ].addLifeCycle(Lifecycles::getDefault(true, true)).build, 2 + ) + + addRule(ruleFactory.createRule.precondition(UmlClassInModelQuerySpecification::instance) + // TODO: use proper lifecycle instead of this hack + .action(IncQueryActivationStateEnum::APPEARED) [] + .action(IncQueryActivationStateEnum::DISAPPEARED) [ val qualifiedName = (it.model.qualifiedName + "::" + it.umlClass.name).toJDTQN + debug('''Class in model disappeared: <«qualifiedName»>''') manipulator.deleteClass(qualifiedName) elementNameRegistry.remove(it.umlClass.name) - ].addLifeCycle(Lifecycles::getDefault(false, true)).build, 1) + ].addLifeCycle(Lifecycles::getDefault(false, true)).build, 3) addRule(ruleFactory.createRule.precondition(UmlClassInPackageQuerySpecification::instance) - .action(IncQueryActivationStateEnum.APPEARED) [ - debug('''Class in package appeared: <«it.umlClass.qualifiedName»>''') - val qualifiedName = it.umlClass.qualifiedName.toJDTQN - manipulator.createClass(qualifiedName) - elementNameRegistry.put(it.umlClass, it.umlClass.name) - ].action(IncQueryActivationStateEnum.UPDATED) [ - debug('''Class in package updated: <«it.umlClass.qualifiedName»>''') - val clazzName = elementNameRegistry.get(it.umlClass) - manipulator.updateClass((it.umlPackage.qualifiedName + "::" + clazzName).toJDTQN, it.umlClass.name) - elementNameRegistry.put(it.umlClass, it.umlClass.name) - ].action(IncQueryActivationStateEnum.DISAPPEARED) [ + // TODO: use proper lifecycle instead of this hack + .action(IncQueryActivationStateEnum::APPEARED) [] + .action(IncQueryActivationStateEnum::DISAPPEARED) [ debug('''Class in package disappeared: <«it.umlClass.qualifiedName»>''') val qualifiedName = (it.umlPackage.qualifiedName + "::" + it.umlClass.name).toJDTQN manipulator.deleteClass(qualifiedName) elementNameRegistry.remove(it.umlClass.name) - ].addLifeCycle(Lifecycles::getDefault(false, true)).build, 1) + ].addLifeCycle(Lifecycles::getDefault(false, true)).build, 3) } } \ No newline at end of file diff --git a/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/PackageRules.xtend b/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/PackageRules.xtend index 02e5e74..2fd50ab 100644 --- a/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/PackageRules.xtend +++ b/com.incquerylabs.evm.jdt.java.transformation/src/com/incquerylabs/evm/jdt/java/transformation/rules/PackageRules.xtend @@ -1,6 +1,8 @@ package com.incquerylabs.evm.jdt.java.transformation.rules import com.incquerylabs.evm.jdt.java.transformation.queries.util.LeafPackageQuerySpecification +import com.incquerylabs.evm.jdt.java.transformation.queries.util.PackageInPackageQuerySpecification +import com.incquerylabs.evm.jdt.java.transformation.queries.util.UmlPackageQuerySpecification import com.incquerylabs.evm.jdt.jdtmanipulator.impl.JDTManipulator import java.util.Map import org.apache.log4j.Level @@ -21,15 +23,36 @@ class PackageRules extends RuleProvider { // only handle leaf packages, as otherwise the order they appear could be wrong in some situations addRule(ruleFactory.createRule.precondition(LeafPackageQuerySpecification::instance) .action(IncQueryActivationStateEnum::APPEARED) [ - debug('''Package appeared: <«it.qualifiedName»>''') - manipulator.createPackage(it.qualifiedName.toJDTQN) + debug('''Package appeared: <«it.umlPackage.qualifiedName»>''') + manipulator.createPackage(it.umlPackage.qualifiedName.toJDTQN) ].action(IncQueryActivationStateEnum::UPDATED) [ - debug('''Package updated: <«it.qualifiedName»>''') - ].action(IncQueryActivationStateEnum::DISAPPEARED) [ - debug('''Package disappeared: <«it.qualifiedName»>''') - manipulator.deletePackage(it.qualifiedName.toJDTQN) + val packageName = elementNameRegistry.get(it.umlPackage) + val qualifiedName = (it.umlPackage.namespace.qualifiedName + "::" + packageName) + debug('''Package updated: <«qualifiedName»>''') + manipulator.updatePackage(qualifiedName.toJDTQN, it.umlPackage.qualifiedName.toJDTQN) ].addLifeCycle(Lifecycles::getDefault(true, true)).build, 0 ) + + addRule(ruleFactory.createRule.precondition(UmlPackageQuerySpecification::instance) + .action(IncQueryActivationStateEnum::APPEARED) [ + elementNameRegistry.put(it.umlPackage, it.umlPackage.name) + ].action(IncQueryActivationStateEnum::UPDATED) [ + val packageName = elementNameRegistry.get(it.umlPackage) + val qualifiedName = (it.umlPackage.namespace.qualifiedName + "::" + packageName) + debug('''Package updated: <«qualifiedName»>''') + manipulator.updatePackage(qualifiedName.toJDTQN, it.umlPackage.qualifiedName.toJDTQN) + ].addLifeCycle(Lifecycles::getDefault(true, true)).build, 0 + ) + + addRule(ruleFactory.createRule.precondition(PackageInPackageQuerySpecification::instance) + // TODO: use proper lifecycle instead of this hack + .action(IncQueryActivationStateEnum::APPEARED) [] + .action(IncQueryActivationStateEnum::DISAPPEARED) [ + debug('''Package disappeared: <«it.child.name»> in <«it.parent.qualifiedName»>''') + val packageQualifiedName = it.parent.qualifiedName + "::" + it.child.name + manipulator.deletePackage(packageQualifiedName.toJDTQN) + ].addLifeCycle(Lifecycles::getDefault(false, true)).build, 1 + ) } } \ No newline at end of file diff --git a/com.incquerylabs.evm.jdt.jdtmanipulator/src/com/incquerylabs/evm/jdt/jdtmanipulator/IJDTManipulator.xtend b/com.incquerylabs.evm.jdt.jdtmanipulator/src/com/incquerylabs/evm/jdt/jdtmanipulator/IJDTManipulator.xtend index 2d81955..a644bb7 100644 --- a/com.incquerylabs.evm.jdt.jdtmanipulator/src/com/incquerylabs/evm/jdt/jdtmanipulator/IJDTManipulator.xtend +++ b/com.incquerylabs.evm.jdt.jdtmanipulator/src/com/incquerylabs/evm/jdt/jdtmanipulator/IJDTManipulator.xtend @@ -17,7 +17,7 @@ interface IJDTManipulator { def void deleteField(QualifiedName qualifiedName) def void deleteMethod(QualifiedName qualifiedName) - def void changePackageName(QualifiedName oldQualifiedName, String name) + def void updatePackage(QualifiedName oldQualifiedName, QualifiedName newQualifiedName) def void updateClass(QualifiedName oldQualifiedName, String name) def void updateField(QualifiedName oldQualifiedName, QualifiedName type, String name) def void changeMethodName(QualifiedName oldQualifiedName, String name) diff --git a/com.incquerylabs.evm.jdt.jdtmanipulator/src/com/incquerylabs/evm/jdt/jdtmanipulator/impl/JDTManipulator.xtend b/com.incquerylabs.evm.jdt.jdtmanipulator/src/com/incquerylabs/evm/jdt/jdtmanipulator/impl/JDTManipulator.xtend index ac269eb..19351d2 100644 --- a/com.incquerylabs.evm.jdt.jdtmanipulator/src/com/incquerylabs/evm/jdt/jdtmanipulator/impl/JDTManipulator.xtend +++ b/com.incquerylabs.evm.jdt.jdtmanipulator/src/com/incquerylabs/evm/jdt/jdtmanipulator/impl/JDTManipulator.xtend @@ -121,8 +121,17 @@ class JDTManipulator implements IJDTManipulator { throw new UnsupportedOperationException("TODO: auto-generated method stub") } - override changePackageName(QualifiedName oldQualifiedName, String name) { - throw new UnsupportedOperationException("TODO: auto-generated method stub") + override updatePackage(QualifiedName oldQualifiedName, QualifiedName newQualifiedName) { + val genFolder = rootProject.project.getFolder(GENERATION_FOLDER) + val packageRoot = rootProject.getPackageFragmentRoot(genFolder) + val existingPackage = packageRoot.getPackageFragment(oldQualifiedName.toString) + if(!existingPackage.exists) { + error('''Package <«oldQualifiedName»> cannot be renamed, does not exist''') + return + } + + debug('''Renaming package <«existingPackage.elementName»> to <«newQualifiedName.toString»>''') + existingPackage.rename(newQualifiedName.toString, false, new NullProgressMonitor) } override updateClass(QualifiedName oldQualifiedName, String name) {