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

Add constructors (String,Throwable) and (Throwable) to InvalidParameterException

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P4
    • 20
    • security-libs
    • None
    • source
    • minimal
    • The added constructors will do no harm regarding compatibility, as long as no very special reflection coding is used that relies on exactly the old interface of java.security.InvalidParameterException .
    • Java API

    Description

      Summary

      Constructors (String msg,Throwable cause) and (Throwable cause) will be added to the class java.security.InvalidParameterException, making creation of objects with cause easier.

      Problem

      Currently setting the cause of InvalidParameterException is possible but leads to clumsy code like this :

      InvalidParameterException ipe = new InvalidParameterException("Error configuring SunPKCS11 provider");
      throw (InvalidParameterException) ipe.initCause(pae.getException());

      We can improve this to one constructor call (a lot of other exception classes have this already).

      Solution

      We add 2 constructors to InvalidParameterException, one taking String msg and Throwable cause parameters, another taking a Throwable cause parameter.

      Specification

      Here is a short diff showing the added constructors and one usage (there are more usages in the jdk codebase).

      diff --git a/src/java.base/share/classes/java/security/InvalidParameterException.java b/src/java.base/share/classes/java/security/InvalidParameterException.java
      index d2e7a4ff8b1..6737f525e22 100644
      --- a/src/java.base/share/classes/java/security/InvalidParameterException.java
      +++ b/src/java.base/share/classes/java/security/InvalidParameterException.java
      @@ -58,4 +58,16 @@ public class InvalidParameterException extends IllegalArgumentException {
           public InvalidParameterException(String msg) {
               super(msg);
           }
      +
      + /**
      +  * Constructs an {@code InvalidParameterException} with the
      +  * specified detail message and cause. A detail message is a {@code String} that describes
      +  * this particular exception.
      +  *
      +  * <p>Note that the detail message associated with {@code cause} is
      +  * <i>not</i> automatically incorporated in this exception's detail
      +  * message.
      +  *
      +  * @param  msg the detail message (which is saved for later retrieval
      +  *         by the {@link Throwable#getMessage()} method).
      +  * @param  cause the cause (which is saved for later retrieval by the
      +  *         {@link Throwable#getCause()} method).  (A {@code null} value
      +  *         is permitted, and indicates that the cause is nonexistent or
      +  *         unknown.)
      +  *
      + * @since 20
      + */
      +    public InvalidParameterException(String msg, Throwable cause) {
      +        super(msg, cause);
      +    }
      +
      + /**
      + * Constructs an {@code InvalidParameterException} with the specified cause and a detail
      + * message of {@code (cause==null ? null : cause.toString())} (which
      + * typically contains the class and detail message of {@code cause}).
      + * This constructor is useful for exceptions that are little more than
      + * wrappers for other throwables.
      + *
      + * @param  cause the cause (which is saved for later retrieval by the
      + *         {@link Throwable#getCause()} method).  (A {@code null} value is
      + *         permitted, and indicates that the cause is nonexistent or
      + *         unknown.)
      + *
      + * @since  20
      + */
      +    public InvalidParameterException(Throwable cause) {
      +        super(cause);
      +    }
       }
      
          diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyGenerator.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyGenerator.java
          index 5bc199b55ad..c298d0b526f 100644
          --- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyGenerator.java
          +++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyGenerator.java
          @@ -335,8 +335,7 @@ final class P11KeyGenerator extends KeyGeneratorSpi {
                   try {
                       newSignificantKeySize = checkKeySize(mechanism, keySize, range);
                   } catch (InvalidAlgorithmParameterException iape) {
          -            throw (InvalidParameterException)
          -                    (new InvalidParameterException().initCause(iape));
          +            throw new InvalidParameterException("Invalid algorithm parameter", iape);
                   }

      Attachments

        Issue Links

          Activity

            People

              mbaesken Matthias Baesken
              mbaesken Matthias Baesken
              Martin Doerr, Sean Mullan
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: