Skip to content

Commit

Permalink
Handle added/deleted block body in composite statements
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Nov 2, 2024
1 parent 15e54b0 commit 3fd666a
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 27 deletions.
27 changes: 22 additions & 5 deletions src/main/java/org/codetracker/BlockTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import org.codetracker.change.Change;
import org.codetracker.change.ChangeFactory;
import org.codetracker.change.Introduced;
import org.codetracker.change.block.BlockBodyAdded;
import org.codetracker.change.block.BlockBodyRemoved;
import org.codetracker.change.block.BlockSignatureFormatChange;
import org.codetracker.change.block.ElseBlockAdded;
import org.codetracker.change.block.ElseBodyBlockAdded;
import org.codetracker.change.block.ExpressionChange;
import org.codetracker.change.block.MergeBlock;
import org.codetracker.change.block.ReplaceConditionalWithTernary;
Expand Down Expand Up @@ -839,6 +842,20 @@ else if (ifBefore.getStatements().size() == 2 && ifAfter.getStatements().size()
blockChangeHistory.addChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.ELSE_BLOCK_REMOVED));
elseChange = true;
}
else if (ifBefore.getStatements().size() == 2 && ifAfter.getStatements().size() == 2) {
if(ifBefore.getStatements().get(1).getLocationInfo().getCodeElementType().equals(CodeElementType.BLOCK) &&
!ifAfter.getStatements().get(1).getLocationInfo().getCodeElementType().equals(CodeElementType.BLOCK) &&
!ifAfter.getStatements().get(1).getLocationInfo().getCodeElementType().equals(CodeElementType.IF_STATEMENT)) {
blockChangeHistory.addChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.ELSE_BODY_BLOCK_REMOVED));
elseChange = true;
}
else if(!ifBefore.getStatements().get(1).getLocationInfo().getCodeElementType().equals(CodeElementType.BLOCK) &&
!ifBefore.getStatements().get(1).getLocationInfo().getCodeElementType().equals(CodeElementType.IF_STATEMENT) &&
ifAfter.getStatements().get(1).getLocationInfo().getCodeElementType().equals(CodeElementType.BLOCK)) {
blockChangeHistory.addChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.ELSE_BODY_BLOCK_ADDED));
elseChange = true;
}
}
}
if (!bodyChange && !catchOrFinallyChange && !elseChange) {
if(blockBefore.differInFormatting(blockAfter)) {
Expand Down Expand Up @@ -891,10 +908,10 @@ private void handleCompositeExpressionChange(Block blockBefore, Block blockAfter
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));
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.BLOCK_BODY_REMOVED));
}
else if(!blockBefore.getComposite().getActualSignature().endsWith("{") && blockAfter.getComposite().getActualSignature().endsWith("{")) {
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.SIGNATURE_FORMAT_CHANGE));
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.BLOCK_BODY_ADDED));
}
if(blockBefore.differInFormatting(blockAfter)) {
addStatementChange(blockBefore, blockAfter, ChangeFactory.forBlock(Change.Type.SIGNATURE_FORMAT_CHANGE));
Expand Down Expand Up @@ -1229,12 +1246,12 @@ public HistoryInfo<Block> blameReturn(Block startBlock, int exactLineNumber) {
boolean multiLine = startBlock.isMultiLine();
for (Change change : historyInfo.getChangeList()) {
if (startBlock.isElseBlockStart() || startBlock.isElseBlockEnd()) {
if (change instanceof Introduced || change instanceof ElseBlockAdded) {
if (change instanceof ElseBodyBlockAdded || change instanceof Introduced || change instanceof ElseBlockAdded) {
return historyInfo;
}
}
else if (startBlock.isClosingCurlyBracket()) {
if (change instanceof Introduced || change instanceof ReplacePipelineWithLoop) {
if (change instanceof BlockBodyAdded || change instanceof Introduced || change instanceof ReplacePipelineWithLoop) {
return historyInfo;
}
}
Expand All @@ -1260,7 +1277,7 @@ else if (startBlock.isDoWhileConditional()) {
return historyInfo;
}
}
if (startBlock.getComposite() instanceof CompositeStatementObject && (change instanceof ExpressionChange || change instanceof BlockSignatureFormatChange)) {
if (startBlock.getComposite() instanceof CompositeStatementObject && (change instanceof BlockBodyAdded || change instanceof BlockBodyRemoved || change instanceof ExpressionChange || change instanceof BlockSignatureFormatChange)) {
if (multiLine) {
if (lineChangeMap.containsKey(pair)) {
if (lineChangeMap.get(pair).contains(exactLineNumber)) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/codetracker/change/Change.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ enum Type {
FINALLY_BLOCK_REMOVED("finally block removed"),
ELSE_BLOCK_ADDED("else block added"),
ELSE_BLOCK_REMOVED("else block removed"),
ELSE_BODY_BLOCK_ADDED("else body block added"),
ELSE_BODY_BLOCK_REMOVED("else body block removed"),
BLOCK_BODY_ADDED("block body added"),
BLOCK_BODY_REMOVED("block body removed"),
BLOCK_SPLIT("block split"),
BLOCK_MERGE("block merge"),
METHOD_SPLIT("method split"),
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/codetracker/change/ChangeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ public AbstractChange build() {
change = new FinallyBlockRemoved();
break;
}
case BLOCK_BODY_ADDED: {
change = new BlockBodyAdded();
break;
}
case BLOCK_BODY_REMOVED: {
change = new BlockBodyRemoved();
break;
}
case ELSE_BLOCK_ADDED: {
change = new ElseBlockAdded();
break;
Expand All @@ -173,6 +181,14 @@ public AbstractChange build() {
change = new ElseBlockRemoved();
break;
}
case ELSE_BODY_BLOCK_ADDED: {
change = new ElseBodyBlockAdded();
break;
}
case ELSE_BODY_BLOCK_REMOVED: {
change = new ElseBodyBlockRemoved();
break;
}
case INTERFACE_LIST_CHANGE: {
change = new ClassInterfaceListChange();
break;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/codetracker/change/block/BlockBodyAdded.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.codetracker.change.block;

import org.codetracker.change.Change;

public class BlockBodyAdded extends BlockChange {

public BlockBodyAdded() {
super(Change.Type.BLOCK_BODY_ADDED);
}

@Override
public String toString() {
return "Block Body Added";
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/codetracker/change/block/BlockBodyRemoved.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.codetracker.change.block;

import org.codetracker.change.Change;

public class BlockBodyRemoved extends BlockChange {

public BlockBodyRemoved() {
super(Change.Type.BLOCK_BODY_REMOVED);
}

@Override
public String toString() {
return "Block Body Removed";
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/codetracker/change/block/ElseBodyBlockAdded.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.codetracker.change.block;

import org.codetracker.change.Change;

public class ElseBodyBlockAdded extends BlockChange {
public ElseBodyBlockAdded() {
super(Change.Type.ELSE_BODY_BLOCK_ADDED);
}

@Override
public String toString() {
return "Else Body Block Added";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.codetracker.change.block;

import org.codetracker.change.Change;

public class ElseBodyBlockRemoved extends BlockChange {
public ElseBodyBlockRemoved() {
super(Change.Type.ELSE_BODY_BLOCK_REMOVED);
}

@Override
public String toString() {
return "Else Body Block Removed";
}
}
Loading

0 comments on commit 3fd666a

Please sign in to comment.