You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2023/05/13 11:26:39 UTC

[tomcat] branch 10.1.x updated: Clean-up. CheckStyle and formatting.

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new c71254215f Clean-up. CheckStyle and formatting.
c71254215f is described below

commit c71254215fa9919593bfcc50e1325cd15fc3040e
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Sat May 13 12:25:19 2023 +0100

    Clean-up. CheckStyle and formatting.
    
    Fix various CheckStyle errors. Mostly Javadoc tweaks. Run the IDE
    formatter to the standard formatting rules for good measure.
---
 java/org/apache/catalina/util/NetMask.java        | 115 +++++++++-------------
 java/org/apache/catalina/util/NetMaskSet.java     |  41 ++++----
 test/org/apache/catalina/util/TestNetMask.java    |   5 +-
 test/org/apache/catalina/util/TestNetMaskSet.java |   4 +-
 4 files changed, 76 insertions(+), 89 deletions(-)

diff --git a/java/org/apache/catalina/util/NetMask.java b/java/org/apache/catalina/util/NetMask.java
index b97eee934f..a41cda1cf0 100644
--- a/java/org/apache/catalina/util/NetMask.java
+++ b/java/org/apache/catalina/util/NetMask.java
@@ -19,7 +19,6 @@ package org.apache.catalina.util;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.Arrays;
-import java.util.Objects;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
 
