diff --git a/src/main/java/gr/uom/java/xmi/decomposition/ReplacementAlgorithm.java b/src/main/java/gr/uom/java/xmi/decomposition/ReplacementAlgorithm.java index a06444b31..2ce10c631 100644 --- a/src/main/java/gr/uom/java/xmi/decomposition/ReplacementAlgorithm.java +++ b/src/main/java/gr/uom/java/xmi/decomposition/ReplacementAlgorithm.java @@ -547,7 +547,9 @@ else if(invocationCoveringTheEntireStatement1.arguments().contains(methodInvocat methodInvocations1.addAll(variablesAndMethodInvocations1); variablesAndMethodInvocations1.addAll(methodInvocations1); variablesAndMethodInvocations1.addAll(variables1); - + //perform creation replacements + findReplacements(creations1, creations2, replacementInfo, ReplacementType.CLASS_INSTANCE_CREATION, container1, container2, classDiff); + findReplacements(variables1, creations2, replacementInfo, ReplacementType.VARIABLE_REPLACED_WITH_CLASS_INSTANCE_CREATION, container1, container2, classDiff); findReplacements(creations1, variables2, replacementInfo, ReplacementType.VARIABLE_REPLACED_WITH_CLASS_INSTANCE_CREATION, container1, container2, classDiff); if(statement1.getString().startsWith(JAVA.THROW_SPACE) && statement2.getString().startsWith(JAVA.THROW_SPACE) && creationCoveringTheEntireStatement2 != null && creations2.isEmpty()) { @@ -730,9 +732,6 @@ else if(methodInvocations1.contains(s1) && variables2.contains(s2)) { findReplacements(variables1, variables2, replacementInfo, ReplacementType.VARIABLE_NAME, container1, container2, classDiff); } } - //perform creation replacements - findReplacements(creations1, creations2, replacementInfo, ReplacementType.CLASS_INSTANCE_CREATION, container1, container2, classDiff); - findReplacements(parenthesizedExpressions1, parenthesizedExpressions2, replacementInfo, ReplacementType.PARENTHESIZED_EXPRESSION, container1, container2, classDiff); //perform literal replacements @@ -3854,6 +3853,30 @@ else if(!old.getBefore().equals(element1) && old.getAfter().equals(element2)) { } } } + //remove subsumed replacements + if(replacements.values().contains(true) && replacements.values().contains(false)) { + Replacement trueReplacement = null; + for(Replacement key : replacements.keySet()) { + if(replacements.get(key) == true) { + trueReplacement = key; + break; + } + } + Set toBeRemoved = new LinkedHashSet(); + for(Replacement key : replacements.keySet()) { + if(replacements.get(key) == false) { + if(key.getBefore().equals(trueReplacement.getBefore()) && trueReplacement.getAfter().contains(key.getAfter())) { + toBeRemoved.add(key); + } + else if(key.getAfter().equals(trueReplacement.getAfter()) && trueReplacement.getBefore().contains(key.getBefore())) { + toBeRemoved.add(key); + } + } + } + for(Replacement r : toBeRemoved) { + replacements.remove(r); + } + } return replacements; } @@ -3919,6 +3942,30 @@ else if(substringBeforeIndex1.length() < substringBeforeIndex2.length()) { } } } + //remove subsumed replacements + if(replacements.values().contains(true) && replacements.values().contains(false)) { + Replacement trueReplacement = null; + for(Replacement key : replacements.keySet()) { + if(replacements.get(key) == true) { + trueReplacement = key; + break; + } + } + Set toBeRemoved = new LinkedHashSet(); + for(Replacement key : replacements.keySet()) { + if(replacements.get(key) == false) { + if(key.getBefore().equals(trueReplacement.getBefore()) && trueReplacement.getAfter().contains(key.getAfter())) { + toBeRemoved.add(key); + } + else if(key.getAfter().equals(trueReplacement.getAfter()) && trueReplacement.getBefore().contains(key.getBefore())) { + toBeRemoved.add(key); + } + } + } + for(Replacement r : toBeRemoved) { + replacements.remove(r); + } + } return replacements; } diff --git a/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java b/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java index e2672d4d8..8afc0e4d3 100644 --- a/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java +++ b/src/main/java/gr/uom/java/xmi/decomposition/UMLOperationBodyMapper.java @@ -6733,6 +6733,27 @@ else if(isIfBranch(grandParent, grandParent.getParent()) && !hasElseBranch(grand if(identicalCount == 0) elseCondition = true; } + else { + List ifParents = extractIfParentsFromBlocks(parentMap.keySet()); + Set grandParents = new LinkedHashSet(); + for(CompositeStatementObject ifParent : ifParents) { + CompositeStatementObject parent = ifParent.getParent(); + while(parent != null && parent.getLocationInfo().getCodeElementType().equals(CodeElementType.BLOCK)) { + parent = parent.getParent(); + } + grandParents.add(parent); + } + if(container1.getBody() != null && grandParents.size() == 1) { + CompositeStatementObject grandParent2 = grandParents.iterator().next(); + List innerNodes1 = container1.getBody().getCompositeStatement().getInnerNodes(); + for(CompositeStatementObject innerNode1 : innerNodes1) { + if(innerNode1.getString().equals(grandParent2.getString())) { + elseCondition = true; + break; + } + } + } + } } if(ifFound && (elseIfFound || (elseFound && elseCondition)) && (ifElseIfChain(parentMap.keySet()) || passDueToIfBecomingElseIf)) { Set variableDeclarationNames1 = new LinkedHashSet<>(); diff --git a/src/test/java/org/refactoringminer/test/TestAllRefactorings.java b/src/test/java/org/refactoringminer/test/TestAllRefactorings.java index 04ab514ee..8632e5ce1 100644 --- a/src/test/java/org/refactoringminer/test/TestAllRefactorings.java +++ b/src/test/java/org/refactoringminer/test/TestAllRefactorings.java @@ -15,6 +15,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(12045, 20, 241); + test.assertExpectationsWithGitHubAPI(12046, 20, 240); } } diff --git a/src/test/resources/oracle/data.json b/src/test/resources/oracle/data.json index c490407fa..27af465ae 100644 --- a/src/test/resources/oracle/data.json +++ b/src/test/resources/oracle/data.json @@ -27480,7 +27480,7 @@ "description": "Move Method public addItem(item Item, count int) : void from class tconstruct.library.modifiers.Modifier to public addItem(item Item, amountNeeded int, amountMatched int) : void from class tconstruct.library.mantle.RecipeMatchRegistry", "comment": "

For me, it is an 'extract class' refactoring. Class RecipeMatchRegistry is extracted from Modifier.

See that RecipeMatchRegistry is an attribute of class  Modifier in the refactored version, and the  3 addItem(..) methods in the refactored version of Modifier are now delegating to the 3 addItem(..) in RecipeMatchRegistry.

 The method is moved with the addition of a new parameter (amountNeeded), which is not used.

- public void addItem(Item item, int count) {
- modifierItems.add(new RecipeMatch.Item(new ItemStack(item), count));
- }

public void addItem(Item item, int amountNeeded, int amountMatched) {
items.add(new RecipeMatch.Item(new ItemStack(item), 1, amountMatched));
}

", "validation": "TP", - "detectionTools": "RefDiff, RD-1x", + "detectionTools": "RefactoringMiner, RefDiff, RD-1x", "validators": "Matin, Laleh, Davood, Nikos" }, { "type": "Change Attribute Type", diff --git a/src/test/resources/oracle/expected.txt b/src/test/resources/oracle/expected.txt index eed407cee..51a035eea 100644 --- a/src/test/resources/oracle/expected.txt +++ b/src/test/resources/oracle/expected.txt @@ -488,7 +488,7 @@ a9379ee529ed87e28c0736c3c6657dcd6a0680e4, 5, 0, 0 687bda5a3ea1b5daae2764653843d318c77f4590, 6, 0, 0 3ffd70929c08be5cf14f156189e8050969caa87e, 1, 0, 0 da39bfeb9c370abe2d86e6e327fade252434090d, 13, 0, 0 -71820e573134be3fad3935035249cd77c4412f4e, 8, 0, 3 +71820e573134be3fad3935035249cd77c4412f4e, 9, 0, 2 f05e86c4d31987ff2f30330745c3eb605de4c4dc, 4, 0, 0 19d1936c3b07d97d88646aeae30de747715e3248, 21, 0, 0 b40adc25008b6f608ee3eb3422c8884fff987337, 16, 0, 0