JDK-8282697 : Add constructors take a cause to InvalidObjectException and InvalidClassException
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.io
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 19
  • Submitted: 2022-03-05
  • Updated: 2022-03-05
  • Resolved: 2022-03-05
Related Reports
CSR :  
Description
Summary
-------

Add cause-taking constructors to  `InvalidObjectException` and `InvalidClassException`, along with their superclass `ObjectStreamException`.

Problem
-------

The exception classes in question repeatedly have their `initCause` methods called. That functionality is better achieved passing a cause into the constructor.

Solution
--------
Add the overloaded constructors in question.

Specification
-------------

-

    -- a/src/java.base/share/classes/java/io/InvalidClassException.java
    +++ b/src/java.base/share/classes/java/io/InvalidClassException.java
    @@ -73,6 +73,30 @@ public class InvalidClassException extends ObjectStreamException {
             classname = cname;
         }
     
    +    /**
    +     * Report an InvalidClassException for the reason and cause specified.
    +     *
    +     * @param reason  String describing the reason for the exception.
    +     * @param cause the cause
    +     * @since 19
    +     */
    +    public InvalidClassException(String reason, Throwable cause) {
    +        super(reason, cause);
    +    }
    +
    +    /**
    +     * Report an InvalidClassException for the reason and cause specified.
    +     *
    +     * @param cname   a String naming the invalid class.
    +     * @param reason  String describing the reason for the exception.
    +     * @param cause the cause
    +     * @since 19
    +     */
    +    public InvalidClassException(String cname, String reason, Throwable cause) {
    +        super(reason, cause);
    +        classname = cname;
    +    }
    +
         /**
          * Produce the message and include the classname, if present.
          */
    
    --- a/src/java.base/share/classes/java/io/InvalidObjectException.java
    +++ b/src/java.base/share/classes/java/io/InvalidObjectException.java
    @@ -48,4 +48,18 @@ public class InvalidObjectException extends ObjectStreamException {
         public  InvalidObjectException(String reason) {
             super(reason);
         }
    +
    +    /**
    +     * Constructs an {@code InvalidObjectException} with the given
    +     * reason and cause.
    +     *
    +     * @param reason Detailed message explaining the reason for the failure.
    +     * @param cause the cause
    +     *
    +     * @see ObjectInputValidation
    +     * @since 19
    +     */
    +    public  InvalidObjectException(String reason, Throwable cause) {
    +        super(reason, cause);
    +    }
     }
    
    --- a/src/java.base/share/classes/java/io/ObjectStreamException.java
    +++ b/src/java.base/share/classes/java/io/ObjectStreamException.java
    @@ -44,10 +44,32 @@ public abstract class ObjectStreamException extends IOException {
             super(message);
         }
     
    +    /**
    +     * Create an ObjectStreamException with the specified message and
    +     * cause.
    +     *
    +     * @param message the detailed message for the exception
    +     * @param cause the cause
    +     * @since 19
    +     */
    +    protected ObjectStreamException(String message, Throwable cause) {
    +        super(message, cause);
    +    }
    +
         /**
          * Create an ObjectStreamException.
          */
         protected ObjectStreamException() {
             super();
         }
    +
    +    /**
    +     * Create an ObjectStreamException with the specified cause.
    +     *
    +     * @param cause the cause
    +     * @since 19
    +     */
    +    protected ObjectStreamException(Throwable cause) {
    +        super(cause);
    +    }
     }


Comments
Moving to Approved.
05-03-2022

The implementation of InvalidObjectException(String reason, Throwable cause) is wrong but the doesn't need to be in the CSR anyway.
05-03-2022