Fix ietf-type-util checkstyle 23/81123/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 22 Mar 2019 18:46:53 +0000 (19:46 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 22 Mar 2019 19:00:53 +0000 (20:00 +0100)
This fixes checkstyle violations, adding a few suppressions and
remove UnsupportedExceptionThrows and related reflection-based tests.

Change-Id: I0c1b8828a881299dde6df4fe666df000f0ec486b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
model/ietf/ietf-type-util/pom.xml
model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java
model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java
model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv4Utils.java
model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv6Utils.java
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv4UtilsTest.java [deleted file]
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv6UtilsTest.java
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacClass.java
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/PhysClass.java

index 50f5ff08978982e80bc004b6847145257b4c224e..b4e855a088510a6c2ba95feee7f09b9d107a3f64 100644 (file)
                     </instructions>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-checkstyle-plugin</artifactId>
+                <configuration>
+                    <propertyExpansion>checkstyle.violationSeverity=error</propertyExpansion>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 
index 7321274ec4eba3dc6a00b40af81c6c4f896fcd22..85dc1c632364c1c8d8c8d0b44dcc4a0f967b0074 100644 (file)
@@ -26,9 +26,11 @@ import org.opendaylight.mdsal.binding.spec.reflect.StringValueObjectFactory;
  * A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
  */
 @Beta
+@SuppressWarnings("checkstyle:classTypeParameterName")
 public abstract class AbstractIetfInetUtil<A4, A4NZ extends A4, P4, A6, A6NZ extends A6, P6, A, ANZ, P> {
     private static final int INET4_LENGTH = 4;
     private static final int INET6_LENGTH = 16;
+
     private final StringValueObjectFactory<A4> address4Factory;
     private final StringValueObjectFactory<A4NZ> address4NoZoneFactory;
     private final StringValueObjectFactory<P4> prefix4Factory;
@@ -48,20 +50,31 @@ public abstract class AbstractIetfInetUtil<A4, A4NZ extends A4, P4, A6, A6NZ ext
     }
 
     protected abstract @NonNull A ipv4Address(@NonNull A4 addr);
+
     protected abstract @NonNull ANZ ipv4AddressNoZone(@NonNull A4NZ addr);
+
     protected abstract @NonNull A ipv6Address(@NonNull A6 addr);
+
     protected abstract @NonNull ANZ ipv6AddressNoZone(@NonNull A6NZ addr);
 
     protected abstract @Nullable A4 maybeIpv4Address(@NonNull A addr);
+
     protected abstract @Nullable A4NZ maybeIpv4AddressNoZone(@NonNull ANZ addr);
+
     protected abstract @Nullable A6 maybeIpv6Address(@NonNull A addr);
+
     protected abstract @Nullable A6NZ maybeIpv6AddressNoZone(@NonNull ANZ addr);
 
     protected abstract @NonNull P ipv4Prefix(@NonNull P4 addr);
+
     protected abstract @NonNull P ipv6Prefix(@NonNull P6 addr);
+
     protected abstract @NonNull String ipv4AddressString(@NonNull A4 addr);
+
     protected abstract @NonNull String ipv6AddressString(@NonNull A6 addr);
+
     protected abstract @NonNull String ipv4PrefixString(@NonNull P4 prefix);
+
     protected abstract @NonNull String ipv6PrefixString(@NonNull P6 prefix);
 
     /**
@@ -324,26 +337,8 @@ public abstract class AbstractIetfInetUtil<A4, A4NZ extends A4, P4, A6, A6NZ ext
         return prefix4Factory.newInstance(prefixStringV4(address, mask));
     }
 
-    public final @NonNull P4 ipv4PrefixForShort(final byte @NonNull[] address, final int mask) {
-        if (mask == 0) {
-            // Easy case, reuse the template
-            return prefix4Factory.getTemplate();
-        }
-
-        return v4PrefixForShort(address, 0, mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1), mask);
-    }
-
-    public final @NonNull P4 ipv4PrefixForShort(final byte @NonNull[] array, final int startOffset, final int mask) {
-        if (mask == 0) {
-            // Easy case, reuse the template
-            return prefix4Factory.getTemplate();
-        }
-
-        return v4PrefixForShort(array, startOffset, mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1), mask);
-    }
-
     /**
-     * Create a /32 Ipv4Prefix for an {@link Inet4Address}
+     * Create a /32 Ipv4Prefix for an {@link Inet4Address}.
      *
      * @param addr An {@link Inet4Address}
      * @return An Ipv4Prefix object
@@ -391,6 +386,24 @@ public abstract class AbstractIetfInetUtil<A4, A4NZ extends A4, P4, A6, A6NZ ext
         return newIpv4Prefix(ipv4AddressString(addr), mask);
     }
 
+    public final @NonNull P4 ipv4PrefixForShort(final byte @NonNull[] address, final int mask) {
+        if (mask == 0) {
+            // Easy case, reuse the template
+            return prefix4Factory.getTemplate();
+        }
+
+        return v4PrefixForShort(address, 0, mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1), mask);
+    }
+
+    public final @NonNull P4 ipv4PrefixForShort(final byte @NonNull[] array, final int startOffset, final int mask) {
+        if (mask == 0) {
+            // Easy case, reuse the template
+            return prefix4Factory.getTemplate();
+        }
+
+        return v4PrefixForShort(array, startOffset, mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1), mask);
+    }
+
     private static String stripZone(final String str) {
         final int percent = str.indexOf('%');
         return percent == -1 ? str : str.substring(0, percent);
@@ -524,25 +537,6 @@ public abstract class AbstractIetfInetUtil<A4, A4NZ extends A4, P4, A6, A6NZ ext
         return prefix6Factory.newInstance(addressStringV6(address) + '/' + mask);
     }
 
-    public final @NonNull P6 ipv6PrefixForShort(final byte @NonNull[] address, final int mask) {
-        return ipv6PrefixForShort(address, 0, mask);
-    }
-
-    public final @NonNull P6 ipv6PrefixForShort(final byte @NonNull[] array, final int startOffset, final int mask) {
-        if (mask == 0) {
-            // Easy case, reuse the template
-            return prefix6Factory.getTemplate();
-        }
-
-        checkArgument(mask > 0 && mask <= 128, "Invalid mask %s", mask);
-        final int size = mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1);
-
-        // Until we can instantiate an IPv6 address for a partial array, use a temporary buffer
-        byte[] tmp = new byte[INET6_LENGTH];
-        System.arraycopy(array, startOffset, tmp, 0, size);
-        return ipv6PrefixFor(tmp, mask);
-    }
-
     /**
      * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv4 address.
      *
@@ -593,6 +587,25 @@ public abstract class AbstractIetfInetUtil<A4, A4NZ extends A4, P4, A6, A6NZ ext
         return newIpv6Prefix(ipv6AddressString(addr), mask);
     }
 
+    public final @NonNull P6 ipv6PrefixForShort(final byte @NonNull[] address, final int mask) {
+        return ipv6PrefixForShort(address, 0, mask);
+    }
+
+    public final @NonNull P6 ipv6PrefixForShort(final byte @NonNull[] array, final int startOffset, final int mask) {
+        if (mask == 0) {
+            // Easy case, reuse the template
+            return prefix6Factory.getTemplate();
+        }
+
+        checkArgument(mask > 0 && mask <= 128, "Invalid mask %s", mask);
+        final int size = mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1);
+
+        // Until we can instantiate an IPv6 address for a partial array, use a temporary buffer
+        byte[] tmp = new byte[INET6_LENGTH];
+        System.arraycopy(array, startOffset, tmp, 0, size);
+        return ipv6PrefixFor(tmp, mask);
+    }
+
     private P6 newIpv6Prefix(final String addr, final int mask) {
         checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
         return prefix6Factory.newInstance(addr + '/' + mask);
index 6edc034c6a044ce3732ac5479e8816f4261aa4bd..6f1a05417168bcbbafe588cf92b31c0bf563eba8 100644 (file)
@@ -26,6 +26,7 @@ public abstract class AbstractIetfYangUtil<M, P> {
     private static final int MAC_BYTE_LENGTH = 6;
     private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
     private static final byte[] HEX_VALUES;
+
     static {
         final byte[] b = new byte['f' + 1];
         Arrays.fill(b, (byte)-1);
@@ -118,21 +119,21 @@ public abstract class AbstractIetfYangUtil<M, P> {
 
     protected abstract String getPhysValue(P physAddress);
 
-    static byte hexValue(final char c) {
-        byte v;
+    static byte hexValue(final char ch) {
+        byte value;
         try {
             // Performance optimization: access the array and rely on the VM for catching
             // illegal access (which boils down to illegal character, which should never happen.
-            v = HEX_VALUES[c];
+            value = HEX_VALUES[ch];
         } catch (IndexOutOfBoundsException e) {
-            v = -1;
+            value = -1;
         }
 
-        if (v < 0) {
-            throw new IllegalArgumentException("Invalid character '" + c + "' encountered");
+        if (value < 0) {
+            throw new IllegalArgumentException("Invalid character '" + ch + "' encountered");
         }
 
-        return v;
+        return value;
     }
 
     /**
@@ -178,8 +179,8 @@ public abstract class AbstractIetfYangUtil<M, P> {
         return sb.toString();
     }
 
-    private static final void appendHexByte(final StringBuilder sb, final byte b) {
-        final int v = Byte.toUnsignedInt(b);
-        sb.append(HEX_CHARS[v >>> 4]).append(HEX_CHARS[v & 15]);
+    private static void appendHexByte(final StringBuilder sb, final byte byteVal) {
+        final int intVal = Byte.toUnsignedInt(byteVal);
+        sb.append(HEX_CHARS[intVal >>> 4]).append(HEX_CHARS[intVal & 15]);
     }
 }
index 13d4537386b7d2c9dc8b508638746954f5c43013..b79e2866810c2f813db0e873caa92db322e4cd1a 100644 (file)
@@ -16,7 +16,7 @@ import org.eclipse.jdt.annotation.NonNull;
  */
 final class Ipv4Utils {
     private Ipv4Utils() {
-        throw new UnsupportedOperationException();
+
     }
 
     static void fillIpv4Bytes(final byte @NonNull[] bytes, final int byteStart, final String str, final int strStart,
index 001921508b31794c5f8549e91ff897b385ae2ed3..86fcb224da019c6947ae14494a85507a045b10a4 100644 (file)
@@ -49,91 +49,92 @@ final class Ipv6Utils {
     private static final int INT16SZ = Short.BYTES;
 
     private Ipv6Utils() {
-        throw new UnsupportedOperationException();
+
     }
 
     /**
-     * Convert Ipv6Address object to a valid Canonical v6 address in byte format
+     * Convert Ipv6Address object to a valid Canonical v6 address in byte format.
      *
      * @param bytes Byte array for output
      * @param str String representation
      * @param strLimit String offset which should not be processed
      * @throws NullPointerException if ipv6address is null
      */
+    @SuppressWarnings("checkstyle:localVariableName")
     static void fillIpv6Bytes(final byte @NonNull[] bytes, final String str, final int strLimit) {
-       // Leading :: requires some special handling.
-       int i = 0;
-       if (str.charAt(i) == ':') {
-           // Note ++i side-effect in check
-           checkArgument(str.charAt(++i) == ':', "Invalid v6 address '%s'", str);
-       }
+        // Leading :: requires some special handling.
+        int i = 0;
+        if (str.charAt(i) == ':') {
+            // Note ++i side-effect in check
+            checkArgument(str.charAt(++i) == ':', "Invalid v6 address '%s'", str);
+        }
 
-       boolean haveVal = false;
-       int val = 0;
-       int colonp = -1;
-       int j = 0;
-       int curtok = i;
-       while (i < strLimit) {
-           final char ch = str.charAt(i++);
+        boolean haveVal = false;
+        int val = 0;
+        int colonp = -1;
+        int j = 0;
+        int curtok = i;
+        while (i < strLimit) {
+            final char ch = str.charAt(i++);
 
-           // v6 separator
-           if (ch == ':') {
-               curtok = i;
-               if (haveVal) {
-                   // removed overrun check - the regexp checks for valid data
-                   bytes[j++] = (byte) (val >>> 8 & 0xff);
-                   bytes[j++] = (byte) (val & 0xff);
-                   haveVal = false;
-                   val = 0;
-               } else {
-                   // no need to check separator position validity - regexp does that
-                   colonp = j;
-               }
+            // v6 separator
+            if (ch == ':') {
+                curtok = i;
+                if (haveVal) {
+                    // removed overrun check - the regexp checks for valid data
+                    bytes[j++] = (byte) (val >>> 8 & 0xff);
+                    bytes[j++] = (byte) (val & 0xff);
+                    haveVal = false;
+                    val = 0;
+                } else {
+                    // no need to check separator position validity - regexp does that
+                    colonp = j;
+                }
 
-               continue;
-           }
+                continue;
+            }
 
-           // frankenstein - v4 attached to v6, mixed notation
-           if (ch == '.' && j + INADDR4SZ <= INADDR6SZ) {
-               /*
-                * This has passed the regexp so it is fairly safe to parse it
-                * straight away. Use the Ipv4Utils for that.
-                */
-               Ipv4Utils.fillIpv4Bytes(bytes, j, str, curtok, strLimit);
-               j += INADDR4SZ;
-               haveVal = false;
-               break;
-           }
+            // frankenstein - v4 attached to v6, mixed notation
+            if (ch == '.' && j + INADDR4SZ <= INADDR6SZ) {
+                /*
+                 * This has passed the regexp so it is fairly safe to parse it
+                 * straight away. Use the Ipv4Utils for that.
+                 */
+                Ipv4Utils.fillIpv4Bytes(bytes, j, str, curtok, strLimit);
+                j += INADDR4SZ;
+                haveVal = false;
+                break;
+            }
 
-           /*
-            * Business as usual - ipv6 address digit.
-            * We can remove all checks from the original BSD code because
-            * the regexp has already verified that we are not being fed
-            * anything bigger than 0xffff between the separators.
-            */
-           final int chval = AbstractIetfYangUtil.hexValue(ch);
-           val = val << 4 | chval;
-           haveVal = true;
-       }
+            /*
+             * Business as usual - ipv6 address digit.
+             * We can remove all checks from the original BSD code because
+             * the regexp has already verified that we are not being fed
+             * anything bigger than 0xffff between the separators.
+             */
+            final int chval = AbstractIetfYangUtil.hexValue(ch);
+            val = val << 4 | chval;
+            haveVal = true;
+        }
 
-       if (haveVal) {
-           verify(j + INT16SZ <= INADDR6SZ, "Overrun in parsing of '%s', should not occur", str);
-           bytes[j++] = (byte) (val >> 8 & 0xff);
-           bytes[j++] = (byte) (val & 0xff);
-       }
+        if (haveVal) {
+            verify(j + INT16SZ <= INADDR6SZ, "Overrun in parsing of '%s', should not occur", str);
+            bytes[j++] = (byte) (val >> 8 & 0xff);
+            bytes[j++] = (byte) (val & 0xff);
+        }
 
-       if (colonp != -1) {
-           verify(j != INADDR6SZ, "Overrun in parsing of '%s', should not occur", str);
-           expandZeros(bytes, colonp, j);
-       } else {
-           verify(j == INADDR6SZ, "Overrun in parsing of '%s', should not occur", str);
-       }
-   }
+        if (colonp != -1) {
+            verify(j != INADDR6SZ, "Overrun in parsing of '%s', should not occur", str);
+            expandZeros(bytes, colonp, j);
+        } else {
+            verify(j == INADDR6SZ, "Overrun in parsing of '%s', should not occur", str);
+        }
+    }
 
-   private static void expandZeros(final byte[] bytes, final int where, final int filledBytes) {
-       final int tailLength = filledBytes - where;
-       final int tailOffset = INADDR6SZ - tailLength;
-       System.arraycopy(bytes, where, bytes, tailOffset, tailLength);
-       Arrays.fill(bytes, where, tailOffset, (byte)0);
+    private static void expandZeros(final byte[] bytes, final int where, final int filledBytes) {
+        final int tailLength = filledBytes - where;
+        final int tailOffset = INADDR6SZ - tailLength;
+        System.arraycopy(bytes, where, bytes, tailOffset, tailLength);
+        Arrays.fill(bytes, where, tailOffset, (byte)0);
     }
 }
index 21322b375be6261b995374d1efc70e5bfa016cd4..3e9d64474aa686cf514f5df046c021f225a12c9b 100644 (file)
@@ -5,11 +5,11 @@
  * 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.mdsal.model.ietf.util;
 
 import static java.util.Objects.requireNonNull;
 
+@SuppressWarnings("checkstyle:memberName")
 public class IpClass {
     private final String _value;
 
diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv4UtilsTest.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv4UtilsTest.java
deleted file mode 100644 (file)
index 7019dc6..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * 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.mdsal.model.ietf.util;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
-
-import java.lang.reflect.Constructor;
-import org.junit.Test;
-
-public class Ipv4UtilsTest {
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void privateConstructTest() throws Throwable {
-        final Constructor<Ipv4Utils> constructor = Ipv4Utils.class.getDeclaredConstructor();
-        assertFalse(constructor.isAccessible());
-        constructor.setAccessible(true);
-        try {
-            constructor.newInstance();
-            fail("Exception should be thrown");
-        } catch (Exception e) {
-            throw e.getCause();
-        }
-    }
-}
\ No newline at end of file
index 7641e6a57b81f362db69af4b5525505c1b23786f..75f1ab63594ebad03d47abdf33560e19b1af1f67 100644 (file)
@@ -10,11 +10,8 @@ package org.opendaylight.mdsal.model.ietf.util;
 
 import static com.google.common.net.InetAddresses.forString;
 import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
 import static org.opendaylight.mdsal.model.ietf.util.Ipv6Utils.fillIpv6Bytes;
 
-import java.lang.reflect.Constructor;
 import org.junit.Test;
 
 public class Ipv6UtilsTest {
@@ -49,8 +46,9 @@ public class Ipv6UtilsTest {
         assertArrayEquals(test1, bytesForString("0:0:0:0:0:0:13.1.68.3"));
         assertArrayEquals(test1, bytesForString("::13.1.68.3"));
 
-        final byte[] test2 = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte)255, (byte)255,
-                (byte)129, (byte)144, 52, 38 };
+        final byte[] test2 = new byte[] {
+            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte)255, (byte)255, (byte)129, (byte)144, 52, 38
+        };
         assertArrayEquals(test2, bytesForString("0:0:0:0:0:FFFF:129.144.52.38"));
         assertArrayEquals(test2, bytesForString("::FFFF:129.144.52.38"));
     }
@@ -113,17 +111,4 @@ public class Ipv6UtilsTest {
     private static void assertEqualResult(final String str) {
         assertArrayEquals(forString(str).getAddress(), bytesForString(str));
     }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void privateConstructTest() throws Throwable {
-        final Constructor<Ipv6Utils> constructor = Ipv6Utils.class.getDeclaredConstructor();
-        assertFalse(constructor.isAccessible());
-        constructor.setAccessible(true);
-        try {
-            constructor.newInstance();
-            fail("Exception should be thrown");
-        } catch (Exception e) {
-            throw e.getCause();
-        }
-    }
-}
\ No newline at end of file
+}
index d08fbdd63512c0fae42fa19165d1bd61ac737ef8..439df7c69bbe89c8c7158eeeeaee73eab1be193c 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.mdsal.model.ietf.util;
 
 import static java.util.Objects.requireNonNull;
 
+@SuppressWarnings("checkstyle:memberName")
 public final class MacClass {
     private final String _value;
 
index 3cd1b0158cd537ca530a4be98f814ce308fd0164..fb983e9ff49e49909f90db8df0a6e00c1da86677 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.mdsal.model.ietf.util;
 
 import static java.util.Objects.requireNonNull;
 
+@SuppressWarnings("checkstyle:memberName")
 public final class PhysClass {
     private final String _value;