8274079: Cleanup unnecessary calls to Throwable.initCause() in java.base module

Reviewed-by: weijun
This commit is contained in:
Andrey Turbanov 2021-10-05 13:36:37 +00:00 committed by Weijun Wang
parent 8609ea55ac
commit 1459180f35
22 changed files with 55 additions and 132 deletions

View File

@ -72,9 +72,7 @@ abstract class AESCipher extends CipherSpi {
engineSetPadding(padding); engineSetPadding(padding);
} catch (GeneralSecurityException gse) { } catch (GeneralSecurityException gse) {
// internal error; re-throw as provider exception // internal error; re-throw as provider exception
ProviderException pe =new ProviderException("Internal Error"); throw new ProviderException("Internal Error", gse);
pe.initCause(gse);
throw pe;
} }
} }
} }

View File

@ -76,16 +76,10 @@ final class ConstructKeys {
encodedKeyAlgorithm + encodedKeyAlgorithm +
"algorithm"); "algorithm");
} catch (InvalidKeySpecException ikse2) { } catch (InvalidKeySpecException ikse2) {
InvalidKeyException ike = throw new InvalidKeyException("Cannot construct public key", ikse2);
new InvalidKeyException("Cannot construct public key");
ike.initCause(ikse2);
throw ike;
} }
} catch (InvalidKeySpecException ikse) { } catch (InvalidKeySpecException ikse) {
InvalidKeyException ike = throw new InvalidKeyException("Cannot construct public key", ikse);
new InvalidKeyException("Cannot construct public key");
ike.initCause(ikse);
throw ike;
} }
return key; return key;
@ -116,16 +110,10 @@ final class ConstructKeys {
encodedKeyAlgorithm + encodedKeyAlgorithm +
"algorithm"); "algorithm");
} catch (InvalidKeySpecException ikse2) { } catch (InvalidKeySpecException ikse2) {
InvalidKeyException ike = throw new InvalidKeyException("Cannot construct private key", ikse2);
new InvalidKeyException("Cannot construct private key");
ike.initCause(ikse2);
throw ike;
} }
} catch (InvalidKeySpecException ikse) { } catch (InvalidKeySpecException ikse) {
InvalidKeyException ike = throw new InvalidKeyException("Cannot construct private key", ikse);
new InvalidKeyException("Cannot construct private key");
ike.initCause(ikse);
throw ike;
} finally { } finally {
SharedSecrets.getJavaSecuritySpecAccess().clearEncodedKeySpec(keySpec); SharedSecrets.getJavaSecuritySpecAccess().clearEncodedKeySpec(keySpec);
if (keyBytes != encodedKey) { if (keyBytes != encodedKey) {

View File

@ -181,10 +181,7 @@ public final class DESedeWrapCipher extends CipherSpi {
engineInit(opmode, key, (AlgorithmParameterSpec) null, random); engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
} catch (InvalidAlgorithmParameterException iape) { } catch (InvalidAlgorithmParameterException iape) {
// should never happen // should never happen
InvalidKeyException ike = throw new InvalidKeyException("Parameters required", iape);
new InvalidKeyException("Parameters required");
ike.initCause(iape);
throw ike;
} }
} }
@ -285,11 +282,8 @@ public final class DESedeWrapCipher extends CipherSpi {
paramsEng.engineInit(params.getEncoded()); paramsEng.engineInit(params.getEncoded());
ivSpec = paramsEng.engineGetParameterSpec(IvParameterSpec.class); ivSpec = paramsEng.engineGetParameterSpec(IvParameterSpec.class);
} catch (Exception ex) { } catch (Exception ex) {
InvalidAlgorithmParameterException iape = throw new InvalidAlgorithmParameterException
new InvalidAlgorithmParameterException ("Wrong parameter type: IV expected", ex);
("Wrong parameter type: IV expected");
iape.initCause(ex);
throw iape;
} }
} }
engineInit(opmode, key, ivSpec, random); engineInit(opmode, key, ivSpec, random);

