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

Remove default constructors from java.lang and java.io

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 14
    • core-libs
    • None
    • behavioral
    • minimal
    • Generally add explicit constructors matching the default ones, new deprecations added.
    • Java API
    • SE

      Summary

      Add explicit constructors to the public classes in java.lang and java.io relying on default constructors.

      Problem

      Using default constructors is not a good practice for official types, they allow the creation of accidental constructors and do not provide a location for javadoc.

      Solution

      Add the constructors to the types in question, deprecating for removal constructors that are not useful and seem to have been allowed by accident.

      Specification

      --- old/src/java.base/share/classes/java/io/InputStream.java    2019-09-06 11:11:22.330589001 -0700
      +++ new/src/java.base/share/classes/java/io/InputStream.java    2019-09-06 11:11:22.146589001 -0700
      @@ -56,6 +56,11 @@
           private static final int DEFAULT_BUFFER_SIZE = 8192;
      
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public InputStream() {}
      +
      +    /**
            * Returns a new {@code InputStream} that reads no bytes. The returned
            * stream is initially open.  The stream is closed by calling the
            * {@code close()} method.  Subsequent calls to {@code close()} have no
      --- old/src/java.base/share/classes/java/io/ObjectInputStream.java  2019-09-06 11:11:22.838589001 -0700
      +++ new/src/java.base/share/classes/java/io/ObjectInputStream.java  2019-09-06 11:11:22.646589001 -0700
      @@ -1321,6 +1321,10 @@
            * Provide access to the persistent fields read from the input stream.
            */
           public abstract static class GetField {
      +        /**
      +         * Constructor for subclasses to call.
      +         */
      +        public GetField() {}
      
               /**
                * Get the ObjectStreamClass that describes the fields in the stream.
      --- old/src/java.base/share/classes/java/io/ObjectOutputStream.java 2019-09-06 11:11:23.346589001 -0700
      +++ new/src/java.base/share/classes/java/io/ObjectOutputStream.java 2019-09-06 11:11:23.162589001 -0700
      @@ -879,6 +879,10 @@
            * @since 1.2
            */
           public abstract static class PutField {
      +        /**
      +         * Constructor for subclasses to call.
      +         */
      +        public PutField() {}
      
               /**
                * Put the value of the named boolean field into the persistent field.
      --- old/src/java.base/share/classes/java/io/OutputStream.java   2019-09-06 11:11:23.866589001 -0700
      +++ new/src/java.base/share/classes/java/io/OutputStream.java   2019-09-06 11:11:23.674589001 -0700
      @@ -47,6 +47,11 @@
        */
       public abstract class OutputStream implements Closeable, Flushable {
           /**
      +     * Constructor for subclasses to call.
      +     */
      +    public OutputStream() {}
      +
      +    /**
            * Returns a new {@code OutputStream} which discards all bytes.  The
            * returned stream is initially open.  The stream is closed by calling
            * the {@code close()} method.  Subsequent calls to {@code close()} have
       --- old/src/java.base/share/classes/java/lang/InheritableThreadLocal.java  2019-09-06 11:11:24.394589001 -0700
      +++ new/src/java.base/share/classes/java/lang/InheritableThreadLocal.java   2019-09-06 11:11:24.182589001 -0700
      @@ -52,6 +52,11 @@
      
       public class InheritableThreadLocal<T> extends ThreadLocal<T> {
           /**
      +     * Creates an inheritable thread local variable.
      +     */
      +    public InheritableThreadLocal() {}
      +
      +    /**
            * Computes the child's initial value for this inheritable thread-local
            * variable as a function of the parent's value at the time the child
            * thread is created.  This method is called from within the parent
         --- old/src/java.base/share/classes/java/lang/ThreadDeath.java   2019-09-06 11:11:25.486589001 -0700
          +++ new/src/java.base/share/classes/java/lang/ThreadDeath.java  2019-09-06 11:11:25.234589001 -0700
          @@ -49,4 +49,9 @@
           public class ThreadDeath extends Error {
               @java.io.Serial
               private static final long serialVersionUID = -4417128565033088268L;
          +
          +    /**
          +     * Constructs a {@code ThreadDeath}.
          +     */
          +    public ThreadDeath() {}
           }
          --- old/src/java.base/share/classes/java/lang/invoke/ConstantBootstraps.java    2019-09-06 11:11:26.846589001 -0700
          +++ new/src/java.base/share/classes/java/lang/invoke/ConstantBootstraps.java    2019-09-06 11:11:26.582589001 -0700
          @@ -40,6 +40,12 @@
            * @since 11
            */
           public final class ConstantBootstraps {
          +    /**
          +     * Do not call.
          +     */
          +    @Deprecated(forRemoval=true, since="14")
          +    public ConstantBootstraps() {}
          +
               // implements the upcall from the JVM, MethodHandleNatives.linkDynamicConstant:
               /*non-public*/
               static Object makeConstant(MethodHandle bootstrapMethod,
          --- old/src/java.base/share/classes/java/lang/reflect/Modifier.java 2019-09-06 11:11:27.538589001 -0700
          +++ new/src/java.base/share/classes/java/lang/reflect/Modifier.java 2019-09-06 11:11:27.274589001 -0700
          @@ -43,6 +43,12 @@
            * @since 1.1
            */
           public class Modifier {
          +    /**
          +     * Do not call.
          +     */
          +    @Deprecated(forRemoval=true, since="14")
          +    public Modifier() {}
          +
      
               /**
                * Return {@code true} if the integer argument includes the

            darcy Joe Darcy
            darcy Joe Darcy
            Brian Burkhalter, Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: