@@ -25,7 +25,7 @@ public void run1 () {
25
25
}catch (java .io .IOException e ) {
26
26
LOGGER .log (Level .WARN , "Interrupted!" , e );
27
27
}catch (InterruptedException e ) { // Noncompliant [[sc=13;ec=35]] {{Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here.}}
28
- LOGGER .log (Level .WARN , "Interrupted!" , e );
28
+ LOGGER .log (Level .WARN , "Interrupted!" , e );
29
29
}
30
30
}
31
31
@@ -57,10 +57,10 @@ public void runInterrupted() {
57
57
try {
58
58
throw new InterruptedException ();
59
59
} catch (InterruptedException e ) {
60
- LOGGER .log (Level .WARN , "Interrupted!" , e );
61
- // clean up state...
62
- Thread .currentThread ().interrupt ();
63
- }
60
+ LOGGER .log (Level .WARN , "Interrupted!" , e );
61
+ // clean up state...
62
+ Thread .currentThread ().interrupt ();
63
+ }
64
64
try {
65
65
while (true ) {
66
66
throw new InterruptedException ();
@@ -71,7 +71,7 @@ public void runInterrupted() {
71
71
// clean up state...
72
72
new Interruptable ().interrupt ();
73
73
}
74
- }
74
+ }
75
75
76
76
public Object getNextTask (BlockingQueue <Object > queue ) {
77
77
boolean interrupted = false ;
@@ -96,14 +96,14 @@ class Interruptable {
96
96
static final Log LOGGER = null ;
97
97
98
98
void interrupt () {
99
-
99
+
100
100
}
101
101
102
102
private static void waitForNextExecution (Set <Runnable > running , LongSupplier waitTimeoutMillis ) {
103
103
try {
104
104
Thread .sleep (waitTimeoutMillis .getAsLong ());
105
105
} catch (InterruptedException e ) { //Compliant
106
- cancelAllSubTasksAndInterrupt (running );
106
+ cancelAllSubTasksAndInterrupt (running );
107
107
}
108
108
}
109
109
@@ -113,28 +113,28 @@ private static void cancelAllSubTasksAndInterrupt(Set<Runnable> subTasks) {
113
113
}
114
114
Thread .currentThread ().interrupt ();
115
115
}
116
-
116
+
117
117
private static void waitForNextExecution1 (Set <Runnable > running , LongSupplier waitTimeoutMillis ) {
118
118
try {
119
119
Thread .sleep (waitTimeoutMillis .getAsLong ());
120
120
} catch (InterruptedException e ) { // Noncompliant, too many levels
121
- cancelAllSubTasksAndInterrupt1 (running );
121
+ cancelAllSubTasksAndInterrupt1 (running );
122
122
}
123
123
}
124
124
125
125
private static void cancelAllSubTasksAndInterrupt1 (Set <Runnable > subTasks ) {
126
126
cancelAllSubTasksAndInterrupt2 (subTasks );
127
127
}
128
128
129
- private static void cancelAllSubTasksAndInterrupt2 (Set <Runnable > subTasks ) {
129
+ private static void cancelAllSubTasksAndInterrupt2 (Set <Runnable > subTasks ) {
130
130
cancelAllSubTasksAndInterrupt3 (subTasks );
131
131
}
132
132
133
133
private static void cancelAllSubTasksAndInterrupt3 (Set <Runnable > subTasks ) {
134
134
cancelAllSubTasksAndInterrupt4 (subTasks );
135
135
if (LOGGER != null )throw new RuntimeException ();
136
136
}
137
-
137
+
138
138
private static void cancelAllSubTasksAndInterrupt4 (Set <Runnable > subTasks ) {
139
139
for (Runnable task : subTasks ) {
140
140
System .out .println ("--- waitForNextExecution: Service interrupted. Cancel execution of task {}." );
@@ -275,7 +275,6 @@ public void throwNewInterruptedExceptionFromFunction() throws InterruptedExcepti
275
275
}
276
276
277
277
public void throwNewCustomizedInterruptedExceptionFromFunction () throws InterruptedException {
278
-
279
278
try {
280
279
throwsInterruptedException ();
281
280
} catch (InterruptedException e ) { // Compliant
@@ -302,7 +301,6 @@ public void throwNewCustomizedInterruptedExceptionFromFunction() throws Interrup
302
301
}
303
302
304
303
public void rethrowSameException () throws InterruptedException {
305
-
306
304
try {
307
305
throwsInterruptedException ();
308
306
} catch (InterruptedException e ) { // Compliant
@@ -340,7 +338,6 @@ public void rethrowSameException() throws InterruptedException {
340
338
}
341
339
342
340
public void cutControlFlow () throws InterruptedException {
343
-
344
341
try {
345
342
throwsInterruptedException ();
346
343
} catch (InterruptedException e ) { // Noncompliant, because neither foo nor bar belong to the control flow of this catch block.
@@ -359,11 +356,74 @@ void bar() throws InterruptedException {
359
356
try {
360
357
throwsInterruptedException ();
361
358
} catch (InterruptedException e ) { // Noncompliant, because neither foo, nor bar belong to the control flow of this catch block.
362
- Runnable foo = () -> Thread .currentThread ().interrupt ();
363
- Action bar = () -> {
364
- throw new InterruptedException ();
365
- };
359
+ Runnable foo = () -> Thread .currentThread ().interrupt ();
360
+ Action bar = () -> {
361
+ throw new InterruptedException ();
362
+ };
363
+ }
364
+ }
365
+
366
+ void falsePositivesSonarjava4406 () {
367
+ try {
368
+ try {
369
+ throwsInterruptedException ();
370
+ } catch (InterruptedException ie ) { // Noncompliant, because interruption state is lost.
371
+ }
372
+ } catch (Exception e ) { // Compliant, because inner try does not throw an InterruptedException
373
+ }
374
+
375
+ try {
376
+ try {
377
+ throwsInterruptedException ();
378
+ } catch (InterruptedException ie ) { // Compliant
379
+ Thread .currentThread ().interrupt ();
380
+ }
381
+ } catch (Exception e ) { // Compliant, because inner try does not throw an InterruptedException
366
382
}
383
+
384
+ try {
385
+ try {
386
+ throwsInterruptedException ();
387
+ } catch (InterruptedException ie ) { // Compliant
388
+ throw new InterruptedException ();
389
+ }
390
+ } catch (InterruptedException e ) { // Noncompliant, because explicitly catch InterruptedException
391
+ }
392
+
393
+ try {
394
+ try {
395
+ throwsInterruptedException ();
396
+ } catch (RuntimeException ie ) { // Compliant
397
+ }
398
+ } catch (Exception e ) { // Noncompliant, because inner try may throw an InterruptedException
399
+ }
400
+
401
+ }
402
+
403
+ public void throwInterruptedExceptionFromResourceList () throws InterruptedException {
404
+ try {
405
+ try (AutoCloseable autocloseable = getAutoCloseableThrowsInterruptedException ()) {
406
+ doSomething ();
407
+ } catch (IOException ie ) {
408
+ doSomething ();
409
+ }
410
+ } catch (Exception e ) { // Noncompliant
411
+ }
412
+ }
413
+
414
+ public void throwInterruptedExceptionWithinThrowStatement () throws InterruptedException {
415
+ try {
416
+ throw getRuntimeExceptionThrowsInterruptedException ();
417
+ } catch (Exception e ) { // Noncompliant
418
+ }
419
+ }
420
+
421
+ private AutoCloseable getAutoCloseableThrowsInterruptedException () throws InterruptedException {
422
+ throw getInterruptedException ();
423
+ }
424
+
425
+ private RuntimeException getRuntimeExceptionThrowsInterruptedException () throws InterruptedException {
426
+ throw getInterruptedException ();
367
427
}
368
428
369
429
@ FunctionalInterface
@@ -414,15 +474,11 @@ private static void interruptByThrowingCustomizedInterruptedExceptionL4() throws
414
474
throw new CustomizedInterruptedException ();
415
475
}
416
476
417
- public void throwsException () throws Exception {
477
+ public static void throwsException () throws Exception {
418
478
throw new Exception ();
419
479
}
420
480
421
- public void throwsInterruptedException () throws InterruptedException {
422
- throw new InterruptedException ();
423
- }
424
-
425
- public void throwsCustomizedInterruptedException () throws InterruptedException {
481
+ public static void throwsInterruptedException () throws InterruptedException {
426
482
throw new InterruptedException ();
427
483
}
428
484
0 commit comments