Skip to content

Commit ef8feb7

Browse files
kaufcoleveretka
authored andcommitted
SONARJAVA-4406 also considers throw statements
1 parent b1109b5 commit ef8feb7

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

java-checks-test-sources/src/main/java/checks/InterruptedExceptionCheck.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,29 @@ void bar() throws InterruptedException {
363363
}
364364
}
365365

366+
public void throwExceptionInTryDirectly() throws InterruptedException {
367+
try {
368+
throw new RuntimeException();
369+
} catch (Exception e) { // Compliant
370+
}
371+
372+
try {
373+
throw new InterruptedException();
374+
} catch (Exception e) { // Noncompliant
375+
}
376+
377+
try {
378+
InterruptedException ie = new InterruptedException();
379+
throw ie;
380+
} catch (Exception e) { // Noncompliant
381+
}
382+
383+
try {
384+
throw getInterruptedException();
385+
} catch (Exception e) { // Noncompliant
386+
}
387+
}
388+
366389
void falsePositivesSonarjava4406() {
367390
try {
368391
try {
@@ -381,6 +404,15 @@ void falsePositivesSonarjava4406() {
381404
} catch (Exception e) { // Compliant, because inner try does not throw an InterruptedException
382405
}
383406

407+
try {
408+
try {
409+
throwsInterruptedException();
410+
} catch (InterruptedException ie) { // Compliant
411+
throw new InterruptedException();
412+
}
413+
} catch (Exception e) { // Noncompliant, because inner try throws an InterruptedException
414+
}
415+
384416
try {
385417
try {
386418
throwsInterruptedException();

java-checks/src/main/java/org/sonar/java/checks/InterruptedExceptionCheck.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ public void visitTryStatement(TryStatementTree tryStatementTree) {
179179
scan(tryStatementTree.catches());
180180
scan(tryStatementTree.finallyBlock());
181181
}
182+
183+
@Override
184+
public void visitThrowStatement(ThrowStatementTree tree) {
185+
// besides to method invocation, we also need to collect throw statements
186+
if (isInterruptingExceptionExpression(tree.expression())) {
187+
invocationTree.add(tree);
188+
}
189+
super.visitThrowStatement(tree);
190+
}
182191
}
183192

184193
private static class BlockVisitor extends BaseTreeVisitor {

0 commit comments

Comments
 (0)