Skip to content

Commit

Permalink
Fix handling of comments between try and catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Nov 6, 2024
1 parent 452954c commit 2c5e5dc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
43 changes: 41 additions & 2 deletions src/main/java/org/codetracker/util/AbstractCodeElementLocator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.codetracker.util;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

Expand Down Expand Up @@ -236,17 +237,55 @@ else if (block.getComposite() instanceof CompositeStatementObject) {
}

protected boolean endLineBlockPredicate(Block block) {
List<UMLComment> comments = block.getOperation().getComments();
if (block.getComposite() instanceof TryStatementObject) {
TryStatementObject tryStatement = (TryStatementObject)block.getComposite();
List<AbstractStatement> statements = tryStatement.getStatements();
AbstractStatement lastStatement = null;
if (statements.size() > 0) {
lastStatement = statements.get(statements.size()-1);
}
if (tryStatement.getCatchClauses().size() > 0) {
CompositeStatementObject catchClause = tryStatement.getCatchClauses().get(0);
if (catchClause.getLocationInfo().getStartLine() == lineNumber || catchClause.getLocationInfo().getStartLine() == lineNumber + 1) {
List<UMLComment> commentsBetweenTryCatch = new ArrayList<UMLComment>();
if(lastStatement != null) {
for (UMLComment comment : comments) {
if (comment.getLocationInfo().getStartLine() > lastStatement.getLocationInfo().getEndLine() &&
comment.getLocationInfo().getEndLine() < catchClause.getLocationInfo().getStartLine()) {
commentsBetweenTryCatch.add(comment);
}
}
}
boolean commentOnLineNumber = false;
for (UMLComment comment : commentsBetweenTryCatch) {
if (comment.getLocationInfo().getStartLine() >= lineNumber && comment.getLocationInfo().getEndLine() <= lineNumber) {
commentOnLineNumber = true;
break;
}
}
if (!commentOnLineNumber && (catchClause.getLocationInfo().getStartLine() == lineNumber || catchClause.getLocationInfo().getStartLine() == lineNumber + 1)) {
return block.getComposite().getLocationInfo().getStartLine() <= lineNumber;
}
}
else if (tryStatement.getFinallyClause() != null) {
CompositeStatementObject finnalyClause = tryStatement.getFinallyClause();
if (finnalyClause.getLocationInfo().getStartLine() == lineNumber || finnalyClause.getLocationInfo().getStartLine() == lineNumber + 1) {
List<UMLComment> commentsBetweenTryFinally = new ArrayList<UMLComment>();
if(lastStatement != null) {
for (UMLComment comment : comments) {
if (comment.getLocationInfo().getStartLine() > lastStatement.getLocationInfo().getEndLine() &&
comment.getLocationInfo().getEndLine() < finnalyClause.getLocationInfo().getStartLine()) {
commentsBetweenTryFinally.add(comment);
}
}
}
boolean commentOnLineNumber = false;
for (UMLComment comment : commentsBetweenTryFinally) {
if (comment.getLocationInfo().getStartLine() >= lineNumber && comment.getLocationInfo().getEndLine() <= lineNumber) {
commentOnLineNumber = true;
break;
}
}
if (!commentOnLineNumber && (finnalyClause.getLocationInfo().getStartLine() == lineNumber || finnalyClause.getLocationInfo().getStartLine() == lineNumber + 1)) {
return block.getComposite().getLocationInfo().getStartLine() <= lineNumber;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/blame/blameTestWithLocalRepo3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ d46c2cf0e src/main/java/com/puppycrawl/tools/checkstyle/Checker.java (Andrei Se
13b7c6343 src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java (Oliver Burn 2008-11-09 09:37:27 +0000 292) fireFileFinished(fileName);
d46c2cf0e src/main/java/com/puppycrawl/tools/checkstyle/Checker.java (Andrei Selkin 2016-02-09 17:50:02 +0000 293) }
25a37e504 src/main/java/com/puppycrawl/tools/checkstyle/Checker.java (Andrei Selkin 2016-09-14 20:58:34 +0000 294) // -@cs[IllegalCatch] There is no other way to deliver filename that was under
f020066f8 src/main/java/com/puppycrawl/tools/checkstyle/Checker.java (Roman Ivanov 2015-11-01 13:52:32 +0000 295) // processing. See https://github.com/checkstyle/checkstyle/issues/2285
25a37e504 src/main/java/com/puppycrawl/tools/checkstyle/Checker.java (Andrei Selkin 2016-09-14 20:58:34 +0000 295) // processing. See https://github.com/checkstyle/checkstyle/issues/2285
f020066f8 src/main/java/com/puppycrawl/tools/checkstyle/Checker.java (Roman Ivanov 2015-11-01 13:52:32 +0000 296) catch (Exception ex) {
77acd41cf src/main/java/com/puppycrawl/tools/checkstyle/Checker.java (Andrei Selkin 2016-02-16 19:22:01 +0000 297) // We need to catch all exceptions to put a reason failure (file name) in exception
f020066f8 src/main/java/com/puppycrawl/tools/checkstyle/Checker.java (Roman Ivanov 2015-11-01 13:52:32 +0000 298) throw new CheckstyleException("Exception was thrown while processing "
Expand Down

0 comments on commit 2c5e5dc

Please sign in to comment.