Skip to content

Commit

Permalink
Handle duplicated merged methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Sep 29, 2024
1 parent dd38ce9 commit 57c5197
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
58 changes: 58 additions & 0 deletions src/main/java/org/codetracker/BlockTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Set;
import java.util.function.Predicate;

import org.apache.commons.lang3.tuple.Pair;
import org.codetracker.api.History;
import org.codetracker.api.History.HistoryInfo;
import org.codetracker.api.Version;
Expand Down Expand Up @@ -126,6 +127,63 @@ public boolean checkClassDiffForBlockChange(Version currentVersion, Version pare
return false;
}

public boolean isMergeMultiMapping(Version currentVersion, Version parentVersion, Predicate<Method> equalMethod, Block rightBlock, List<Refactoring> refactorings) {
Set<Pair<Block, Block>> mappings = new LinkedHashSet<>();
AbstractCodeFragment fragment2 = null;
int fragment2Matches = 0;
MergeOperationRefactoring mergeOperationRefactoring = null;
for (Refactoring refactoring : refactorings) {
switch (refactoring.getRefactoringType()) {
case MERGE_OPERATION: {
mergeOperationRefactoring = (MergeOperationRefactoring) refactoring;
Method methodAfter = Method.of(mergeOperationRefactoring.getNewMethodAfterMerge(), currentVersion);
if (equalMethod.test(methodAfter)) {
for (UMLOperationBodyMapper bodyMapper : mergeOperationRefactoring.getMappers()) {
for (AbstractCodeMapping mapping : bodyMapper.getMappings()) {
if (mapping instanceof CompositeStatementObjectMapping) {
Block matchedBlockInsideMergedMethodBody = Block.of((CompositeStatementObject) mapping.getFragment2(), bodyMapper.getContainer2(), currentVersion);
if (matchedBlockInsideMergedMethodBody.equalIdentifierIgnoringVersion(rightBlock)) {
Block blockBefore = Block.of((CompositeStatementObject) mapping.getFragment1(), bodyMapper.getContainer1(), parentVersion);
mappings.add(Pair.of(blockBefore, matchedBlockInsideMergedMethodBody));
if (fragment2 == null) {
fragment2 = mapping.getFragment2();
}
else if (fragment2.equals(mapping.getFragment2())) {
fragment2Matches++;
}
}
}
else if (mapping instanceof LeafMapping && mapping.getFragment1() instanceof StatementObject && mapping.getFragment2() instanceof StatementObject) {
Block matchedBlockInsideMergedMethodBody = Block.of((StatementObject) mapping.getFragment2(), bodyMapper.getContainer2(), currentVersion);
if (matchedBlockInsideMergedMethodBody.equalIdentifierIgnoringVersion(rightBlock)) {
Block blockBefore = Block.of((StatementObject) mapping.getFragment1(), bodyMapper.getContainer1(), parentVersion);
mappings.add(Pair.of(blockBefore, matchedBlockInsideMergedMethodBody));
if (fragment2 == null) {
fragment2 = mapping.getFragment2();
}
else if (fragment2.equals(mapping.getFragment2())) {
fragment2Matches++;
}
}
}
}
}
}
break;
}
}
}
if (mappings.size() > 1 && mappings.size() == fragment2Matches + 1) {
for (Pair<Block, Block> pair : mappings) {
blockChangeHistory.handleAdd(pair.getLeft(), pair.getRight(), mergeOperationRefactoring.toString());
elements.add(pair.getLeft());
}
blockChangeHistory.connectRelatedNodes();
return true;
}
return false;
}

public boolean checkForExtractionOrInline(Version currentVersion, Version parentVersion, Predicate<Method> equalMethod, Block rightBlock, List<Refactoring> refactorings) {
int extractMatches = 0;
for (Refactoring refactoring : refactorings) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/codetracker/FileTrackerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,11 @@ private void processLocallyRefactoredMethods(Map<Method, MethodTrackerChangeHist
}
startBlockChangeHistory.poll();
alreadyProcessed.add(startBlock);
boolean found = startBlockChangeHistory.checkForExtractionOrInline(currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, rightBlock, refactorings);
boolean found = startBlockChangeHistory.isMergeMultiMapping(currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, rightBlock, refactorings);
if (found) {
continue;
}
found = startBlockChangeHistory.checkForExtractionOrInline(currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, rightBlock, refactorings);
if (found) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,11 @@ private void processLocallyRefactoredMethods(Map<Method, MethodTrackerChangeHist
}
startBlockChangeHistory.poll();
alreadyProcessed.add(startBlock);
boolean found = startBlockChangeHistory.checkForExtractionOrInline(currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, rightBlock, refactorings);
boolean found = startBlockChangeHistory.isMergeMultiMapping(currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, rightBlock, refactorings);
if (found) {
continue;
}
found = startBlockChangeHistory.checkForExtractionOrInline(currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, rightBlock, refactorings);
if (found) {
continue;
}
Expand Down
16 changes: 8 additions & 8 deletions src/test/resources/blame/blameTestWithLocalRepo13.txt
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ bf69cf167 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocal
bf69cf167 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:32 +0000 317) * @return true if the scope of a node is restricted to a specific code block.
bf69cf167 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:32 +0000 318) */
bf69cf167 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:32 +0000 319) private static boolean isInSpecificCodeBlock(DetailAST node, int blockType) {
320) boolean returnValue = false;
321) for (DetailAST token = node.getParent(); token != null; token = token.getParent()) {
322) final int type = token.getType();
0a1a4c6e9 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:31 +0000 320) boolean returnValue = false;
0a1a4c6e9 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:31 +0000 321) for (DetailAST token = node.getParent(); token != null; token = token.getParent()) {
0a1a4c6e9 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:31 +0000 322) final int type = token.getType();
0a1a4c6e9 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:31 +0000 323) if (type == blockType) {
324) returnValue = true;
325) break;
326) }
327) }
328) return returnValue;
0a1a4c6e9 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:31 +0000 324) returnValue = true;
0a1a4c6e9 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:31 +0000 325) break;
0a1a4c6e9 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:31 +0000 326) }
0a1a4c6e9 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:31 +0000 327) }
0a1a4c6e9 src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:31 +0000 328) return returnValue;
55aa50d8f src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Andrei Selkin 2016-03-29 22:47:31 +0000 329) }
330)
0c717ef2a src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java (Rick Giles 2003-09-11 12:01:47 +0000 331) /**
Expand Down

0 comments on commit 57c5197

Please sign in to comment.