Checkstyle rule to prevent throwing the generic RuntimeException

This is a frequent cause of sonar breakage.

Change-Id: I54e0044447633a61bab560b020b57ed0a6875ebe
This commit is contained in:
Ray Milkey 2018-07-02 09:18:09 -07:00 committed by Thomas Vachuska
parent bcc53d3268
commit dbd3821cf0
11 changed files with 26 additions and 17 deletions

View File

@ -284,7 +284,7 @@ public class NetconfDeviceConfigSynchronizerProviderTest {
return new DefaultCompositeStream(id, xml); return new DefaultCompositeStream(id, xml);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new IllegalStateException(e);
} }
} }

View File

@ -804,7 +804,7 @@ public class EventuallyConsistentMapImplTest {
} else if (subject.equals(UPDATE_REQUEST_SUBJECT)) { } else if (subject.equals(UPDATE_REQUEST_SUBJECT)) {
requestHandler = (Consumer<Collection<UpdateRequest<String>>>) handler; requestHandler = (Consumer<Collection<UpdateRequest<String>>>) handler;
} else { } else {
throw new RuntimeException("Unexpected message subject " + subject.toString()); throw new IllegalStateException("Unexpected message subject " + subject.toString());
} }
} }
@ -814,7 +814,7 @@ public class EventuallyConsistentMapImplTest {
if (subject.equals(ANTI_ENTROPY_MESSAGE_SUBJECT)) { if (subject.equals(ANTI_ENTROPY_MESSAGE_SUBJECT)) {
antiEntropyHandler = (Function<AntiEntropyAdvertisement<String>, AntiEntropyResponse>) handler; antiEntropyHandler = (Function<AntiEntropyAdvertisement<String>, AntiEntropyResponse>) handler;
} else if (!subject.equals(INITIALIZE_MESSAGE_SUBJECT)) { } else if (!subject.equals(INITIALIZE_MESSAGE_SUBJECT)) {
throw new RuntimeException("Unexpected message subject " + subject.toString()); throw new IllegalStateException("Unexpected message subject " + subject.toString());
} }
} }
} }

View File

@ -445,7 +445,7 @@ public class AtomixConsistentSetMultimapTest extends AtomixTestBase<AtomixConsis
AtomixConsistentSetMultimap map = newPrimitive(mapName); AtomixConsistentSetMultimap map = newPrimitive(mapName);
return map; return map;
} catch (Throwable e) { } catch (Throwable e) {
throw new RuntimeException(e.toString()); throw new IllegalStateException(e.toString());
} }
} }

View File

@ -485,7 +485,7 @@ public class AtomixConsistentTreeMapTest extends AtomixTestBase<AtomixConsistent
AtomixConsistentTreeMap map = newPrimitive(mapName); AtomixConsistentTreeMap map = newPrimitive(mapName);
return map; return map;
} catch (Throwable e) { } catch (Throwable e) {
throw new RuntimeException(e.toString()); throw new IllegalStateException(e.toString());
} }
} }
private static class TestMapEventListener private static class TestMapEventListener

View File

