Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package detectors.preserve_thread_interruption_status_rule;

import java.lang.InterruptedException;
import java.lang.RuntimeException;
import java.lang.Thread;

public class PreserveThreadInterruptionStatusRule {

// {fact rule=preserve-thread-interruption-status-rule@v1.0 defects=1}
public void preserveThreadInterruptionStatusRuleNoncompliant(int numTimes) throws RuntimeException {
try {
for (int i=0; i < numTimes; i++ ) {
Thread.sleep(1000L);
}
// Noncompliant: InterruptedException wrapped and rethrown using RuntimeException but without resetting interrupt status.
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// {/fact}

// {fact rule=preserve-thread-interruption-status-rule@v1.0 defects=0}
public void preserveThreadInterruptionStatusRuleCompliant(int numTimes) throws RuntimeException {
try {
for (int i=0; i < numTimes; i++ ) {
Thread.sleep(1000L);
}
} catch (InterruptedException e) {
// Compliant: InterruptedException wrapped and rethrown using RuntimeException and resetting interrupt status.
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
// {/fact}
}