Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8250245

Address reliance on default constructors in java.net

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 16
    • core-libs
    • None
    • behavioral
    • minimal
    • One accidental constructor deprecated.
    • Java API
    • SE

      Summary

      Add explicit constructors to replace default constructors in the java.net package.

      Problem

      Default constructors are not recommend for official APIs.

      Solution

      Add explicit constructors to the classes in question.

      Specification

      diff -r 8a791122cd5e src/java.base/share/classes/java/net/Authenticator.java
      --- a/src/java.base/share/classes/java/net/Authenticator.java   Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/Authenticator.java   Thu Jul 23 17:15:14 2020 -0700
      @@ -75,6 +75,11 @@
           private final String key = AuthenticatorKeys.computeKey(this);
      
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public Authenticator() {}
      +
      +    /**
            * The type of the entity requesting authentication.
            *
            * @since 1.5
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/CacheRequest.java
      --- a/src/java.base/share/classes/java/net/CacheRequest.java    Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/CacheRequest.java    Thu Jul 23 17:15:14 2020 -0700
      @@ -44,6 +44,11 @@
       public abstract class CacheRequest {
      
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public CacheRequest() {}
      +
      +    /**
            * Returns an OutputStream to which the response body can be
            * written.
            *
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/CacheResponse.java
      --- a/src/java.base/share/classes/java/net/CacheResponse.java   Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/CacheResponse.java   Thu Jul 23 17:15:14 2020 -0700
      @@ -42,6 +42,11 @@
       public abstract class CacheResponse {
      
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public CacheResponse() {}
      +
      +    /**
            * Returns the response headers as a Map.
            *
            * @return An immutable Map from response header field names to
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/ContentHandler.java
      --- a/src/java.base/share/classes/java/net/ContentHandler.java  Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/ContentHandler.java  Thu Jul 23 17:15:14 2020 -0700
      @@ -84,6 +84,11 @@
       public abstract class ContentHandler {
      
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public ContentHandler() {}
      +
      +    /**
            * Given a URL connect stream positioned at the beginning of the
            * representation of an object, this method reads that stream and
            * creates an object from it.
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/CookieHandler.java
      --- a/src/java.base/share/classes/java/net/CookieHandler.java   Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/CookieHandler.java   Thu Jul 23 17:15:14 2020 -0700
      @@ -51,6 +51,11 @@
        */
       public abstract class CookieHandler {
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public CookieHandler() {}
      +
      +    /**
            * The system-wide cookie handler that will apply cookies to the
            * request headers and manage cookies from the response headers.
            *
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/DatagramSocketImpl.java
      --- a/src/java.base/share/classes/java/net/DatagramSocketImpl.java  Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/DatagramSocketImpl.java  Thu Jul 23 17:15:14 2020 -0700
      @@ -58,6 +58,11 @@
       public abstract class DatagramSocketImpl implements SocketOptions {
      
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public DatagramSocketImpl() {}
      +
      +    /**
            * The local port number.
            */
           protected int localPort;
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/ProxySelector.java
      --- a/src/java.base/share/classes/java/net/ProxySelector.java   Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/ProxySelector.java   Thu Jul 23 17:15:14 2020 -0700
      @@ -81,6 +81,11 @@
           }
      
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public ProxySelector() {}
      +
      +    /**
            * Gets the system-wide proxy selector.
            *
            * @throws  SecurityException
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/ResponseCache.java
      --- a/src/java.base/share/classes/java/net/ResponseCache.java   Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/ResponseCache.java   Thu Jul 23 17:15:14 2020 -0700
      @@ -61,6 +61,11 @@
       public abstract class ResponseCache {
      
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public ResponseCache() {}
      +
      +    /**
            * The system wide cache that provides access to a url
            * caching mechanism.
            *
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/SecureCacheResponse.java
      --- a/src/java.base/share/classes/java/net/SecureCacheResponse.java Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/SecureCacheResponse.java Thu Jul 23 17:15:14 2020 -0700
      @@ -40,6 +40,11 @@
        */
       public abstract class SecureCacheResponse extends CacheResponse {
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public SecureCacheResponse() {}
      +
      +    /**
            * Returns the cipher suite in use on the original connection that
            * retrieved the network resource.
            *
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/SocketAddress.java
      --- a/src/java.base/share/classes/java/net/SocketAddress.java   Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/SocketAddress.java   Thu Jul 23 17:15:14 2020 -0700
      @@ -43,4 +43,8 @@
           @java.io.Serial
           static final long serialVersionUID = 5215720748342549866L;
      
      +    /**
      +     * Constructor for subclasses to call.
      +     */
      +    public SocketAddress() {}
       }
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/URLDecoder.java
      --- a/src/java.base/share/classes/java/net/URLDecoder.java  Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/URLDecoder.java  Thu Jul 23 17:15:14 2020 -0700
      @@ -81,6 +81,12 @@
      
       public class URLDecoder {
      
      +    /**
      +     * Do not call.
      +     */
      +    @Deprecated(since="16", forRemoval=true)
      +    public URLDecoder() {}
      +
           // The platform default encoding
           static String dfltEncName = URLEncoder.dfltEncName;
      
      diff -r 8a791122cd5e src/java.base/share/classes/java/net/URLStreamHandler.java
      --- a/src/java.base/share/classes/java/net/URLStreamHandler.java    Thu Jul 23 15:13:08 2020 -0700
      +++ b/src/java.base/share/classes/java/net/URLStreamHandler.java    Thu Jul 23 17:15:14 2020 -0700
      @@ -52,6 +52,11 @@
        */
       public abstract class URLStreamHandler {
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public URLStreamHandler() {}
      +
      +    /**
            * Opens a connection to the object referenced by the
            * {@code URL} argument.
            * This method should be overridden by a subclass.

            darcy Joe Darcy
            darcy Joe Darcy
            Alan Bateman
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: