Skip to content

Commit

Permalink
Tolerate Generic with Diamond replacements in PurityChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Dec 5, 2024
1 parent ee16e25 commit bc0c67e
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/main/java/org/refactoringminer/api/PurityChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static PurityCheckResult detectSplitMethodPurity(SplitOperationRefactori

UMLOperationBodyMapper bodyMapper = findTheMapper(refactoring, mapping);
HashSet<Replacement> 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);
}
Expand Down Expand Up @@ -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<Replacement> replacementsToCheck, UMLOperationBodyMapper bodyMapper) {
private static ReplacementJustificationResult checkReplacementForSplitMethod(SplitOperationRefactoring refactoring, HashSet<Replacement> replacementsToCheck, List<Refactoring> refactorings, UMLOperationBodyMapper bodyMapper) {
ReplacementJustificationResult replacementJustificationResult = new ReplacementJustificationResult();

omitThisPatternReplacements(replacementsToCheck);
omitDiamondReplacements(replacementsToCheck, refactorings);
omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, bodyMapper);
omitBooleanVariableDeclarationReplacement(replacementsToCheck, bodyMapper); // For the runTests commit
omitEqualStringLiteralsReplacement(replacementsToCheck);
Expand Down Expand Up @@ -189,7 +190,7 @@ private static PurityCheckResult detectPullUpMethodPurity(PullUpOperationRefacto
int sizeToCheckBefore = replacementsToCheck.size();

omitThisPatternReplacements(replacementsToCheck);

omitDiamondReplacements(replacementsToCheck, refactorings);


omitPrintAndLogMessagesRelatedReplacements(replacementsToCheck, refactoring.getBodyMapper());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -4229,11 +4238,40 @@ private static void omitThisPatternReplacements(HashSet<Replacement> 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<Replacement> replacementsToCheck, List<Refactoring> refactorings) {
Set<Replacement> 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<String, String> stringStringEntry : refactoring.getParameterToArgumentMap().entrySet()) {
for (Map.Entry<String, String> stringEntry : refactoring1.getParameterToArgumentMap().entrySet()) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bc0c67e

Please sign in to comment.