View File

@ -291,10 +291,8 @@ final class DHPrivateKey implements PrivateKey,
DerInputStream in = new DerInputStream(this.key); DerInputStream in = new DerInputStream(this.key);
this.x = in.getBigInteger(); this.x = in.getBigInteger();
} catch (IOException e) { } catch (IOException e) {
InvalidKeyException ike = new InvalidKeyException( throw new InvalidKeyException(
"Error parsing key encoding: " + e.getMessage()); "Error parsing key encoding: " + e.getMessage(), e);
ike.initCause(e);
throw ike;
} }
} }

View File

@ -162,10 +162,7 @@ abstract class PBES2Core extends CipherSpi {
try { try {
engineInit(opmode, key, (AlgorithmParameterSpec) null, random); engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
} catch (InvalidAlgorithmParameterException ie) { } catch (InvalidAlgorithmParameterException ie) {
InvalidKeyException ike = throw new InvalidKeyException("requires PBE parameters", ie);
new InvalidKeyException("requires PBE parameters");
ike.initCause(ie);
throw ike;
} }
} }
@ -279,10 +276,7 @@ abstract class PBES2Core extends CipherSpi {
try { try {
s = (PBKDF2KeyImpl)kdf.engineGenerateSecret(pbeSpec); s = (PBKDF2KeyImpl)kdf.engineGenerateSecret(pbeSpec);
} catch (InvalidKeySpecException ikse) { } catch (InvalidKeySpecException ikse) {
InvalidKeyException ike = throw new InvalidKeyException("Cannot construct PBE key", ikse);
new InvalidKeyException("Cannot construct PBE key");
ike.initCause(ikse);
throw ike;
} finally { } finally {
pbeSpec.clearPassword(); pbeSpec.clearPassword();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,7 +28,6 @@ package com.sun.crypto.provider;
import java.security.*; import java.security.*;
import java.security.spec.*; import java.security.spec.*;
import javax.crypto.*; import javax.crypto.*;
import javax.crypto.spec.*;
/** /**
* This class represents password-based encryption as defined by the PKCS #5 * This class represents password-based encryption as defined by the PKCS #5
@ -183,10 +182,7 @@ public final class PBEWithMD5AndDESCipher extends CipherSpi {
try { try {
engineInit(opmode, key, (AlgorithmParameterSpec) null, random); engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
} catch (InvalidAlgorithmParameterException ie) { } catch (InvalidAlgorithmParameterException ie) {
InvalidKeyException ike = throw new InvalidKeyException("requires PBE parameters", ie);
new InvalidKeyException("requires PBE parameters");
ike.initCause(ie);
throw ike;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,7 +28,6 @@ package com.sun.crypto.provider;
import java.security.*; import java.security.*;
import java.security.spec.*; import java.security.spec.*;
import javax.crypto.*; import javax.crypto.*;
import javax.crypto.spec.*;
/** /**
* This class implements a proprietary password-based encryption algorithm. * This class implements a proprietary password-based encryption algorithm.
@ -195,10 +194,7 @@ public final class PBEWithMD5AndTripleDESCipher extends CipherSpi {
try { try {
core.init(opmode, key, (AlgorithmParameterSpec) null, random); core.init(opmode, key, (AlgorithmParameterSpec) null, random);
} catch (InvalidAlgorithmParameterException ie) { } catch (InvalidAlgorithmParameterException ie) {
InvalidKeyException ike = throw new InvalidKeyException("requires PBE parameters", ie);
new InvalidKeyException("requires PBE parameters");
ike.initCause(ie);
throw ike;
} }
} }

View File

@ -151,10 +151,8 @@ abstract class PBKDF2Core extends SecretKeyFactorySpi {
try { try {
return new PBKDF2KeyImpl(spec, prfAlgo); return new PBKDF2KeyImpl(spec, prfAlgo);
} catch (InvalidKeySpecException re) { } catch (InvalidKeySpecException re) {
InvalidKeyException ike = new InvalidKeyException throw new InvalidKeyException
("Invalid key component(s)"); ("Invalid key component(s)", re);
ike.initCause(re);
throw ike;
} finally { } finally {
if (password != null) { if (password != null) {
Arrays.fill(password, (char) 0); Arrays.fill(password, (char) 0);

View File

@ -151,10 +151,8 @@ public final class PBKDF2HmacSHA1Factory extends SecretKeyFactorySpi {
try { try {
return new PBKDF2KeyImpl(spec, "HmacSHA1"); return new PBKDF2KeyImpl(spec, "HmacSHA1");
} catch (InvalidKeySpecException re) { } catch (InvalidKeySpecException re) {
InvalidKeyException ike = new InvalidKeyException throw new InvalidKeyException
("Invalid key component(s)"); ("Invalid key component(s)", re);
ike.initCause(re);
throw ike;
} finally { } finally {
if (password != null) { if (password != null) {
Arrays.fill(password, (char) 0); Arrays.fill(password, (char) 0);

View File

@ -121,9 +121,7 @@ final class PBKDF2KeyImpl implements javax.crypto.interfaces.PBEKey {
this.key = deriveKey(prf, passwdBytes, salt, iterCount, keyLength); this.key = deriveKey(prf, passwdBytes, salt, iterCount, keyLength);
} catch (NoSuchAlgorithmException nsae) { } catch (NoSuchAlgorithmException nsae) {
// not gonna happen; re-throw just in case // not gonna happen; re-throw just in case
InvalidKeySpecException ike = new InvalidKeySpecException(); throw new InvalidKeySpecException(nsae);
ike.initCause(nsae);
throw ike;
} finally { } finally {
Arrays.fill(passwdBytes, (byte) 0x00); Arrays.fill(passwdBytes, (byte) 0x00);

View File

@ -26,9 +26,7 @@
package com.sun.crypto.provider; package com.sun.crypto.provider;
import java.util.Arrays; import java.util.Arrays;
import java.nio.ByteBuffer;
import javax.crypto.MacSpi;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEKeySpec;
@ -181,10 +179,7 @@ abstract class PBMAC1Core extends HmacCore {
s = (PBKDF2KeyImpl)kdf.engineGenerateSecret(pbeSpec); s = (PBKDF2KeyImpl)kdf.engineGenerateSecret(pbeSpec);
derivedKey = s.getEncoded(); derivedKey = s.getEncoded();
} catch (InvalidKeySpecException ikse) { } catch (InvalidKeySpecException ikse) {
InvalidKeyException ike = throw new InvalidKeyException("Cannot construct PBE key", ikse);
new InvalidKeyException("Cannot construct PBE key");
ike.initCause(ikse);
throw ike;
} finally { } finally {
pbeSpec.clearPassword(); pbeSpec.clearPassword();
if (s != null) { if (s != null) {

View File

@ -211,10 +211,7 @@ public final class RSACipher extends CipherSpi {
} catch (InvalidAlgorithmParameterException iape) { } catch (InvalidAlgorithmParameterException iape) {
// never thrown when null parameters are used; // never thrown when null parameters are used;
// but re-throw it just in case // but re-throw it just in case
InvalidKeyException ike = throw new InvalidKeyException("Wrong parameters", iape);
new InvalidKeyException("Wrong parameters");
ike.initCause(iape);
throw ike;
} }
} }
@ -237,10 +234,7 @@ public final class RSACipher extends CipherSpi {
params.getParameterSpec(OAEPParameterSpec.class); params.getParameterSpec(OAEPParameterSpec.class);
init(opmode, key, random, spec); init(opmode, key, random, spec);
} catch (InvalidParameterSpecException ipse) { } catch (InvalidParameterSpecException ipse) {
InvalidAlgorithmParameterException iape = throw new InvalidAlgorithmParameterException("Wrong parameter", ipse);
new InvalidAlgorithmParameterException("Wrong parameter");
iape.initCause(ipse);
throw iape;
} }
} }
} }

View File

@ -1712,9 +1712,7 @@ public class ObjectStreamClass implements Serializable {
} else if (th instanceof Error) { } else if (th instanceof Error) {
throw (Error) th; throw (Error) th;
} else { } else {
IOException ex = new IOException("unexpected exception type"); throw new IOException("unexpected exception type", th);
ex.initCause(th);
throw ex;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -32,7 +32,6 @@ import javax.security.auth.x500.X500Principal;
import sun.security.util.AnchorCertificates; import sun.security.util.AnchorCertificates;
import sun.security.x509.NameConstraintsExtension; import sun.security.x509.NameConstraintsExtension;
import sun.security.x509.X500Name;
/** /**
* A trust anchor or most-trusted Certification Authority (CA). * A trust anchor or most-trusted Certification Authority (CA).
@ -286,10 +285,7 @@ public class TrustAnchor {
try { try {
nc = new NameConstraintsExtension(Boolean.FALSE, bytes); nc = new NameConstraintsExtension(Boolean.FALSE, bytes);
} catch (IOException ioe) { } catch (IOException ioe) {
IllegalArgumentException iae = throw new IllegalArgumentException(ioe.getMessage(), ioe);
new IllegalArgumentException(ioe.getMessage());
iae.initCause(ioe);
throw iae;
} }
} }
} }

View File

@ -371,7 +371,7 @@ public class X509CRLSelector implements CRLSelector {
try { try {
x500Principals.add(new X500Principal((byte[])nameObject)); x500Principals.add(new X500Principal((byte[])nameObject));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw (IOException)new IOException("Invalid name").initCause(e); throw new IOException("Invalid name", e);
} }
} }
} }

View File

@ -411,7 +411,7 @@ public final class Duration
try { try {
return create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos); return create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos);
} catch (ArithmeticException ex) { } catch (ArithmeticException ex) {
throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: overflow", text, 0).initCause(ex); throw new DateTimeParseException("Text cannot be parsed to a Duration: overflow", text, 0, ex);
} }
} }
} }
@ -432,7 +432,7 @@ public final class Duration
long val = Long.parseLong(text, start, end, 10); long val = Long.parseLong(text, start, end, 10);
return Math.multiplyExact(val, multiplier); return Math.multiplyExact(val, multiplier);
} catch (NumberFormatException | ArithmeticException ex) { } catch (NumberFormatException | ArithmeticException ex) {
throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: " + errorText, text, 0).initCause(ex); throw new DateTimeParseException("Text cannot be parsed to a Duration: " + errorText, text, 0, ex);
} }
} }
@ -451,7 +451,7 @@ public final class Duration
} }
return fraction * negate; return fraction * negate;
} catch (NumberFormatException | ArithmeticException ex) { } catch (NumberFormatException | ArithmeticException ex) {
throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: fraction", text, 0).initCause(ex); throw new DateTimeParseException("Text cannot be parsed to a Duration: fraction", text, 0, ex);
} }
} }

View File

@ -372,12 +372,9 @@ public class SSLContext {
try { try {
return contextSpi.engineCreateSSLEngine(); return contextSpi.engineCreateSSLEngine();
} catch (AbstractMethodError e) { } catch (AbstractMethodError e) {
UnsupportedOperationException unsup = throw new UnsupportedOperationException(
new UnsupportedOperationException( "Provider: " + getProvider() +
"Provider: " + getProvider() + " doesn't support this operation", e);
" doesn't support this operation");
unsup.initCause(e);
throw unsup;
} }
} }
@ -412,12 +409,9 @@ public class SSLContext {
try { try {
return contextSpi.engineCreateSSLEngine(peerHost, peerPort); return contextSpi.engineCreateSSLEngine(peerHost, peerPort);
} catch (AbstractMethodError e) { } catch (AbstractMethodError e) {
UnsupportedOperationException unsup = throw new UnsupportedOperationException(
new UnsupportedOperationException( "Provider: " + getProvider() +
"Provider: " + getProvider() + " does not support this operation", e);
" does not support this operation");
unsup.initCause(e);
throw unsup;
} }
} }

View File

@ -270,17 +270,15 @@ public abstract class Configuration {
} catch (PrivilegedActionException e) { } catch (PrivilegedActionException e) {
Exception ee = e.getException(); Exception ee = e.getException();
if (ee instanceof InstantiationException) { if (ee instanceof InstantiationException) {
throw (SecurityException) new throw new SecurityException
SecurityException
("Configuration error:" + ("Configuration error:" +
ee.getCause().getMessage() + ee.getCause().getMessage() +
"\n").initCause(ee.getCause()); "\n", ee.getCause());
} else { } else {
throw (SecurityException) new throw new SecurityException
SecurityException
("Configuration error: " + ("Configuration error: " +
ee.toString() + ee.toString() +
"\n").initCause(ee); "\n", ee);
} }
} }
} }

View File

@ -181,10 +181,8 @@ public final class X500Principal implements Principal, java.io.Serializable {
try { try {
thisX500Name = new X500Name(name, keywordMap); thisX500Name = new X500Name(name, keywordMap);
} catch (Exception e) { } catch (Exception e) {
IllegalArgumentException iae = new IllegalArgumentException throw new IllegalArgumentException
("improperly specified input name: " + name); ("improperly specified input name: " + name, e);
iae.initCause(e);
throw iae;
} }
} }
@ -226,10 +224,8 @@ public final class X500Principal implements Principal, java.io.Serializable {
try { try {
thisX500Name = new X500Name(name); thisX500Name = new X500Name(name);
} catch (Exception e) { } catch (Exception e) {
IllegalArgumentException iae = new IllegalArgumentException throw new IllegalArgumentException
("improperly specified input name"); ("improperly specified input name", e);
iae.initCause(e);
throw iae;
} }
} }
@ -266,17 +262,13 @@ public final class X500Principal implements Principal, java.io.Serializable {
try { try {
is.reset(); is.reset();
} catch (IOException ioe) { } catch (IOException ioe) {
IllegalArgumentException iae = new IllegalArgumentException throw new IllegalArgumentException
("improperly specified input stream " + ("improperly specified input stream " +
("and unable to reset input stream")); ("and unable to reset input stream"), e);
iae.initCause(e);
throw iae;
} }
} }
IllegalArgumentException iae = new IllegalArgumentException throw new IllegalArgumentException
("improperly specified input stream"); ("improperly specified input stream", e);
iae.initCause(e);
throw iae;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -184,8 +184,7 @@ abstract class DigestBase extends MessageDigestSpi implements Cloneable {
try { try {
engineDigest(b, 0, b.length); engineDigest(b, 0, b.length);
} catch (DigestException e) { } catch (DigestException e) {
throw (ProviderException) throw new ProviderException("Internal error", e);
new ProviderException("Internal error").initCause(e);
} }
return b; return b;
} }

View File

@ -810,9 +810,9 @@ public abstract class JavaKeyStore extends KeyStoreSpi {
if (!MessageDigest.isEqual(computed, actual)) { if (!MessageDigest.isEqual(computed, actual)) {
Throwable t = new UnrecoverableKeyException Throwable t = new UnrecoverableKeyException
("Password verification failed"); ("Password verification failed");
throw (IOException) new IOException throw new IOException
("Keystore was tampered with, or " ("Keystore was tampered with, or "
+ "password was incorrect").initCause(t); + "password was incorrect", t);
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -254,8 +254,7 @@ public class HostnameChecker {
return new X500Name(subjectX500.getEncoded()); return new X500Name(subjectX500.getEncoded());
} }
} catch (IOException e) { } catch (IOException e) {
throw(CertificateParsingException) throw new CertificateParsingException(e);
new CertificateParsingException().initCause(e);
} }
} }