Skip to content

Commit

Permalink
Fix bug in Move Code implementation for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Oct 22, 2024
1 parent 2f393a6 commit d3bf42a
Show file tree
Hide file tree
Showing 5 changed files with 489 additions and 355 deletions.
139 changes: 122 additions & 17 deletions src/main/java/org/codetracker/BlockTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,23 +370,16 @@ else if (mapping instanceof LeafMapping && mapping.getFragment1() instanceof Sta
}
}
}
Block blockBefore = Block.of(rightBlock.getComposite(), rightBlock.getOperation(), parentVersion);
if (matchedBlockFromSourceMethod == null) {
blockChangeHistory.handleAdd(blockBefore, rightBlock, moveCodeRefactoring.toString());
if(extractMatches == 0) {
elements.add(blockBefore);
}
}
else {
if (matchedBlockFromSourceMethod != null) {
VariableDeclarationContainer sourceOperation = moveCodeRefactoring.getSourceContainer();
Method sourceMethod = Method.of(sourceOperation, parentVersion);
Block leftBlock = Block.of(matchedBlockFromSourceMethod instanceof StatementObject ? (StatementObject) matchedBlockFromSourceMethod : (CompositeStatementObject) matchedBlockFromSourceMethod, sourceMethod);
if(extractMatches == 0) {
elements.add(leftBlock);
}
blockChangeHistory.connectRelatedNodes();
extractMatches++;
}
blockChangeHistory.connectRelatedNodes();
extractMatches++;
}
break;
}
Expand Down Expand Up @@ -996,13 +989,20 @@ public void processChange(Block blockBefore, Block blockAfter) {
Block startBlock = getStart();
if (startBlock != null && startBlock.isMultiLine()) {
List<String> start = IOUtils.readLines(new StringReader(((StatementObject)startBlock.getComposite()).getActualSignature()));
List<String> startNoWhitespace = new ArrayList<String>();
for(String s : start) {
startNoWhitespace.add(s.replaceAll("\\s+", ""));
}
List<String> original = IOUtils.readLines(new StringReader(((StatementObject)blockBefore.getComposite()).getActualSignature()));
List<String> revised = IOUtils.readLines(new StringReader(((StatementObject)blockAfter.getComposite()).getActualSignature()));

Patch<String> patch = DiffUtils.diff(original, revised);
List<AbstractDelta<String>> deltas = patch.getDeltas();
for (int i=0; i<deltas.size(); i++) {
AbstractDelta<String> delta = deltas.get(i);
if (indentationChange(delta)) {
continue;
}
Chunk<String> target = delta.getTarget();
List<String> affectedLines = new ArrayList<>(target.getLines());
boolean subListFound = false;
Expand All @@ -1026,9 +1026,14 @@ public void processChange(Block blockBefore, Block blockAfter) {
if (!subListFound) {
for (String line : affectedLines) {
List<Integer> matchingIndices = findAllMatchingIndices(start, line);
if (matchingIndices.isEmpty()) {
matchingIndices = findAllMatchingIndices(startNoWhitespace, line.replaceAll("\\s+", ""));
}
for (Integer index : matchingIndices) {
if (original.size() > index && revised.size() > index &&
original.get(index).equals(line) && revised.get(index).equals(line)) {
if (original.size() > index && revised.size() > index && equalOrStripEqual(original, revised, line, index)) {
continue;
}
if(equalOrStripEqual(original, revised, line)) {
continue;
}
int actualLine = startBlock.signatureStartLine() + index;
Expand All @@ -1040,7 +1045,28 @@ public void processChange(Block blockBefore, Block blockAfter) {
list.add(actualLine);
lineChangeMap.put(pair, list);
}
break;
//break;
}
if (matchingIndices.isEmpty() && !line.isBlank()) {
matchingIndices = findAllMatchingIndicesRelaxed(start, line);
for (Integer index : matchingIndices) {
if (original.size() > index && revised.size() > index && equalOrStripEqual(original, revised, line, index)) {
continue;
}
if(equalOrStripEqual(original, revised, line)) {
continue;
}
int actualLine = startBlock.signatureStartLine() + index;
if (lineChangeMap.containsKey(pair)) {
lineChangeMap.get(pair).add(actualLine);
}
else {
List<Integer> list = new ArrayList<>();
list.add(actualLine);
lineChangeMap.put(pair, list);
}
break;
}
}
}
}
Expand All @@ -1052,11 +1078,90 @@ public void processChange(Block blockBefore, Block blockAfter) {
}
}

private List<Integer> findAllMatchingIndices(List<String> startCommentLines, String line) {
private boolean equalOrStripEqual(List<String> original, List<String> revised, String line) {
List<Integer> originalMatchingIndices = new ArrayList<>();
for(int i=0; i<original.size(); i++) {
String s = original.get(i);
if(s.strip().equals(line.strip())) {
originalMatchingIndices.add(i);
}
}
List<Integer> revisedMatchingIndices = new ArrayList<>();
for(int i=0; i<revised.size(); i++) {
String s = revised.get(i);
if(s.strip().equals(line.strip())) {
revisedMatchingIndices.add(i);
}
}
if(revisedMatchingIndices.equals(originalMatchingIndices) && originalMatchingIndices.size() > 0) {
return true;
}
if(revisedMatchingIndices.size() == originalMatchingIndices.size() && originalMatchingIndices.size() > 0) {
int matches = 0;
for(int i=0; i<originalMatchingIndices.size(); i++) {
int originalIndex = originalMatchingIndices.get(i);
int revisedIndex = revisedMatchingIndices.get(i);
if(originalIndex == revisedIndex) {
matches++;
}
else if(revisedIndex - originalIndex == revised.size() - original.size()) {
matches++;
}
}
return matches == originalMatchingIndices.size();
}
return false;
}

private boolean equalOrStripEqual(List<String> original, List<String> revised, String line, Integer index) {
String originalAtIndex = original.get(index);
String revisedAtIndex = revised.get(index);
return equalOrStripEqual(originalAtIndex, revisedAtIndex, line);
}

private boolean equalOrStripEqual(String originalAtIndex, String revisedAtIndex, String line) {
if(originalAtIndex.equals(line) && revisedAtIndex.equals(line))
return true;
String originalStripped = originalAtIndex.strip();
String revisedStripped = revisedAtIndex.strip();
if(originalStripped.equals(revisedStripped) && originalStripped.equals(line.strip()))
return true;
return false;
}

private boolean indentationChange(AbstractDelta<String> delta) {
Chunk<String> source = delta.getSource();
Chunk<String> target = delta.getTarget();
if (source.getLines().size() == target.getLines().size() && source.getLines().size() > 0) {
List<String> sourceStrippedLines = new ArrayList<String>();
List<String> targetStrippedLines = new ArrayList<String>();
for(int i=0; i<source.getLines().size(); i++) {
sourceStrippedLines.add(source.getLines().get(i).strip());
targetStrippedLines.add(target.getLines().get(i).strip());
}
if (sourceStrippedLines.equals(targetStrippedLines)) {
return true;
}
}
return false;
}

private List<Integer> findAllMatchingIndices(List<String> lines, String line) {
List<Integer> matchingIndices = new ArrayList<>();
for(int i=0; i<lines.size(); i++) {
String element = lines.get(i);
if(element.strip().equals(line.strip())) {
matchingIndices.add(i);
}
}
return matchingIndices;
}

private List<Integer> findAllMatchingIndicesRelaxed(List<String> lines, String line) {
List<Integer> matchingIndices = new ArrayList<>();
for(int i=0; i<startCommentLines.size(); i++) {
String element = startCommentLines.get(i).trim();
if(line.equals(element) || element.contains(line.trim())) {
for(int i=0; i<lines.size(); i++) {
String element = lines.get(i);
if(element.strip().contains(line.strip())) {
matchingIndices.add(i);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,41 @@
"parentCommitId": "d6272b70eb5ad61fd8ab34091e970c50b1f67386",
"commitId": "1a2c318e22a0b2b22ccc76019217c0892fe2d59b",
"commitTime": 1430849532,
"changeType": "body change",
"elementFileBefore": "src/main/java/com/puppycrawl/tools/checkstyle/Main.java",
"elementNameBefore": "src/main/java/com.puppycrawl.tools.checkstyle.Main#processCommandLine(CommandLine)$if(116-120)",
"elementFileAfter": "src/main/java/com/puppycrawl/tools/checkstyle/Main.java",
"elementNameAfter": "src/main/java/com.puppycrawl.tools.checkstyle.Main#main(String[])$if(75-97)"
},
{
"parentCommitId": "d6272b70eb5ad61fd8ab34091e970c50b1f67386",
"commitId": "1a2c318e22a0b2b22ccc76019217c0892fe2d59b",
"commitTime": 1430849532,
"changeType": "expression change",
"elementFileBefore": "src/main/java/com/puppycrawl/tools/checkstyle/Main.java",
"elementNameBefore": "src/main/java/com.puppycrawl.tools.checkstyle.Main#processCommandLine(CommandLine)$if(116-120)",
"elementFileAfter": "src/main/java/com/puppycrawl/tools/checkstyle/Main.java",
"elementNameAfter": "src/main/java/com.puppycrawl.tools.checkstyle.Main#main(String[])$if(75-97)"
},
{
"parentCommitId": "e380ac8df92c051d843d380d5e8a6fe90af9df9d",
"commitId": "ab2f93f9bf61816d84154e636d32c81c05854e24",
"commitTime": 1429852725,
"changeType": "body change",
"elementFileBefore": "src/main/java/com/puppycrawl/tools/checkstyle/Main.java",
"elementNameBefore": "src/main/java/com.puppycrawl.tools.checkstyle.Main#main(String[])$if(85-89)",
"elementFileAfter": "src/main/java/com/puppycrawl/tools/checkstyle/Main.java",
"elementNameAfter": "src/main/java/com.puppycrawl.tools.checkstyle.Main#processCommandLine(CommandLine)$if(110-114)"
},
{
"parentCommitId": "cf38b070cc22197040e77ad6de6af8363ae83841",
"commitId": "f85edb712767e01dafb8bf4a4a07d0d0ed3e9a38",
"commitTime": 1416599535,
"changeType": "introduced",
"elementFileBefore": "src/main/java/com/puppycrawl/tools/checkstyle/Main.java",
"elementNameBefore": "src/main/java/com.puppycrawl.tools.checkstyle.Main#main(String[])$if(75-97)",
"elementNameBefore": "src/main/java/com.puppycrawl.tools.checkstyle.Main#main(String[])$if(87-90)",
"elementFileAfter": "src/main/java/com/puppycrawl/tools/checkstyle/Main.java",
"elementNameAfter": "src/main/java/com.puppycrawl.tools.checkstyle.Main#main(String[])$if(75-97)",
"comment": "new block"
"elementNameAfter": "src/main/java/com.puppycrawl.tools.checkstyle.Main#main(String[])$if(87-90)"
}
]
}
2 changes: 1 addition & 1 deletion src/test/resources/blame/blameTestWithLocalRepo7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ b2ba6b951 junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/Cl
a97e4d308 junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java (Sam Brannen 2017-07-04 11:00:00 +0000 431) private void invokeMethodInExtensionContext(Method method, ExtensionContext context, ExtensionRegistry registry) {
bef03b5ce junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java (Sam Brannen 2017-07-10 16:34:34 +0000 432) Object testInstance = context.getRequiredTestInstance();
323dac885 junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java (Sam Brannen 2017-06-30 16:16:42 +0000 433) testInstance = ReflectionUtils.getOutermostInstance(testInstance, method.getDeclaringClass()).orElseThrow(
fdadf9054 junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java (Sam Brannen 2017-06-30 16:16:42 +0000 434) () -> new JUnitException("Failed to find instance for method: " + method.toGenericString()));
b2ba6b951 junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/ClassTestDescriptor.java (Sam Brannen 2016-05-02 15:30:42 +0000 434) () -> new JUnitException("Failed to find instance for method: " + method.toGenericString()));
435)
fdadf9054 junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java (Sam Brannen 2017-06-30 16:16:42 +0000 436) executableInvoker.invoke(method, testInstance, context, registry);
8db7e41d0 junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/ClassTestDescriptor.java (Johannes Link 2015-12-17 12:48:18 +0000 437) }
Expand Down
Loading

0 comments on commit d3bf42a

Please sign in to comment.