Fix illegalCatch suppressions 75/90975/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 8 Jul 2020 08:40:41 +0000 (10:40 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 8 Jul 2020 09:02:15 +0000 (11:02 +0200)
We are catching multiple checked exceptions here, but really we are
being lazy. Enumerate caught exceptions and remove suppression.

Change-Id: Ia2d57baf109e19459c662d9e1e3ccd9373b7c08d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/ops/AbstractEdit.java
netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/ops/RuntimeRpc.java
netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/AbstractNetconfOperationTest.java
netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/RuntimeRpcTest.java
netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/get/FilterContentValidatorTest.java
netconf/mdsal-netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/SshProxyServer.java
netconf/mdsal-netconf-ssh/src/test/java/org/opendaylight/netconf/netty/EchoClient.java
netconf/mdsal-netconf-ssh/src/test/java/org/opendaylight/netconf/netty/EchoServer.java
netconf/mdsal-netconf-ssh/src/test/java/org/opendaylight/netconf/netty/ProxyServer.java
netconf/mdsal-netconf-ssh/src/test/java/org/opendaylight/netconf/netty/SSHTest.java

index 2a99755c4e3f6bcc11a79f56bfcaf38fc91660eb..2576641b8386f889c89d14cf3338065e0c30caac 100644 (file)
@@ -8,11 +8,13 @@
 package org.opendaylight.netconf.mdsal.connector.ops;
 
 import com.google.common.collect.ImmutableMap;
+import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Optional;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.transform.dom.DOMSource;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
@@ -32,6 +34,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
 
 abstract class AbstractEdit extends AbstractConfigOperation {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractEdit.class);
@@ -44,7 +47,6 @@ abstract class AbstractEdit extends AbstractConfigOperation {
         this.schemaContext = schemaContext;
     }
 
-    @SuppressWarnings("checkstyle:IllegalCatch")
     protected void parseIntoNormalizedNode(final DataSchemaNode schemaNode, final XmlElement element,
                                          final NormalizedNodeStreamWriter writer) throws DocumentedException {
         if (!(schemaNode instanceof ContainerSchemaNode) && !(schemaNode instanceof ListSchemaNode)) {
@@ -57,7 +59,7 @@ abstract class AbstractEdit extends AbstractConfigOperation {
         final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext.getCurrentContext(), schemaNode);
         try {
             xmlParser.traverse(new DOMSource(element.getDomElement()));
-        } catch (final Exception ex) {
+        } catch (final XMLStreamException | URISyntaxException | IOException | SAXException ex) {
             throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex, ErrorType.PROTOCOL,
                 ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
         }
index f29adff83a65e67463c3af912ef0689b290e2a4c..747ae2103aa9a1a007747a9d6d8fc85f0830aa59 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.netconf.mdsal.connector.ops;
 
 import java.io.IOException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Map;
@@ -54,6 +55,7 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
 
 public class RuntimeRpc extends AbstractSingletonNetconfOperation {
 
@@ -252,7 +254,6 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
      * @param input   input container schema node, or null if rpc does not take any input
      * @return parsed rpc into normalized node, or null if input schema is null
      */
-    @SuppressWarnings("checkstyle:IllegalCatch")
     private @Nullable ContainerNode rpcToNNode(final XmlElement element,
             final @Nullable ContainerSchemaNode input) throws DocumentedException {
         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
@@ -261,7 +262,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
 
         try {
             xmlParser.traverse(new DOMSource(element.getDomElement()));
-        } catch (final Exception ex) {
+        } catch (final XMLStreamException | URISyntaxException | IOException | SAXException ex) {
             throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex, ErrorType.PROTOCOL,
                     ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
         }
index 2f1ae629f911e46b3946d2eb976753005ec76ec1..ccc2e78acc2102b4d9d30698241fd25be168f1be 100644 (file)
@@ -16,9 +16,11 @@ import static org.opendaylight.yangtools.yang.test.util.YangParserTestUtils.pars
 import com.google.common.io.ByteSource;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.MoreExecutors;
+import java.io.IOException;
 import java.io.StringWriter;
 import java.util.EnumMap;
 import java.util.concurrent.ExecutorService;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
@@ -53,6 +55,7 @@ import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
 
 public abstract class AbstractNetconfOperationTest {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractNetconfOperationTest.class);
@@ -114,12 +117,11 @@ public abstract class AbstractNetconfOperationTest {
         return transactionProvider;
     }
 
-    @SuppressWarnings("illegalCatch")
     private static Document getReplyOk() {
         Document doc;
         try {
             doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
-        } catch (final Exception e) {
+        } catch (final IOException | SAXException | ParserConfigurationException e) {
             LOG.debug("unable to load rpc reply ok.", e);
             doc = XmlUtil.newDocument();
         }
index ac84a0c8aa3f18bfe467df72b355b5feb0237517..d27385ea5658c8b4da05d974fdc030ac90fa061a 100644 (file)
@@ -18,9 +18,11 @@ import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediate
 import com.google.common.base.Preconditions;
 import com.google.common.io.ByteSource;
 import com.google.common.util.concurrent.FluentFuture;
+import java.io.IOException;
 import java.net.URI;
 import java.util.Collection;
 import java.util.Collections;
+import javax.xml.parsers.ParserConfigurationException;
 import org.custommonkey.xmlunit.DetailedDiff;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
@@ -69,23 +71,21 @@ import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
 
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class RuntimeRpcTest {
     private static final Logger LOG = LoggerFactory.getLogger(RuntimeRpcTest.class);
     private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
-    private static final Document RPC_REPLY_OK = RuntimeRpcTest.getReplyOk();
+    private static final Document RPC_REPLY_OK = getReplyOk();
 
-    @SuppressWarnings("illegalCatch")
     private static Document getReplyOk() {
-        Document doc;
         try {
-            doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/runtimerpc-ok-reply.xml");
-        } catch (final Exception e) {
+            return XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/runtimerpc-ok-reply.xml");
+        } catch (final IOException | SAXException | ParserConfigurationException e) {
             LOG.debug("unable to load rpc reply ok.", e);
-            doc = XmlUtil.newDocument();
+            return XmlUtil.newDocument();
         }
-        return doc;
     }
 
     private static final DOMRpcService RPC_SERVICE_VOID_INVOKER = new DOMRpcService() {
index 3b493d27825a8d00202c4a0e9408bce3d1297f8f..180c0bbd1e306bc51e171b79c997f5f939c31f17 100644 (file)
@@ -8,6 +8,10 @@
 package org.opendaylight.netconf.mdsal.connector.ops.get;
 
 import static java.util.Objects.requireNonNull;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeThat;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
@@ -22,12 +26,12 @@ import java.util.List;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 import org.junit.runners.model.InitializationError;
+import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.xml.XmlElement;
 import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
@@ -81,21 +85,25 @@ public class FilterContentValidatorTest {
         validator = new FilterContentValidator(currentContext);
     }
 
-    @SuppressWarnings("checkstyle:IllegalCatch")
     @Test
-    public void testValidate() throws Exception {
-        if (expected.startsWith("success")) {
-            final String expId = expected.replace("success=", "");
-            final YangInstanceIdentifier actual = validator.validate(filterContent);
-            Assert.assertEquals(fromString(expId), actual);
-        } else if (expected.startsWith("error")) {
-            try {
-                validator.validate(filterContent);
-                Assert.fail(XmlUtil.toString(filterContent) + " is not valid and should throw exception.");
-            } catch (final Exception e) {
-                final String expectedExceptionClass = expected.replace("error=", "");
-                Assert.assertEquals(expectedExceptionClass, e.getClass().getName());
-            }
+    public void testValidateSuccess() throws DocumentedException {
+        assumeThat(expected, startsWith("success"));
+
+        final String expId = expected.replace("success=", "");
+        final YangInstanceIdentifier actual = validator.validate(filterContent);
+        assertEquals(fromString(expId), actual);
+    }
+
+    @Test
+    public void testValidateError() {
+        assumeThat(expected, startsWith("error"));
+
+        try {
+            validator.validate(filterContent);
+            fail(XmlUtil.toString(filterContent) + " is not valid and should throw exception.");
+        } catch (final DocumentedException e) {
+            final String expectedExceptionClass = expected.replace("error=", "");
+            assertEquals(expectedExceptionClass, e.getClass().getName());
         }
     }
 
index cf9cdb817a98efa49f5440fb0f5d3c474aa95cca..b294da39b545381af8fed361f4fa856469788ca9 100644 (file)
@@ -154,13 +154,12 @@ public class SshProxyServer implements AutoCloseable {
             super(manager, group);
         }
 
-        @SuppressWarnings("checkstyle:IllegalCatch")
         @Override
         protected void doCloseImmediately() {
             try {
                 group().shutdownNow();
                 group().awaitTermination(5, TimeUnit.SECONDS);
-            } catch (final Exception e) {
+            } catch (final IOException | InterruptedException e) {
                 log.debug("Exception caught while closing channel group", e);
             } finally {
                 super.doCloseImmediately();
index 2cdd97c9f90994a47472439a9320ad151a3a92b6..359f9dfbdc48543f858fdfcff0531a8822c43146 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.netty;
 
 import io.netty.bootstrap.Bootstrap;
@@ -28,24 +27,21 @@ import org.slf4j.LoggerFactory;
 public class EchoClient extends Thread {
     private static final Logger LOG = LoggerFactory.getLogger(EchoClient.class);
 
-
     private final ChannelInitializer<LocalChannel> channelInitializer;
 
-
     public EchoClient(final ChannelHandler clientHandler) {
-        channelInitializer = new ChannelInitializer<LocalChannel>() {
+        channelInitializer = new ChannelInitializer<>() {
             @Override
-            public void initChannel(LocalChannel ch) throws Exception {
+            public void initChannel(final LocalChannel ch) throws Exception {
                 ch.pipeline().addLast(clientHandler);
             }
         };
     }
 
-    public EchoClient(ChannelInitializer<LocalChannel> channelInitializer) {
+    public EchoClient(final ChannelInitializer<LocalChannel> channelInitializer) {
         this.channelInitializer = channelInitializer;
     }
 
-    @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     public void run() {
         // Configure the client.
@@ -63,7 +59,7 @@ public class EchoClient extends Thread {
 
             // Wait until the connection is closed.
             future.channel().closeFuture().sync();
-        } catch (Exception e) {
+        } catch (InterruptedException e) {
             LOG.error("Error in client", e);
             throw new RuntimeException("Error in client", e);
         } finally {
index fed42f255b57af182c8312530bbbddee247b4ebc..18b83b51936f4a5dd589e232dad274545641a9e6 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.netty;
 
 import io.netty.bootstrap.ServerBootstrap;
@@ -20,6 +19,7 @@ import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.handler.logging.LogLevel;
 import io.netty.handler.logging.LoggingHandler;
 import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import org.opendaylight.netconf.util.NetconfConfiguration;
 import org.slf4j.Logger;
@@ -31,7 +31,7 @@ import org.slf4j.LoggerFactory;
 public class EchoServer implements Runnable {
     private static final Logger LOG = LoggerFactory.getLogger(EchoServer.class);
 
-    @SuppressWarnings("checkstyle:IllegalCatch")
+    @Override
     public void run() {
         // Configure the server.
         EventLoopGroup bossGroup = new NioEventLoopGroup(1);
@@ -44,7 +44,7 @@ public class EchoServer implements Runnable {
                     .handler(new LoggingHandler(LogLevel.INFO))
                     .childHandler(new ChannelInitializer<LocalChannel>() {
                         @Override
-                        public void initChannel(LocalChannel ch) throws Exception {
+                        public void initChannel(final LocalChannel ch) throws Exception {
                             ch.pipeline().addLast(new EchoServerHandler());
                         }
                     });
@@ -55,7 +55,7 @@ public class EchoServer implements Runnable {
 
             // Wait until the server socket is closed.
             future.channel().closeFuture().sync();
-        } catch (Exception e) {
+        } catch (InterruptedException e) {
             throw new RuntimeException(e);
         } finally {
             // Shut down all event loops to terminate all threads.
@@ -64,7 +64,7 @@ public class EchoServer implements Runnable {
         }
     }
 
-    public static void main(String[] args) throws Exception {
+    public static void main(final String[] args) throws InterruptedException, IOException {
         new Thread(new EchoServer()).start();
         Thread.sleep(1000);
         EchoClientHandler clientHandler = new EchoClientHandler();
index 9f471c4d562ccfc9488338cb6e7dd8de6cb83998..01a1261849b09299db03d68d7f3c278f552b48f2 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.netty;
 
 import io.netty.bootstrap.ServerBootstrap;
@@ -26,11 +25,11 @@ import org.opendaylight.netconf.util.NetconfConfiguration;
 public class ProxyServer implements Runnable {
     private final ProxyHandlerFactory proxyHandlerFactory;
 
-    public ProxyServer(ProxyHandlerFactory proxyHandlerFactory) {
+    public ProxyServer(final ProxyHandlerFactory proxyHandlerFactory) {
         this.proxyHandlerFactory = proxyHandlerFactory;
     }
 
-    @SuppressWarnings("checkstyle:IllegalCatch")
+    @Override
     public void run() {
         // Configure the server.
         final EventLoopGroup bossGroup = new NioEventLoopGroup();
@@ -44,7 +43,7 @@ public class ProxyServer implements Runnable {
                     .handler(new LoggingHandler(LogLevel.INFO))
                     .childHandler(new ChannelInitializer<SocketChannel>() {
                         @Override
-                        public void initChannel(SocketChannel ch) throws Exception {
+                        public void initChannel(final SocketChannel ch) throws Exception {
                             ch.pipeline().addLast(proxyHandlerFactory.create(bossGroup, localAddress));
                         }
                     });
@@ -55,7 +54,7 @@ public class ProxyServer implements Runnable {
 
             // Wait until the server socket is closed.
             future.channel().closeFuture().sync();
-        } catch (Exception e) {
+        } catch (InterruptedException e) {
             throw new RuntimeException(e);
         } finally {
             // Shut down all event loops to terminate all threads.
@@ -68,12 +67,12 @@ public class ProxyServer implements Runnable {
         ChannelHandler create(EventLoopGroup bossGroup, LocalAddress localAddress);
     }
 
-    public static void main(String[] args) {
+    public static void main(final String[] args) {
         ProxyHandlerFactory proxyHandlerFactory = ProxyServerHandler::new;
         start(proxyHandlerFactory);
     }
 
-    public static void start(ProxyHandlerFactory proxyHandlerFactory) {
+    public static void start(final ProxyHandlerFactory proxyHandlerFactory) {
         new Thread(new EchoServer()).start();
         new Thread(new ProxyServer(proxyHandlerFactory)).start();
     }
index dbc19dcfdade8797239a71571e27efdddcc2476d..126f425097acce9c99191974c12c6ad526aa9a2f 100644 (file)
@@ -105,7 +105,7 @@ public class SSHTest {
 
     public EchoClientHandler connectClient(final InetSocketAddress address) {
         final EchoClientHandler echoClientHandler = new EchoClientHandler();
-        final ChannelInitializer<NioSocketChannel> channelInitializer = new ChannelInitializer<NioSocketChannel>() {
+        final ChannelInitializer<NioSocketChannel> channelInitializer = new ChannelInitializer<>() {
             @Override
             public void initChannel(final NioSocketChannel ch) throws Exception {
                 ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(new LoginPasswordHandler("a", "a")));