From 76c008890b28918ad1393662f006b520b2956d33 Mon Sep 17 00:00:00 2001 From: tsantalis Date: Tue, 31 Dec 2024 22:09:10 -0500 Subject: [PATCH] Improved UMLModelDiff.conflictingExpression() --- .../gr/uom/java/xmi/diff/UMLModelDiff.java | 71 ++++++++++++++++--- .../test/TestAllRefactorings.java | 2 +- src/test/resources/oracle/data.json | 21 ++++-- src/test/resources/oracle/expected.txt | 4 +- 4 files changed, 78 insertions(+), 20 deletions(-) diff --git a/src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java b/src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java index 3f72b08e7..9c87df8a5 100644 --- a/src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java +++ b/src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java @@ -4364,8 +4364,8 @@ else if(childFieldDeclarationMap != null) { } } } - if(addedOperationInvocations.size() > 0) { - AbstractCall addedOperationInvocation = addedOperationInvocations.get(0); + for(AbstractCall addedOperationInvocation : addedOperationInvocations) { + //AbstractCall addedOperationInvocation = addedOperationInvocations.get(0); UMLOperationBodyMapper operationBodyMapper = createMapperForExtractAndMove(addedOperation, mapper, className, addedOperationInvocation, Optional.empty()); if(!anotherAddedMethodExistsWithBetterMatchingInvocationExpression(addedOperationInvocation, addedOperation, addedOperations) && @@ -4538,19 +4538,70 @@ private boolean conflictingExpression(AbstractCall invocation, UMLOperation adde } } } - /*else if(expression != null && childFieldDeclarationMap != null && childFieldDeclarationMap.containsKey(expression)) { + else if(expression != null && childFieldDeclarationMap != null && childFieldDeclarationMap.containsKey(expression)) { VariableDeclaration variableDeclaration = childFieldDeclarationMap.get(expression); - UMLClassBaseDiff classDiff = getUMLClassDiff(addedOperation.getClassName()); UMLType type = variableDeclaration.getType(); - boolean superclassRelationship = false; - if(classDiff != null && type != null && classDiff.getNewSuperclass() != null && - classDiff.getNewSuperclass().equals(type)) { - superclassRelationship = true; + boolean superclassRelationship = superclassRelationship(addedOperation, type); + String addedOperationClassName = addedOperation.getNonQualifiedClassName(); + String addedOperationFullQualifiedClassName = addedOperation.getClassName().substring(0, addedOperation.getClassName().lastIndexOf(addedOperationClassName)-1); + String outerClassName = null; + if(addedOperationFullQualifiedClassName.contains(".")) { + String name = addedOperationFullQualifiedClassName.substring(addedOperationFullQualifiedClassName.lastIndexOf(".")+1, addedOperationFullQualifiedClassName.length()); + if(name.length() > 0 && Character.isUpperCase(name.charAt(0))) { + outerClassName = name; + } } - if(type != null && !addedOperation.getNonQualifiedClassName().equals(type.getClassType()) && !superclassRelationship) { + if(type != null && !addedOperationClassName.equals(type.getClassType()) && !type.getClassType().equals(outerClassName) && !superclassRelationship) { return true; } - }*/ + } + return false; + } + + private boolean superclassRelationship(UMLOperation addedOperation, UMLType type) { + UMLClassBaseDiff classDiff = getUMLClassDiff(addedOperation.getClassName()); + if(classDiff != null && type != null) { + if(classDiff.getNewSuperclass() != null && classDiff.getNewSuperclass().equals(type)) { + return true; + } + for(UMLType interfaceType : classDiff.getNextClass().getImplementedInterfaces()) { + if(interfaceType.equals(type)) { + return true; + } + } + + } + UMLClassBaseDiff delegateClassDiff = getUMLClassDiff(type); + if(delegateClassDiff != null && type != null) { + //check for possible delegation + for(UMLOperation operation : delegateClassDiff.getAddedOperations()) { + List invocations = operation.getAllOperationInvocations(); + for(AbstractCall invocation : invocations) { + if(invocation.matchesOperation(addedOperation, operation, delegateClassDiff, this)) { + return true; + } + } + } + for(UMLOperationBodyMapper mapper : delegateClassDiff.getOperationBodyMapperList()) { + List invocations = mapper.getContainer2().getAllOperationInvocations(); + for(AbstractCall invocation : invocations) { + if(invocation.matchesOperation(addedOperation, mapper.getContainer2(), delegateClassDiff, this)) { + return true; + } + } + } + } + UMLClass addedClass = getAddedClass(addedOperation.getClassName()); + if(addedClass != null && type != null) { + if(addedClass.getSuperclass() != null && addedClass.getSuperclass().equals(type)) { + return true; + } + for(UMLType interfaceType : addedClass.getImplementedInterfaces()) { + if(interfaceType.equals(type)) { + return true; + } + } + } return false; } diff --git a/src/test/java/org/refactoringminer/test/TestAllRefactorings.java b/src/test/java/org/refactoringminer/test/TestAllRefactorings.java index beb1d621f..0e9ed7566 100644 --- a/src/test/java/org/refactoringminer/test/TestAllRefactorings.java +++ b/src/test/java/org/refactoringminer/test/TestAllRefactorings.java @@ -17,6 +17,6 @@ public void testAllRefactorings() throws Exception { GitHistoryRefactoringMinerImpl detector = new GitHistoryRefactoringMinerImpl(); TestBuilder test = new TestBuilder(detector, REPOS, Refactorings.All.getValue()); RefactoringPopulator.feedRefactoringsInstances(Refactorings.All.getValue(), Systems.FSE.getValue(), test); - test.assertExpectationsWithGitHubAPI(12403, 22, 226); + test.assertExpectationsWithGitHubAPI(12404, 22, 226); } } diff --git a/src/test/resources/oracle/data.json b/src/test/resources/oracle/data.json index 5ecdee599..c0068d60f 100644 --- a/src/test/resources/oracle/data.json +++ b/src/test/resources/oracle/data.json @@ -12722,13 +12722,6 @@ "validation": "TP", "detectionTools": "RefactoringMiner", "validators": null - }, { - "type": "Extract And Move Method", - "description": "Extract And Move Method public edge(a int, b int, distance double, bothDirections boolean) : EdgeIteratorState extracted from public edge(a int, b int, distance double, bothDirection boolean) : EdgeIteratorState in class com.graphhopper.storage.GraphHopperStorage & moved to class com.graphhopper.storage.LevelGraphImpl", - "comment": null, - "validation": "TP", - "detectionTools": "RefactoringMiner", - "validators": null }, { "type": "Localize Parameter", "description": "Localize Parameter outdir : Directory to outdir : Directory in method public newStorage(store GraphHopperStorage) : GraphHopperStorage from class com.graphhopper.util.GHUtility", @@ -52171,6 +52164,20 @@ "validation": "CTP", "detectionTools": "RefactoringMiner", "validators": null + }, { + "type": "Rename Variable", + "description": "Rename Variable wh : File to dir : File in method public getTempDir(dirName String) : String from class org.apache.drill.BaseTestQuery", + "comment": null, + "validation": "TP", + "detectionTools": "RefactoringMiner", + "validators": null + }, { + "type": "Remove Variable Modifier", + "description": "Remove Variable Modifier final in variable wh : File in method public getInstance() : HiveTestDataGenerator from class org.apache.drill.exec.store.hive.HiveTestDataGenerator", + "comment": null, + "validation": "TP", + "detectionTools": "RefactoringMiner", + "validators": null }], "refDiffExecutionTime": 2022 }, { diff --git a/src/test/resources/oracle/expected.txt b/src/test/resources/oracle/expected.txt index 6f6b31574..a73fc7a9b 100644 --- a/src/test/resources/oracle/expected.txt +++ b/src/test/resources/oracle/expected.txt @@ -12,7 +12,7 @@ b2bbd9941be6b132a83d27c0ae02c935e1dec5dd, 33, 0, 2 f1b8ae1c44e6ba46118c2f66eae1725259acdccc, 7, 0, 0 00aa01fb90f3210d1e3027d7f759fb1085b814bd, 60, 0, 5 658a918eebcbdeb4f920c2947ca8d0e79ad86d89, 1, 0, 0 -c1b847acdc8cb90a1498b236b3bb5c81ca75c044, 78, 0, 3 +c1b847acdc8cb90a1498b236b3bb5c81ca75c044, 80, 0, 3 35893c115ba23bd62a7036a33390420f074ce660, 19, 0, 0 f166866cd68efa963534c5bc7fc9ca38e4aa2838, 1, 0, 0 04bcfe98dbe7b05e508559930c21379ece845732, 50, 0, 0 @@ -333,7 +333,7 @@ b70f7ea0ce27b5defa0a7773d448732364e7aee0, 5, 0, 0 35668435090eb47cf8c5e704243510b6cee35a7b, 42, 0, 0 e37d577b6cfc2d3e11252cef87ab9ebba72e1d52, 3, 0, 0 2b2bb6c734d106cdd1c0f4691607be2fe11d7ebb, 1, 0, 0 -7f80425b6a0af9bdfef12c8a873676e39e0a04a6, 399, 2, 7 +7f80425b6a0af9bdfef12c8a873676e39e0a04a6, 398, 2, 7 4fcd7d4d366d001cf5f1f7d926c608c902e3f0af, 2, 0, 0 573a1d115b86abbe3fb53ff930464d7d8fd95600, 18, 0, 0 ec52e77ecde749e7c5a483b26cbd8041f2a5a33c, 2, 0, 0