diff --git a/src/main/java/org/refactoringminer/api/PurityChecker.java b/src/main/java/org/refactoringminer/api/PurityChecker.java index c191a0d19..b624f8665 100644 --- a/src/main/java/org/refactoringminer/api/PurityChecker.java +++ b/src/main/java/org/refactoringminer/api/PurityChecker.java @@ -90,7 +90,7 @@ private static PurityCheckResult detectSplitMethodPurity(SplitOperationRefactori UMLOperationBodyMapper bodyMapper = findTheMapper(refactoring, mapping); HashSet replacementsToCheck = new HashSet<>(mapping.getReplacements()); - replacementJustificationResult = checkReplacementForSplitMethod(refactoring, replacementsToCheck, bodyMapper); + replacementJustificationResult = checkReplacementForSplitMethod(refactoring, replacementsToCheck, refactorings, bodyMapper); if (!replacementJustificationResult.isJustificationState()) { return new PurityCheckResult(false, "Replacements cannot be justified", "Severe Changes", 2); } @@ -147,10 +147,11 @@ private static PurityCheckResult detectSplitMethodPurity(SplitOperationRefactori return new PurityCheckResult(false, "Replacements cannot be justified", "Severe Changes", 2); } - private static ReplacementJustificationResult checkReplacementForSplitMethod(SplitOperationRefactoring refactoring, HashSet replacementsToCheck, UMLOperationBodyMapper bodyMapper) { + private static ReplacementJustificationResult checkReplacementForSplitMethod(SplitOperationRefactoring refactoring, HashSet replacementsToCheck, List refactorings, UMLOperationBodyMapper bodyMapper) { ReplacementJustificationResult replacementJustificationResult = new ReplacementJustificationResult(); omitThisPatternReplacements(replacementsToCheck); + omitDiamondReplacements(replacementsToCheck, refactorings); omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, bodyMapper); omitBooleanVariableDeclarationReplacement(replacementsToCheck, bodyMapper); // For the runTests commit omitEqualStringLiteralsReplacement(replacementsToCheck); @@ -189,7 +190,7 @@ private static PurityCheckResult detectPullUpMethodPurity(PullUpOperationRefacto int sizeToCheckBefore = replacementsToCheck.size(); omitThisPatternReplacements(replacementsToCheck); - + omitDiamondReplacements(replacementsToCheck, refactorings); omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, refactoring.getBodyMapper()); @@ -625,6 +626,7 @@ private static CheckReplacementResult checkReplacementsPullUpMethod(PullUpOperat int sizeBefore = replacementsToCheck.size(); omitThisPatternReplacements(replacementsToCheck); + omitDiamondReplacements(replacementsToCheck, refactorings); omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, refactoring.getBodyMapper()); omitBooleanVariableDeclarationReplacement(replacementsToCheck, refactoring.getBodyMapper()); // For the runTests commit omitEqualStringLiteralsReplacement(replacementsToCheck); @@ -814,6 +816,7 @@ private static PurityCheckResult detectPushDownMethodPurity(PushDownOperationRef int sizeToCheckBefore = replacementsToCheck.size(); omitThisPatternReplacements(replacementsToCheck); + omitDiamondReplacements(replacementsToCheck, refactorings); if (replacementsToCheck.isEmpty()) { return new PurityCheckResult(true, "this pattern-has been either added or deleted", "Tolerable changes in the body", mappingState); @@ -1126,6 +1129,7 @@ private static CheckReplacementResult checkReplacementsPushDownMethod(PushDownOp int sizeBefore = replacementsToCheck.size(); omitThisPatternReplacements(replacementsToCheck); + omitDiamondReplacements(replacementsToCheck, refactorings); omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, refactoring.getBodyMapper()); omitBooleanVariableDeclarationReplacement(replacementsToCheck, refactoring.getBodyMapper()); // For the runTests commit omitEqualStringLiteralsReplacement(replacementsToCheck); @@ -1327,6 +1331,7 @@ private static PurityCheckResult detectInlineMethodPurity(InlineOperationRefacto purityComment += "Changes are within the Inline Method refactoring mechanics" + "\n"; omitThisPatternReplacements(replacementsToCheck); + omitDiamondReplacements(replacementsToCheck, refactorings); omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, refactoring.getBodyMapper()); omitBooleanVariableDeclarationReplacement(replacementsToCheck, refactoring.getBodyMapper()); omitEqualStringLiteralsReplacement(replacementsToCheck); @@ -1977,6 +1982,7 @@ private static CheckReplacementResult checkReplacementsInlineMethod(InlineOperat purityComment += "Changes are within the Inline Method refactoring mechanics" + "\n"; omitThisPatternReplacements(replacementsToCheck); + omitDiamondReplacements(replacementsToCheck, refactorings); omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, refactoring.getBodyMapper()); omitBooleanVariableDeclarationReplacement(replacementsToCheck, refactoring.getBodyMapper()); omitEqualStringLiteralsReplacement(replacementsToCheck); @@ -2161,6 +2167,7 @@ private static PurityCheckResult detectMoveMethodPurity(MoveOperationRefactoring int sizeToCheckBefore = replacementsToCheck.size(); omitThisPatternReplacements(replacementsToCheck); + omitDiamondReplacements(replacementsToCheck, refactorings); omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, refactoring.getBodyMapper()); omitBooleanVariableDeclarationReplacement(replacementsToCheck, refactoring.getBodyMapper()); // For the runTests commit omitEqualStringLiteralsReplacement(replacementsToCheck); @@ -3043,6 +3050,7 @@ private static CheckReplacementResult checkReplacementsMoveMethod(MoveOperationR int sizeToCheckBefore = replacementsToCheck.size(); omitThisPatternReplacements(replacementsToCheck); + omitDiamondReplacements(replacementsToCheck, refactorings); omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, refactoring.getBodyMapper()); omitBooleanVariableDeclarationReplacement(replacementsToCheck, refactoring.getBodyMapper()); // For the runTests commit omitEqualStringLiteralsReplacement(replacementsToCheck); @@ -3395,6 +3403,7 @@ private static PurityCheckResult detectExtractOperationPurity(ExtractOperationRe } omitThisPatternReplacements(replacementsToCheck); + omitDiamondReplacements(replacementsToCheck, refactorings); omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, refactoring.getBodyMapper()); omitBooleanVariableDeclarationReplacement(replacementsToCheck, refactoring.getBodyMapper()); // For the runTests commit omitEqualStringLiteralsReplacement(replacementsToCheck); @@ -4229,11 +4238,40 @@ private static void omitThisPatternReplacements(HashSet replacement //System.out.println("ignored"); } } + else if (replacement.getBefore().contains("this.") && !replacement.getAfter().contains("this.")) { + String before = replacement.getBefore().replace("this.", ""); + if (before.equals(replacement.getAfter())) { + replacementsToRemove.add(replacement); + } + } + else if (!replacement.getBefore().contains("this.") && replacement.getAfter().contains("this.")) { + String after = replacement.getAfter().replace("this.", ""); + if (after.equals(replacement.getBefore())) { + replacementsToRemove.add(replacement); + } + } } } replacementsToCheck.removeAll(replacementsToRemove); } + private static void omitDiamondReplacements(HashSet replacementsToCheck, List refactorings) { + Set replacementsToRemove = new HashSet<>(); + + for (Replacement replacement : replacementsToCheck) { + for(Refactoring r : refactorings) { + if(r instanceof ReplaceGenericWithDiamondRefactoring) { + ReplaceGenericWithDiamondRefactoring ref = (ReplaceGenericWithDiamondRefactoring)r; + if(replacement.getBefore().equals(ref.getTypeBefore()) && replacement.getAfter().equals(ref.getTypeAfter())) { + replacementsToRemove.add(replacement); + break; + } + } + } + } + replacementsToCheck.removeAll(replacementsToRemove); + } + private static void adjustTheParameterArgumentField(ExtractOperationRefactoring refactoring, ExtractOperationRefactoring refactoring1) { for (Map.Entry stringStringEntry : refactoring.getParameterToArgumentMap().entrySet()) { for (Map.Entry stringEntry : refactoring1.getParameterToArgumentMap().entrySet()) { @@ -5098,6 +5136,7 @@ private static CheckReplacementResult checkReplacements(ExtractOperationRefactor purityComment += "Changes are within the Extract Method refactoring mechanics \n"; omitThisPatternReplacements(replacementsToCheck); + omitDiamondReplacements(replacementsToCheck, refactorings); checkForParameterArgumentPair(refactoring, replacementsToCheck); omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, refactoring.getBodyMapper()); omitBooleanVariableDeclarationReplacement(replacementsToCheck, refactoring.getBodyMapper()); // For the runTests commit