From e146ccacb12facdae44954f2f5b9d29976bfbe7b Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 22 Mar 2019 19:46:53 +0100 Subject: [PATCH] Fix ietf-type-util checkstyle This fixes checkstyle violations, adding a few suppressions and remove UnsupportedExceptionThrows and related reflection-based tests. Change-Id: I0c1b8828a881299dde6df4fe666df000f0ec486b Signed-off-by: Robert Varga --- model/ietf/ietf-type-util/pom.xml | 7 + .../model/ietf/util/AbstractIetfInetUtil.java | 89 +++++++----- .../model/ietf/util/AbstractIetfYangUtil.java | 21 +-- .../mdsal/model/ietf/util/Ipv4Utils.java | 2 +- .../mdsal/model/ietf/util/Ipv6Utils.java | 137 +++++++++--------- .../mdsal/model/ietf/util/IpClass.java | 2 +- .../mdsal/model/ietf/util/Ipv4UtilsTest.java | 31 ---- .../mdsal/model/ietf/util/Ipv6UtilsTest.java | 23 +-- .../mdsal/model/ietf/util/MacClass.java | 1 + .../mdsal/model/ietf/util/PhysClass.java | 1 + 10 files changed, 146 insertions(+), 168 deletions(-) delete mode 100644 model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv4UtilsTest.java diff --git a/model/ietf/ietf-type-util/pom.xml b/model/ietf/ietf-type-util/pom.xml index 50f5ff0897..b4e855a088 100644 --- a/model/ietf/ietf-type-util/pom.xml +++ b/model/ietf/ietf-type-util/pom.xml @@ -47,6 +47,13 @@ + + org.apache.maven.plugins + maven-checkstyle-plugin + + checkstyle.violationSeverity=error + + diff --git a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java index 7321274ec4..85dc1c6323 100644 --- a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java +++ b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java @@ -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 { private static final int INET4_LENGTH = 4; private static final int INET6_LENGTH = 16; + private final StringValueObjectFactory address4Factory; private final StringValueObjectFactory address4NoZoneFactory; private final StringValueObjectFactory prefix4Factory; @@ -48,20 +50,31 @@ public abstract class AbstractIetfInetUtil 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 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); diff --git a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java index 6edc034c6a..6f1a054171 100644 --- a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java +++ b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java @@ -26,6 +26,7 @@ public abstract class AbstractIetfYangUtil { 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 { 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 { 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]); } } diff --git a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv4Utils.java b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv4Utils.java index 13d4537386..b79e286681 100644 --- a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv4Utils.java +++ b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv4Utils.java @@ -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, diff --git a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv6Utils.java b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv6Utils.java index 001921508b..86fcb224da 100644 --- a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv6Utils.java +++ b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv6Utils.java @@ -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); } } diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java index 21322b375b..3e9d64474a 100644 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java +++ b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java @@ -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 index 7019dc68da..0000000000 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv4UtilsTest.java +++ /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 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 diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv6UtilsTest.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv6UtilsTest.java index 7641e6a57b..75f1ab6359 100644 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv6UtilsTest.java +++ b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv6UtilsTest.java @@ -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 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 +} diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacClass.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacClass.java index d08fbdd635..439df7c69b 100644 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacClass.java +++ b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacClass.java @@ -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; diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/PhysClass.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/PhysClass.java index 3cd1b0158c..fb983e9ff4 100644 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/PhysClass.java +++ b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/PhysClass.java @@ -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; -- 2.36.6