@ -211,7 +211,7 @@ public abstract class AtomixTestBase<T extends AbstractRaftPrimitive> {
try { try {
latch.await(30000, TimeUnit.MILLISECONDS); latch.await(30000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new IllegalStateException(e);
} }
return servers; return servers;

View File

@ -94,7 +94,8 @@ final class SafeThriftClient {
} }
} }
throw new RuntimeException("Class needs to implement Iface directly. Use wrap(TServiceClient, Class) instead."); throw new IllegalStateException(
"Class needs to implement Iface directly. Use wrap(TServiceClient, Class) instead.");
} }
/** /**
@ -202,7 +203,7 @@ final class SafeThriftClient {
Thread.sleep(timeBetweenRetries); Thread.sleep(timeBetweenRetries);
} catch (InterruptedException e2) { } catch (InterruptedException e2) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new RuntimeException(e); throw new IllegalStateException(e);
} }
} }
} }

View File

@ -57,7 +57,7 @@ public final class Bmv2PreGroupTranslatorImpl {
*/ */
public static Bmv2PreGroup translate(Group group) { public static Bmv2PreGroup translate(Group group) {
if (!group.type().equals(GroupDescription.Type.ALL)) { if (!group.type().equals(GroupDescription.Type.ALL)) {
throw new RuntimeException("Unable to translate the group to BMv2 PRE group." + throw new IllegalStateException("Unable to translate the group to BMv2 PRE group." +
"A BMv2 PRE group is to be of ALL type. GroupId=" "A BMv2 PRE group is to be of ALL type. GroupId="
+ group.id()); + group.id());
} }
@ -231,11 +231,11 @@ public final class Bmv2PreGroupTranslatorImpl {
private static void checkOutputInstructions(GroupId groupId, private static void checkOutputInstructions(GroupId groupId,
Set<Instructions.OutputInstruction> outputInstructions) { Set<Instructions.OutputInstruction> outputInstructions) {
if (outputInstructions.isEmpty()) { if (outputInstructions.isEmpty()) {
throw new RuntimeException(String.format("Group bucket contains no output instruction. GroupId=%s", throw new IllegalStateException(String.format("Group bucket contains no output instruction. GroupId=%s",
groupId)); groupId));
} }
if (outputInstructions.size() != 1) { if (outputInstructions.size() != 1) {
throw new RuntimeException(String.format("Group bucket contains more than one output instructions. " + throw new IllegalStateException(String.format("Group bucket contains more than one output instructions. " +
"Only one is supported. GroupId=%s", groupId)); "Only one is supported. GroupId=%s", groupId));
} }
} }
@ -249,7 +249,7 @@ public final class Bmv2PreGroupTranslatorImpl {
*/ */
private static void validatePort(PortNumber portNumber) { private static void validatePort(PortNumber portNumber) {
if (portNumber.toLong() < 0 || portNumber.toLong() >= BMV2_PORT_MAP_SIZE) { if (portNumber.toLong() < 0 || portNumber.toLong() >= BMV2_PORT_MAP_SIZE) {
throw new RuntimeException(String.format("Port number %d is not a valid BMv2 physical port number." + throw new IllegalStateException(String.format("Port number %d is not a valid BMv2 physical port number." +
"Valid port range is [0,255]", portNumber)); "Valid port range is [0,255]", portNumber));
} }
} }

View File

@ -213,7 +213,7 @@ public class TerminalDeviceDiscovery
doc.appendChild(rpc); doc.appendChild(rpc);
return NetconfRpcParserUtil.toString(doc); return NetconfRpcParserUtil.toString(doc);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(new NetconfException("Exception in getDeviceDetailsBuilder", e)); throw new IllegalStateException(new NetconfException("Exception in getDeviceDetailsBuilder", e));
} }
*/ */
} }

View File

@ -172,7 +172,7 @@ final class P4RuntimeClientImpl implements P4RuntimeClient {
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.warn("Thread interrupted while waiting for lock (executing {})", log.warn("Thread interrupted while waiting for lock (executing {})",
opDescription); opDescription);
throw new RuntimeException(e); throw new IllegalStateException(e);
} }
try { try {
return supplier.get(); return supplier.get();

View File

@ -115,6 +115,14 @@
<property name="message" value="javafx classes are not supported by all JDKs."/> <property name="message" value="javafx classes are not supported by all JDKs."/>
</module> </module>
<!-- Don't allow usage of RuntimeException -->
<module name="RegexpSingleline">
<property name="format" value="throw[ ]*new[ ]*RuntimeException"/>
<property name="minimum" value="0"/>
<property name="maximum" value="0"/>
<property name="message" value="Don't throw generic exception types"/>
</module>
<!-- Checks for Headers --> <!-- Checks for Headers -->
<!-- See http://checkstyle.sf.net/config_header.html --> <!-- See http://checkstyle.sf.net/config_header.html -->
<!-- <module name="Header"> --> <!-- <module name="Header"> -->

View File

@ -295,7 +295,7 @@ public class ManuallyAdvancingTimer extends java.util.Timer {
currExecTime = TestUtils.getField(currTask, "nextExecutionTime"); currExecTime = TestUtils.getField(currTask, "nextExecutionTime");
} catch (TestUtils.TestUtilsException e) { } catch (TestUtils.TestUtilsException e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException("Could not get nextExecutionTime"); throw new IllegalStateException("Could not get nextExecutionTime");
} }
while (currExecTime <= timerKeeper.currentTimeInMillis()) { while (currExecTime <= timerKeeper.currentTimeInMillis()) {
if (executeTask(queue.pop())) { if (executeTask(queue.pop())) {
@ -309,7 +309,7 @@ public class ManuallyAdvancingTimer extends java.util.Timer {
currExecTime = TestUtils.getField(currTask, "nextExecutionTime"); currExecTime = TestUtils.getField(currTask, "nextExecutionTime");
} catch (TestUtils.TestUtilsException e) { } catch (TestUtils.TestUtilsException e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException("Could not get nextExecutionTime"); throw new IllegalStateException("Could not get nextExecutionTime");
} }
} }
return totalRun; return totalRun;
@ -461,7 +461,7 @@ public class ManuallyAdvancingTimer extends java.util.Timer {
executionTimeTwo = TestUtils.getField(o2, "nextExecutionTime"); executionTimeTwo = TestUtils.getField(o2, "nextExecutionTime");
} catch (TestUtils.TestUtilsException e) { } catch (TestUtils.TestUtilsException e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException("Could not get next execution time."); throw new IllegalStateException("Could not get next execution time.");
} }
if (executionTimeOne == executionTimeTwo) { if (executionTimeOne == executionTimeTwo) {
return 0; return 0;