From dbd3821cf02c20a65cae5ab850780cf0bbc0b6f0 Mon Sep 17 00:00:00 2001 From: Ray Milkey Date: Mon, 2 Jul 2018 09:18:09 -0700 Subject: [PATCH] Checkstyle rule to prevent throwing the generic RuntimeException This is a frequent cause of sonar breakage. Change-Id: I54e0044447633a61bab560b020b57ed0a6875ebe --- .../NetconfDeviceConfigSynchronizerProviderTest.java | 2 +- .../primitives/impl/EventuallyConsistentMapImplTest.java | 4 ++-- .../resources/impl/AtomixConsistentSetMultimapTest.java | 2 +- .../resources/impl/AtomixConsistentTreeMapTest.java | 2 +- .../store/primitives/resources/impl/AtomixTestBase.java | 2 +- .../onosproject/drivers/bmv2/ctl/SafeThriftClient.java | 5 +++-- .../drivers/bmv2/impl/Bmv2PreGroupTranslatorImpl.java | 8 ++++---- .../drivers/odtn/openconfig/TerminalDeviceDiscovery.java | 2 +- .../onosproject/p4runtime/ctl/P4RuntimeClientImpl.java | 2 +- tools/build/conf/src/main/resources/onos/checkstyle.xml | 8 ++++++++ .../test/java/org/onlab/util/ManuallyAdvancingTimer.java | 6 +++--- 11 files changed, 26 insertions(+), 17 deletions(-) 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 8851cf15d4..28fad43f2c 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 @@ public class NetconfDeviceConfigSynchronizerProviderTest { 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 f16c688790..0997261cbc 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 @@ public class EventuallyConsistentMapImplTest { } else if (subject.equals(UPDATE_REQUEST_SUBJECT)) { requestHandler = (Consumer>>) handler; } 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)) { antiEntropyHandler = (Function, 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 01f5d00c43..6c2912eca5 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 @@ public class AtomixConsistentSetMultimapTest extends AtomixTestBase { 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 a510954e94..c1b83da484 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 @@ 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); } 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 6652ee0142..369b7c5619 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 final class Bmv2PreGroupTranslatorImpl { */ 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 @@ public final class Bmv2PreGroupTranslatorImpl { private static void checkOutputInstructions(GroupId groupId, Set 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 @@ public final class Bmv2PreGroupTranslatorImpl { */ 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 f7c4fbf7d8..26bfbbb78e 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 @@ public class TerminalDeviceDiscovery 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 c952e613e0..bd688f7f2a 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 @@ final class P4RuntimeClientImpl implements P4RuntimeClient { } 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 21ac8daa5a..68334864ee 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 @@ + + + + + + + + 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 1e965de6d6..85eb35f13d 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 @@ public class ManuallyAdvancingTimer extends java.util.Timer { 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 @@ public class ManuallyAdvancingTimer extends java.util.Timer { 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 @@ public class ManuallyAdvancingTimer extends java.util.Timer { 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;