@@ -27,28 +26,19 @@ import org.apache.tomcat.util.res.StringManager;
 
 /**
  * A class representing a CIDR netmask.
- *
  * <p>
- * The constructor takes a string as an argument which represents a netmask, as
- * per the CIDR notation -- whether this netmask be IPv4 or IPv6. It then
- * extracts the network address (before the /) and the CIDR prefix (after the
- * /), and tells through the #matches() method whether a candidate
- * {@link InetAddress} object fits in the recorded range.
+ * The constructor takes a string as an argument which represents a netmask, as per the CIDR notation -- whether this
+ * netmask be IPv4 or IPv6. It then extracts the network address (before the /) and the CIDR prefix (after the /), and
+ * tells through the #matches() method whether a candidate {@link InetAddress} object fits in the recorded range.
  * </p>
- *
  * <p>
- * As byte arrays as returned by <code>InetAddress.getByName()</code> are always
- * in network byte order, finding a match is therefore as simple as testing
- * whether the n first bits (where n is the CIDR) are the same in both byte
- * arrays (the one of the network address and the one of the candidate address).
- * We do that by first doing byte comparisons, then testing the last bits if any
- * (that is, if the remainder of the integer division of the CIDR by 8 is not
- * 0).
+ * As byte arrays as returned by <code>InetAddress.getByName()</code> are always in network byte order, finding a match
+ * is therefore as simple as testing whether the n first bits (where n is the CIDR) are the same in both byte arrays
+ * (the one of the network address and the one of the candidate address). We do that by first doing byte comparisons,
+ * then testing the last bits if any (that is, if the remainder of the integer division of the CIDR by 8 is not 0).
  * </p>
- *
  * <p>
- * As a bonus, if no '/' is found in the input, it is assumed that an exact
- * address match is required.
+ * As a bonus, if no '/' is found in the input, it is assumed that an exact address match is required.
  * </p>
  */
 public final class NetMask {
@@ -71,8 +61,7 @@ public final class NetMask {
     private final int nrBytes;
 
     /**
-     * The right shift to apply to the last byte if CIDR % 8 is not 0; if it is
-     * 0, this variable is set to 0
+     * The right shift to apply to the last byte if CIDR % 8 is not 0; if it is 0, this variable is set to 0
      */
     private final int lastByteShift;
 
@@ -91,8 +80,9 @@ public final class NetMask {
      * Constructor
      *
      * @param input the CIDR netmask
-     * @throws IllegalArgumentException if the netmask is not correct (invalid
-     *             address specification, malformed CIDR prefix, etc)
+     *
+     * @throws IllegalArgumentException if the netmask is not correct (invalid address specification, malformed CIDR
+     *                                      prefix, etc)
      */
     public NetMask(final String input) {
 
@@ -135,8 +125,7 @@ public final class NetMask {
         }
 
         /*
-         * OK, we do have a netmask specified, so let's extract both the address
-         * and the CIDR.
+         * OK, we do have a netmask specified, so let's extract both the address and the CIDR.
          */
 
         final String addressPart = nonPortPart.substring(0, idx), cidrPart = nonPortPart.substring(idx + 1);
@@ -163,15 +152,14 @@ public final class NetMask {
         }
 
         /*
-         * We don't want a negative CIDR, nor do we want a CIDR which is greater
-         * than the address length (consider 0.0.0.0/33, or ::/129)
+         * We don't want a negative CIDR, nor do we want a CIDR which is greater than the address length (consider
+         * 0.0.0.0/33, or ::/129)
          */
         if (cidr < 0) {
             throw new IllegalArgumentException(sm.getString("netmask.cidrNegative", cidrPart));
         }
         if (cidr > addrlen) {
-            throw new IllegalArgumentException(
-                    sm.getString("netmask.cidrTooBig", cidrPart, Integer.valueOf(addrlen)));
+            throw new IllegalArgumentException(sm.getString("netmask.cidrTooBig", cidrPart, Integer.valueOf(addrlen)));
         }
 
         nrBytes = cidr / 8;
@@ -181,8 +169,8 @@ public final class NetMask {
          *
          * lastByteShift = (8 - (cidr % 8)) & 7;
          *
-         * But... It's not worth it. In fact, explaining why it could work would
-         * be too long to be worth the trouble, so let's do it the simple way...
+         * But... It's not worth it. In fact, explaining why it could work would be too long to be worth the trouble, so
+         * let's do it the simple way...
          */
 
         final int remainder = cidr % 8;
@@ -196,6 +184,7 @@ public final class NetMask {
      *
      * @param addr The {@link java.net.InetAddress} to test
      * @param port The port to test
+     *
      * @return true on match, false otherwise
      */
     public boolean matches(final InetAddress addr, int port) {
@@ -214,6 +203,7 @@ public final class NetMask {
      * Test if a given address matches this netmask.
      *
      * @param addr The {@link java.net.InetAddress} to test
+     *
      * @return true on match, false otherwise
      */
     public boolean matches(final InetAddress addr) {
@@ -224,8 +214,9 @@ public final class NetMask {
     /**
      * Test if a given address matches this netmask.
      *
-     * @param addr The {@link java.net.InetAddress} to test
+     * @param addr        The {@link java.net.InetAddress} to test
      * @param checkedPort Indicates, whether we already checked the port
+     *
      * @return true on match, false otherwise
      */
     public boolean matches(final InetAddress addr, boolean checkedPort) {
@@ -235,22 +226,17 @@ public final class NetMask {
         final byte[] candidate = addr.getAddress();
 
         /*
-         * OK, remember that a CIDR prefix tells the number of BITS which should
-         * be equal between this NetMask's recorded address (netaddr) and the
-         * candidate address. One byte is 8 bits, no matter what, and IP
-         * addresses, whether they be IPv4 or IPv6, are big endian, aka MSB,
-         * Most Significant Byte (first).
+         * OK, remember that a CIDR prefix tells the number of BITS which should be equal between this NetMask's
+         * recorded address (netaddr) and the candidate address. One byte is 8 bits, no matter what, and IP addresses,
+         * whether they be IPv4 or IPv6, are big endian, aka MSB, Most Significant Byte (first).
          *
-         * We therefore need to get the byte array of the candidate address,
-         * compare as many bytes of the candidate address with the recorded
-         * address as the CIDR prefix tells us to (that is, CIDR / 8), and then
-         * deal with the remaining bits -- if any.
+         * We therefore need to get the byte array of the candidate address, compare as many bytes of the candidate
+         * address with the recorded address as the CIDR prefix tells us to (that is, CIDR / 8), and then deal with the
+         * remaining bits -- if any.
          *
-         * But prior to that, a simple test can be done: we deal with IP
-         * addresses here, which means IPv4 and IPv6. IPv4 addresses are encoded
-         * on 4 bytes, IPv6 addresses are encoded on 16 bytes. If the candidate
-         * address length is different than this NetMask's address, we don't
-         * have a match.
+         * But prior to that, a simple test can be done: we deal with IP addresses here, which means IPv4 and IPv6. IPv4
+         * addresses are encoded on 4 bytes, IPv6 addresses are encoded on 16 bytes. If the candidate address length is
+         * different than this NetMask's address, we don't have a match.
          */
         if (candidate.length != netaddr.length) {
             return false;
@@ -258,13 +244,11 @@ public final class NetMask {
 
 
         /*
-         * Now do the byte-compare. The constructor has recorded the number of
-         * bytes to compare in nrBytes, use that. If any of the byte we have to
-         * compare is different than what we expect, we don't have a match.
+         * Now do the byte-compare. The constructor has recorded the number of bytes to compare in nrBytes, use that. If
+         * any of the byte we have to compare is different than what we expect, we don't have a match.
          *
-         * If, on the opposite, after this loop, all bytes have been deemed
-         * equal, then the loop variable i will point to the byte right after
-         * that -- which we will need...
+         * If, on the opposite, after this loop, all bytes have been deemed equal, then the loop variable i will point
+         * to the byte right after that -- which we will need...
          */
         int i = 0;
         for (; i < nrBytes; i++) {
@@ -274,24 +258,20 @@ public final class NetMask {
         }
 
         /*
-         * ... if there are bits left to test. There aren't any if lastByteShift
-         * is set to 0.
+         * ... if there are bits left to test. There aren't any if lastByteShift is set to 0.
          */
         if (lastByteShift == 0) {
             return true;
         }
 
         /*
-         * If it is not 0, however, we must test for the relevant bits in the
-         * next byte (whatever is in the bytes after that doesn't matter). We do
-         * it this way (remember that lastByteShift contains the amount of bits
-         * we should _right_ shift the last byte):
+         * If it is not 0, however, we must test for the relevant bits in the next byte (whatever is in the bytes after
+         * that doesn't matter). We do it this way (remember that lastByteShift contains the amount of bits we should
+         * _right_ shift the last byte):
          *
-         * - grab both bytes at index i, both from the netmask address and the
-         * candidate address; - xor them both.
+         * - grab both bytes at index i, both from the netmask address and the candidate address; - xor them both.
          *
-         * After the xor, it means that all the remaining bits of the CIDR
-         * should be set to 0...
+         * After the xor, it means that all the remaining bits of the CIDR should be set to 0...
          */
         final int lastByte = netaddr[i] ^ candidate[i];
 
@@ -309,12 +289,15 @@ public final class NetMask {
 
     @Override
     public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
         NetMask other = (NetMask) o;
-        return nrBytes == other.nrBytes &&
-            lastByteShift == other.lastByteShift &&
-            Arrays.equals(netaddr, other.netaddr);
+        return nrBytes == other.nrBytes && lastByteShift == other.lastByteShift &&
+                Arrays.equals(netaddr, other.netaddr);
     }
 
     @Override
diff --git a/java/org/apache/catalina/util/NetMaskSet.java b/java/org/apache/catalina/util/NetMaskSet.java
index 4e6b268b8e..8aa330743c 100644
--- a/java/org/apache/catalina/util/NetMaskSet.java
+++ b/java/org/apache/catalina/util/NetMaskSet.java
@@ -27,19 +27,19 @@ import java.util.Set;
 
 
 /**
- * This class maintains a Set of NetMask objects and allows to check if
- * a given IP address is matched by any of the NetMasks, making it easy
- * to create Allow and Deny lists of CIDR networks and hosts.
+ * This class maintains a Set of NetMask objects and allows to check if a given IP address is matched by any of the
+ * NetMasks, making it easy to create Allow and Deny lists of CIDR networks and hosts.
  */
 public class NetMaskSet {
 
     private final Set<NetMask> netmasks = new HashSet<>();
 
     /**
-     * returns true if the passed inetAddress is matched by any of the {@link NetMask}s in the set
+     * Tests if the provided InetAddress matches any of the {@link NetMask}s in the set.
      *
      * @param inetAddress An InetAddress to check
-     * @return
+     *
+     * @return {@code true} if the passed inetAddress is matched by any of the {@link NetMask}s in the set
      */
     public boolean contains(InetAddress inetAddress) {
 
@@ -53,10 +53,11 @@ public class NetMaskSet {
     }
 
     /**
-     * returns true if the passed inetAddress is matched by any of the {@link NetMask}s in the set
+     * Tests if the provided IP address matches any of the {@link NetMask}s in the set.
      *
      * @param ipAddress an IP address to check
-     * @return
+     *
+     * @return {@code true} if the passed IP address is matched by any of the {@link NetMask}s in the set
      *
      * @throws UnknownHostException if the passed input is not a valid IP address
      */
@@ -67,9 +68,10 @@ public class NetMaskSet {
     }
 
     /**
-     * adds a NetMask object to the set if the set does not contain it
+     * Adds a NetMask object to the set if the set does not contain it
+     *
+     * @param netmask The NetMask to add
      *
-     * @param netmask
      * @return true if the object was added
      */
     public boolean add(NetMask netmask) {
@@ -77,11 +79,13 @@ public class NetMaskSet {
     }
 
     /**
-     * creates a NetMask object from the input string and adds it to the set.
-     * throws UnknownHostException if the input is not a valid CIDR format.
+     * Creates a NetMask object from the input string and adds it to the set.
+     *
+     * @param input The string from which to construct the NetMask
      *
-     * @param input
      * @return true if the object was added
+     *
+     * @throws IllegalArgumentException if the input is not a valid CIDR format.
      */
     public boolean add(String input) {
         NetMask netmask = new NetMask(input);
@@ -96,9 +100,9 @@ public class NetMaskSet {
     }
 
     /**
-     * returns true if the set is empty
+     * Tests if the set is empty.
      *
-     * @return
+     * @return {@code true} if the set is empty, otherwise {@code false}
      */
     public boolean isEmpty() {
         return netmasks.isEmpty();
@@ -108,7 +112,8 @@ public class NetMaskSet {
      * Adds a {@link NetMask} list from a string input containing a comma-separated list of (hopefully valid)
      * {@link NetMask}s.
      *
-     * @param input  The input string
+     * @param input The input string
+     *
      * @return a list of processing error messages (empty when no errors)
      */
     public List<String> addAll(String input) {
@@ -131,9 +136,9 @@ public class NetMaskSet {
     }
 
     /**
-     * returns a comma separated list of the <code>NetMask</code>s in this set
+     * Provides a string representation of this NetMaskSet. The format of the String is not guaranteed to remain fixed.
      *
-     * @return
+     * @return a comma separated list of the <code>NetMask</code>s in this set
      */
     @Override
     public String toString() {
@@ -141,7 +146,7 @@ public class NetMaskSet {
         String result = netmasks.toString();
 
         // remove the open and close brackets
-        return result.substring(1, result.length() -1);
+        return result.substring(1, result.length() - 1);
     }
 
 }
diff --git a/test/org/apache/catalina/util/TestNetMask.java b/test/org/apache/catalina/util/TestNetMask.java
index 423dae6a19..9ba37177d6 100644
--- a/test/org/apache/catalina/util/TestNetMask.java
+++ b/test/org/apache/catalina/util/TestNetMask.java
@@ -45,7 +45,7 @@ public final class TestNetMask {
     public Boolean matches;
 
 
-    @Parameters(name="{index}: mask [{0}], input [{1}]")
+    @Parameters(name = "{index}: mask [{0}], input [{1}]")
     public static Collection<Object[]> inputs() {
         List<Object[]> result = new ArrayList<>();
 
@@ -130,8 +130,7 @@ public final class TestNetMask {
             Assert.assertNotNull(netMask);
         } else {
             Assert.assertNotNull(exception);
-            Assert.assertEquals(IllegalArgumentException.class.getName(),
-                    exception.getClass().getName());
+            Assert.assertEquals(IllegalArgumentException.class.getName(), exception.getClass().getName());
             return;
         }
 
diff --git a/test/org/apache/catalina/util/TestNetMaskSet.java b/test/org/apache/catalina/util/TestNetMaskSet.java
index 1c32f43458..513f417d03 100644
--- a/test/org/apache/catalina/util/TestNetMaskSet.java
+++ b/test/org/apache/catalina/util/TestNetMaskSet.java
@@ -17,11 +17,11 @@
 
 package org.apache.catalina.util;
 
+import java.net.UnknownHostException;
+
 import org.junit.Assert;
 import org.junit.Test;
 
-import java.net.UnknownHostException;
-
 public class TestNetMaskSet {
 
     @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org