Skip to content

Commit

Permalink
Fix failing tests and blaming for do-while statements
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Nov 2, 2024
1 parent bd73fbe commit 15e54b0
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 42 deletions.
63 changes: 42 additions & 21 deletions src/main/java/org/codetracker/BlockTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ public boolean checkForExtractionOrInline(Version currentVersion, Version parent
List<String> stringRepresentationBefore = blockBefore.getComposite().stringRepresentation();
List<String> stringRepresentationAfter = matchedBlockInsideExtractedMethodBody.getComposite().stringRepresentation();
if (!stringRepresentationBefore.equals(stringRepresentationAfter)) {
if (!blockBefore.getComposite().getActualSignature().equals(matchedBlockInsideExtractedMethodBody.getComposite().getActualSignature())) {
//blockChangeHistory.addChange(blockBefore, matchedBlockInsideExtractedMethodBody, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
addStatementChange(blockBefore, matchedBlockInsideExtractedMethodBody, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
}
handleCompositeExpressionChange(blockBefore,
matchedBlockInsideExtractedMethodBody, stringRepresentationBefore,
stringRepresentationAfter);
List<String> stringRepresentationBodyBefore = stringRepresentationBefore.subList(1, stringRepresentationBefore.size());
List<String> stringRepresentationBodyAfter = stringRepresentationAfter.subList(1, stringRepresentationAfter.size());
if (!stringRepresentationBodyBefore.equals(stringRepresentationBodyAfter)) {
Expand Down Expand Up @@ -280,10 +279,9 @@ else if (mapping instanceof LeafMapping && mapping.getFragment1() instanceof Sta
List<String> stringRepresentationBefore = blockBefore.getComposite().stringRepresentation();
List<String> stringRepresentationAfter = matchedBlockInsideExtractedMethodBody.getComposite().stringRepresentation();
if (!stringRepresentationBefore.equals(stringRepresentationAfter)) {
if (!blockBefore.getComposite().getActualSignature().equals(matchedBlockInsideExtractedMethodBody.getComposite().getActualSignature())) {
//blockChangeHistory.addChange(blockBefore, matchedBlockInsideExtractedMethodBody, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
addStatementChange(blockBefore, matchedBlockInsideExtractedMethodBody, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
}
handleCompositeExpressionChange(blockBefore,
matchedBlockInsideExtractedMethodBody,
stringRepresentationBefore, stringRepresentationAfter);
List<String> stringRepresentationBodyBefore = stringRepresentationBefore.subList(1, stringRepresentationBefore.size());
List<String> stringRepresentationBodyAfter = stringRepresentationAfter.subList(1, stringRepresentationAfter.size());
if (!stringRepresentationBodyBefore.equals(stringRepresentationBodyAfter)) {
Expand Down Expand Up @@ -345,10 +343,9 @@ else if (mapping instanceof LeafMapping && mapping.getFragment1() instanceof Sta
List<String> stringRepresentationBefore = blockBefore.getComposite().stringRepresentation();
List<String> stringRepresentationAfter = matchedBlockInsideExtractedMethodBody.getComposite().stringRepresentation();
if (!stringRepresentationBefore.equals(stringRepresentationAfter)) {
if (!blockBefore.getComposite().getActualSignature().equals(matchedBlockInsideExtractedMethodBody.getComposite().getActualSignature())) {
//blockChangeHistory.addChange(blockBefore, matchedBlockInsideExtractedMethodBody, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
addStatementChange(blockBefore, matchedBlockInsideExtractedMethodBody, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
}
handleCompositeExpressionChange(blockBefore,
matchedBlockInsideExtractedMethodBody, stringRepresentationBefore,
stringRepresentationAfter);
List<String> stringRepresentationBodyBefore = stringRepresentationBefore.subList(1, stringRepresentationBefore.size());
List<String> stringRepresentationBodyAfter = stringRepresentationAfter.subList(1, stringRepresentationAfter.size());
if (!stringRepresentationBodyBefore.equals(stringRepresentationBodyAfter)) {
Expand Down Expand Up @@ -748,10 +745,8 @@ public boolean isMatched(UMLOperationBodyMapper umlOperationBodyMapper, Version
List<String> stringRepresentationBefore = blockBefore.getComposite().stringRepresentation();
List<String> stringRepresentationAfter = blockAfter.getComposite().stringRepresentation();
if (!stringRepresentationBefore.equals(stringRepresentationAfter)) {
if (!blockBefore.getComposite().getActualSignature().equals(blockAfter.getComposite().getActualSignature())) {
//blockChangeHistory.addChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
}
handleCompositeExpressionChange(blockBefore, blockAfter, stringRepresentationBefore,
stringRepresentationAfter);
List<String> stringRepresentationBodyBefore = stringRepresentationBefore.subList(1, stringRepresentationBefore.size());
List<String> stringRepresentationBodyAfter = stringRepresentationAfter.subList(1, stringRepresentationAfter.size());
if (!stringRepresentationBodyBefore.equals(stringRepresentationBodyAfter)) {
Expand Down Expand Up @@ -847,8 +842,7 @@ else if (ifBefore.getStatements().size() == 2 && ifAfter.getStatements().size()
}
if (!bodyChange && !catchOrFinallyChange && !elseChange) {
if(blockBefore.differInFormatting(blockAfter)) {
blockChangeHistory.addChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.SIGNATURE_FORMAT_CHANGE));
processChange(blockBefore, blockAfter);
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.SIGNATURE_FORMAT_CHANGE));
}
else {
blockChangeHistory.addChange(blockBefore, blockAfter, ChangeFactory.of(AbstractChange.Type.NO_CHANGE));
Expand All @@ -870,8 +864,7 @@ else if (mapping instanceof LeafMapping && mapping.getFragment1() instanceof Sta
}
else {
if(blockBefore.differInFormatting(blockAfter)) {
blockChangeHistory.addChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.SIGNATURE_FORMAT_CHANGE));
processChange(blockBefore, blockAfter);
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.SIGNATURE_FORMAT_CHANGE));
}
else {
blockChangeHistory.addChange(blockBefore, blockAfter, ChangeFactory.of(AbstractChange.Type.NO_CHANGE));
Expand All @@ -891,6 +884,29 @@ else if (mapping instanceof LeafMapping && mapping.getFragment1() instanceof Sta
return false;
}

private void handleCompositeExpressionChange(Block blockBefore, Block blockAfter,
List<String> stringRepresentationBefore, List<String> stringRepresentationAfter) {
if (!blockBefore.getComposite().getActualSignature().equals(blockAfter.getComposite().getActualSignature())) {
if (!stringRepresentationBefore.get(0).equals(stringRepresentationAfter.get(0))) {
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
}
if(blockBefore.getComposite().getActualSignature().endsWith("{") && !blockAfter.getComposite().getActualSignature().endsWith("{")) {
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.SIGNATURE_FORMAT_CHANGE));
}
else if(!blockBefore.getComposite().getActualSignature().endsWith("{") && blockAfter.getComposite().getActualSignature().endsWith("{")) {
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.SIGNATURE_FORMAT_CHANGE));
}
if(blockBefore.differInFormatting(blockAfter)) {
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.SIGNATURE_FORMAT_CHANGE));
}
}
else if (blockBefore.getComposite().getLocationInfo().getCodeElementType().equals(CodeElementType.DO_STATEMENT)) {
if (!stringRepresentationBefore.get(0).equals(stringRepresentationAfter.get(0))) {
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
}
}
}

private void addStatementChange(Block blockBefore, Block blockAfter, ChangeFactory changeType) {
blockChangeHistory.addChange(blockBefore, blockAfter, changeType);
processChange(blockBefore, blockAfter);
Expand Down Expand Up @@ -1222,6 +1238,11 @@ else if (startBlock.isClosingCurlyBracket()) {
return historyInfo;
}
}
else if (startBlock.isDoWhileConditional()) {
if (change instanceof ExpressionChange) {
return historyInfo;
}
}
else {
if (change instanceof Introduced || change instanceof MergeBlock || change instanceof SplitBlock ||
change instanceof ReplaceLoopWithPipeline || change instanceof ReplacePipelineWithLoop || change instanceof ReplaceConditionalWithTernary) {
Expand All @@ -1247,7 +1268,7 @@ else if (startBlock.isClosingCurlyBracket()) {
}
}
}
else {
else if (!startBlock.getComposite().getLocationInfo().getCodeElementType().equals(CodeElementType.DO_STATEMENT)) {
return historyInfo;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/codetracker/FileTrackerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ else if (startElement instanceof Block) {
startBlock.checkElseBlockStart(lineNumber);
startBlock.checkElseBlockEnd(lineNumber);
startBlock.checkClosingBracketOfAnonymousClassDeclaration(lineNumber);
startBlock.checkDoWhileConditional(lineNumber);
HistoryInfo<Block> historyInfo = startBlockChangeHistory.blameReturn(startBlock, lineNumber);
blameInfo.put(lineNumber, historyInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ else if (startElement instanceof Block) {
startBlock.checkElseBlockStart(lineNumber);
startBlock.checkElseBlockEnd(lineNumber);
startBlock.checkClosingBracketOfAnonymousClassDeclaration(lineNumber);
startBlock.checkDoWhileConditional(lineNumber);
HistoryInfo<Block> historyInfo = startBlockChangeHistory.blameReturn(startBlock, lineNumber);
blameInfo.put(lineNumber, historyInfo);
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/codetracker/element/BaseCodeElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public abstract class BaseCodeElement implements CodeElement {
private boolean closingCurlyBracket;
private boolean elseBlockStart;
private boolean elseBlockEnd;
private boolean doWhileConditional;

public BaseCodeElement(String identifierIgnoringVersion, String name, String filePath, Version version) {
this.identifier = version != null ? identifierIgnoringVersion + version : identifierIgnoringVersion;
Expand All @@ -29,7 +30,15 @@ public BaseCodeElement(String identifierIgnoringVersion, String name, String fil

public abstract BaseCodeElement of(Version anotherVersion);

public boolean isClosingCurlyBracket() {
public boolean isDoWhileConditional() {
return doWhileConditional;
}

public void setDoWhileConditional(boolean doWhileConditional) {
this.doWhileConditional = doWhileConditional;
}

public boolean isClosingCurlyBracket() {
return closingCurlyBracket;
}

Expand Down
18 changes: 11 additions & 7 deletions src/main/java/org/codetracker/element/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,10 @@ public boolean differInFormatting(Block other) {
String thisSignature = composite.getActualSignature();
String otherSignature = other.composite.getActualSignature();
if (thisSignature != null && otherSignature != null) {
int leftLines = this.getLocation().getEndLine() - this.getLocation().getStartLine();
int rightLines = other.getLocation().getEndLine() - other.getLocation().getStartLine();
if (leftLines == rightLines) {
String[] leftLineArray = thisSignature.split("\\r?\\n");
String[] rightLineArray = otherSignature.split("\\r?\\n");
if (leftLineArray.length == rightLineArray.length) {
//check if lines start and end with the same character
String[] leftLineArray = thisSignature.split("\\r?\\n");
String[] rightLineArray = otherSignature.split("\\r?\\n");
int linesWithDifferences = 0;
for (int i=0; i<leftLineArray.length; i++) {
String leftLine = leftLineArray[i];
Expand All @@ -206,7 +204,7 @@ public boolean differInFormatting(Block other) {
return !thisSignature.equals(otherSignature) && thisSignature.replaceAll("\\s+","").equals(otherSignature.replaceAll("\\s+",""));
}
}
return !thisSignature.equals(otherSignature) && leftLines != rightLines && thisSignature.replaceAll("\\s+","").equals(otherSignature.replaceAll("\\s+",""));
return !thisSignature.equals(otherSignature) && leftLineArray.length != rightLineArray.length && thisSignature.replaceAll("\\s+","").equals(otherSignature.replaceAll("\\s+",""));
}
return false;
}
Expand All @@ -228,6 +226,12 @@ public void checkClosingBracketOfAnonymousClassDeclaration(int lineNumber) {
}
}

public void checkDoWhileConditional(int lineNumber) {
if (getComposite() instanceof CompositeStatementObject && getLocation().getCodeElementType().equals(CodeElementType.DO_STATEMENT) && getLocation().getEndLine() == lineNumber) {
setDoWhileConditional(true);
}
}

public void checkClosingBracket(int lineNumber) {
if (getComposite() instanceof TryStatementObject) {
TryStatementObject tryStatement = (TryStatementObject)getComposite();
Expand Down Expand Up @@ -257,7 +261,7 @@ else if (tryStatement.getFinallyClause() != null) {
}
}
}
if (getLocation().getEndLine() == lineNumber && getComposite() instanceof CompositeStatementObject) {
if (getLocation().getEndLine() == lineNumber && getComposite() instanceof CompositeStatementObject && !getComposite().getLocationInfo().getCodeElementType().equals(CodeElementType.DO_STATEMENT)) {
setClosingCurlyBracket(true);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/codetracker/util/OracleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected static <T extends CodeElement> HashMap<String, ChangeHistory> processH
Edge edgeValue = historyImpl.getGraph().getEdgeValue(edge).get();
Set<Change> changeList = edgeValue.getChangeList();
for (Change change : changeList) {
if (Change.Type.NO_CHANGE.equals(change.getType()))
if (Change.Type.NO_CHANGE.equals(change.getType()) || Change.Type.SIGNATURE_FORMAT_CHANGE.equals(change.getType()))
continue;
ChangeHistory changeHistory = new ChangeHistory();

Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/blame/blameTestWithLocalRepo2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ba790bfa4 src/main/java/dat/Make.java (Pouryafard75 2024-03-07 01:33:48 +0
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-07 01:33:47 +0000 30) for (CaseInfo info : configuration.getAllCases()) {
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-07 01:33:47 +0000 31) case_count++;
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-07 01:33:47 +0000 32) ProjectASTDiff projectASTDiff = runWhatever(info.getRepo(), info.getCommit());
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-07 01:33:47 +0000 33) for (ASTDiff rm_astDiff : projectASTDiff.getDiffSet()) {
ba790bfa4 src/main/java/dat/Make.java (Pouryafard75 2024-03-07 01:33:48 +0000 33) for (ASTDiff rm_astDiff : projectASTDiff.getDiffSet()) {
ba790bfa4 src/main/java/dat/Make.java (Pouryafard75 2024-03-07 01:33:48 +0000 34) // logger.info("Working on " + info.makeURL() + " " + rm_astDiff.getSrcPath());
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-07 01:33:47 +0000 35) logger.info("Case " + case_count + "/" + configuration.getAllCases().size());
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-07 01:33:47 +0000 36) GridSearch dat = new GridSearch(info, projectASTDiff, rm_astDiff, configuration);
Expand Down
Loading

0 comments on commit 15e54b0

Please sign in to comment.