Checkstyle rule to prevent throwing the generic RuntimeException
This is a frequent cause of sonar breakage.
Change-Id: I54e0044447633a61bab560b020b57ed0a6875ebe
diff --git a/apps/configsync-netconf/src/test/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerProviderTest.java b/apps/configsync-netconf/src/test/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerProviderTest.java
index 8851cf1..28fad43 100644
--- a/apps/configsync-netconf/src/test/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerProviderTest.java
+++ b/apps/configsync-netconf/src/test/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerProviderTest.java
@@ -284,7 +284,7 @@
return new DefaultCompositeStream(id, xml);
} catch (IOException e) {
- throw new RuntimeException(e);
+ throw new IllegalStateException(e);
}
}
diff --git a/core/store/primitives/src/test/java/org/onosproject/store/primitives/impl/EventuallyConsistentMapImplTest.java b/core/store/primitives/src/test/java/org/onosproject/store/primitives/impl/EventuallyConsistentMapImplTest.java
index f16c688..0997261 100644
--- a/core/store/primitives/src/test/java/org/onosproject/store/primitives/impl/EventuallyConsistentMapImplTest.java
+++ b/core/store/primitives/src/test/java/org/onosproject/store/primitives/impl/EventuallyConsistentMapImplTest.java
@@ -804,7 +804,7 @@
} else if (subject.equals(UPDATE_REQUEST_SUBJECT)) {
requestHandler = (Consumer<Collection<UpdateRequest<String>>>) handler;
} else {
- throw new RuntimeException("Unexpected message subject " + subject.toString());
+ throw new IllegalStateException("Unexpected message subject " + subject.toString());
}
}
@@ -814,7 +814,7 @@
if (subject.equals(ANTI_ENTROPY_MESSAGE_SUBJECT)) {
antiEntropyHandler = (Function<AntiEntropyAdvertisement<String>, AntiEntropyResponse>) handler;
} else if (!subject.equals(INITIALIZE_MESSAGE_SUBJECT)) {
- throw new RuntimeException("Unexpected message subject " + subject.toString());
+ throw new IllegalStateException("Unexpected message subject " + subject.toString());
}
}
}
diff --git a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentSetMultimapTest.java b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentSetMultimapTest.java
index 01f5d00..6c2912e 100644
--- a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentSetMultimapTest.java
+++ b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentSetMultimapTest.java
@@ -445,7 +445,7 @@
AtomixConsistentSetMultimap map = newPrimitive(mapName);
return map;
} catch (Throwable e) {
- throw new RuntimeException(e.toString());
+ throw new IllegalStateException(e.toString());
}
}
diff --git a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentTreeMapTest.java b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentTreeMapTest.java
index 9af2602..941009c 100644
--- a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentTreeMapTest.java
+++ b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentTreeMapTest.java
@@ -485,7 +485,7 @@
AtomixConsistentTreeMap map = newPrimitive(mapName);
return map;
} catch (Throwable e) {
- throw new RuntimeException(e.toString());
+ throw new IllegalStateException(e.toString());
}
}
private static class TestMapEventListener
diff --git a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixTestBase.java b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixTestBase.java
index 4098af1..6f3ddea 100644
--- a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixTestBase.java
+++ b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixTestBase.java
@@ -211,7 +211,7 @@
try {
latch.await(30000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
- throw new RuntimeException(e);
+ throw new IllegalStateException(e);
}
return servers;
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/SafeThriftClient.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/SafeThriftClient.java
index a510954..c1b83da 100644
--- a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/SafeThriftClient.java
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/SafeThriftClient.java
@@ -94,7 +94,8 @@
}
}
- 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 @@
Thread.sleep(timeBetweenRetries);
} catch (InterruptedException e2) {
Thread.currentThread().interrupt();
- throw new RuntimeException(e);
+ throw new IllegalStateException(e);
}
}
}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/impl/Bmv2PreGroupTranslatorImpl.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/impl/Bmv2PreGroupTranslatorImpl.java
index 6652ee0..369b7c5 100644
--- a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/impl/Bmv2PreGroupTranslatorImpl.java
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/impl/Bmv2PreGroupTranslatorImpl.java
@@ -57,7 +57,7 @@
*/
public static Bmv2PreGroup translate(Group group) {
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="
+ group.id());
}
@@ -231,11 +231,11 @@
private static void checkOutputInstructions(GroupId groupId,
Set<Instructions.OutputInstruction> outputInstructions) {
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));
}
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));
}
}
@@ -249,7 +249,7 @@
*/
private static void validatePort(PortNumber portNumber) {
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));
}
}
diff --git a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/TerminalDeviceDiscovery.java b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/TerminalDeviceDiscovery.java
index f7c4fbf..26bfbbb 100644
--- a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/TerminalDeviceDiscovery.java
+++ b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/TerminalDeviceDiscovery.java
@@ -213,7 +213,7 @@
doc.appendChild(rpc);
return NetconfRpcParserUtil.toString(doc);
} catch (Exception e) {
- throw new RuntimeException(new NetconfException("Exception in getDeviceDetailsBuilder", e));
+ throw new IllegalStateException(new NetconfException("Exception in getDeviceDetailsBuilder", e));
}
*/
}
diff --git a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java
index c952e61..bd688f7 100644
--- a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java
+++ b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java
@@ -172,7 +172,7 @@
} catch (InterruptedException e) {
log.warn("Thread interrupted while waiting for lock (executing {})",
opDescription);
- throw new RuntimeException(e);
+ throw new IllegalStateException(e);
}
try {
return supplier.get();
diff --git a/tools/build/conf/src/main/resources/onos/checkstyle.xml b/tools/build/conf/src/main/resources/onos/checkstyle.xml
index 21ac8da..6833486 100644
--- a/tools/build/conf/src/main/resources/onos/checkstyle.xml
+++ b/tools/build/conf/src/main/resources/onos/checkstyle.xml
@@ -115,6 +115,14 @@
<property name="message" value="javafx classes are not supported by all JDKs."/>
</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 -->
<!-- See http://checkstyle.sf.net/config_header.html -->
<!-- <module name="Header"> -->
diff --git a/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimer.java b/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimer.java
index 1e965de..85eb35f 100644
--- a/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimer.java
+++ b/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimer.java
@@ -295,7 +295,7 @@
currExecTime = TestUtils.getField(currTask, "nextExecutionTime");
} catch (TestUtils.TestUtilsException e) {
e.printStackTrace();
- throw new RuntimeException("Could not get nextExecutionTime");
+ throw new IllegalStateException("Could not get nextExecutionTime");
}
while (currExecTime <= timerKeeper.currentTimeInMillis()) {
if (executeTask(queue.pop())) {
@@ -309,7 +309,7 @@
currExecTime = TestUtils.getField(currTask, "nextExecutionTime");
} catch (TestUtils.TestUtilsException e) {
e.printStackTrace();
- throw new RuntimeException("Could not get nextExecutionTime");
+ throw new IllegalStateException("Could not get nextExecutionTime");
}
}
return totalRun;
@@ -461,7 +461,7 @@
executionTimeTwo = TestUtils.getField(o2, "nextExecutionTime");
} catch (TestUtils.TestUtilsException e) {
e.printStackTrace();
- throw new RuntimeException("Could not get next execution time.");
+ throw new IllegalStateException("Could not get next execution time.");
}
if (executionTimeOne == executionTimeTwo) {
return 0;