Skip to content
Permalink
Browse files
8274079: Cleanup unnecessary calls to Throwable.initCause() in java.b…
…ase module

Reviewed-by: weijun
  • Loading branch information
turbanoff authored and wangweij committed Oct 5, 2021
1 parent 8609ea5 commit 1459180f352a5632c0afca2ed55abf31e4b0bfb0
Showing 22 changed files with 55 additions and 132 deletions.
@@ -72,9 +72,7 @@ protected OidImpl(int keySize, String mode, String padding) {
engineSetPadding(padding);
} catch (GeneralSecurityException gse) {
// internal error; re-throw as provider exception
ProviderException pe =new ProviderException("Internal Error");
pe.initCause(gse);
throw pe;
throw new ProviderException("Internal Error", gse);
}
}
}
@@ -76,16 +76,10 @@ private static final PublicKey constructPublicKey(byte[] encodedKey,
encodedKeyAlgorithm +
"algorithm");
} catch (InvalidKeySpecException ikse2) {
InvalidKeyException ike =
new InvalidKeyException("Cannot construct public key");
ike.initCause(ikse2);
throw ike;
throw new InvalidKeyException("Cannot construct public key", ikse2);
}
} catch (InvalidKeySpecException ikse) {
InvalidKeyException ike =
new InvalidKeyException("Cannot construct public key");
ike.initCause(ikse);
throw ike;
throw new InvalidKeyException("Cannot construct public key", ikse);
}

return key;
@@ -116,16 +110,10 @@ private static final PrivateKey constructPrivateKey(byte[] encodedKey,
encodedKeyAlgorithm +
"algorithm");
} catch (InvalidKeySpecException ikse2) {
InvalidKeyException ike =
new InvalidKeyException("Cannot construct private key");
ike.initCause(ikse2);
throw ike;
throw new InvalidKeyException("Cannot construct private key", ikse2);
}
} catch (InvalidKeySpecException ikse) {
InvalidKeyException ike =
new InvalidKeyException("Cannot construct private key");
ike.initCause(ikse);
throw ike;
throw new InvalidKeyException("Cannot construct private key", ikse);
} finally {
SharedSecrets.getJavaSecuritySpecAccess().clearEncodedKeySpec(keySpec);
if (keyBytes != encodedKey) {
@@ -181,10 +181,7 @@ protected void engineInit(int opmode, Key key, SecureRandom random)
engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
} catch (InvalidAlgorithmParameterException iape) {
// should never happen
InvalidKeyException ike =
new InvalidKeyException("Parameters required");
ike.initCause(iape);
throw ike;
throw new InvalidKeyException("Parameters required", iape);
}
}

@@ -285,11 +282,8 @@ protected void engineInit(int opmode, Key key,
paramsEng.engineInit(params.getEncoded());
ivSpec = paramsEng.engineGetParameterSpec(IvParameterSpec.class);
} catch (Exception ex) {
InvalidAlgorithmParameterException iape =
new InvalidAlgorithmParameterException
("Wrong parameter type: IV expected");
iape.initCause(ex);
throw iape;
throw new InvalidAlgorithmParameterException
("Wrong parameter type: IV expected", ex);
}
}
engineInit(opmode, key, ivSpec, random);
@@ -291,10 +291,8 @@ private void parseKeyBits() throws InvalidKeyException {
DerInputStream in = new DerInputStream(this.key);
this.x = in.getBigInteger();
} catch (IOException e) {
InvalidKeyException ike = new InvalidKeyException(
"Error parsing key encoding: " + e.getMessage());
ike.initCause(e);
throw ike;
throw new InvalidKeyException(
"Error parsing key encoding: " + e.getMessage(), e);
}
}

@@ -162,10 +162,7 @@ protected void engineInit(int opmode, Key key, SecureRandom random)
try {
engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
} catch (InvalidAlgorithmParameterException ie) {
InvalidKeyException ike =
new InvalidKeyException("requires PBE parameters");
ike.initCause(ie);
throw ike;
throw new InvalidKeyException("requires PBE parameters", ie);
}
}

@@ -279,10 +276,7 @@ protected void engineInit(int opmode, Key key,
try {
s = (PBKDF2KeyImpl)kdf.engineGenerateSecret(pbeSpec);
} catch (InvalidKeySpecException ikse) {
InvalidKeyException ike =
new InvalidKeyException("Cannot construct PBE key");
ike.initCause(ikse);
throw ike;
throw new InvalidKeyException("Cannot construct PBE key", ikse);
} finally {
pbeSpec.clearPassword();
}
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -28,7 +28,6 @@
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;

/**
* This class represents password-based encryption as defined by the PKCS #5
@@ -183,10 +182,7 @@ protected void engineInit(int opmode, Key key, SecureRandom random)
try {
engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
} catch (InvalidAlgorithmParameterException ie) {
InvalidKeyException ike =
new InvalidKeyException("requires PBE parameters");
ike.initCause(ie);
throw ike;
throw new InvalidKeyException("requires PBE parameters", ie);
}
}

@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -28,7 +28,6 @@
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;

/**
* This class implements a proprietary password-based encryption algorithm.
@@ -195,10 +194,7 @@ protected void engineInit(int opmode, Key key, SecureRandom random)
try {
core.init(opmode, key, (AlgorithmParameterSpec) null, random);
} catch (InvalidAlgorithmParameterException ie) {
InvalidKeyException ike =
new InvalidKeyException("requires PBE parameters");
ike.initCause(ie);
throw ike;
throw new InvalidKeyException("requires PBE parameters", ie);
}
}

@@ -151,10 +151,8 @@ protected SecretKey engineTranslateKey(SecretKey key)
try {
return new PBKDF2KeyImpl(spec, prfAlgo);
} catch (InvalidKeySpecException re) {
InvalidKeyException ike = new InvalidKeyException
("Invalid key component(s)");
ike.initCause(re);
throw ike;
throw new InvalidKeyException
("Invalid key component(s)", re);
} finally {
if (password != null) {
Arrays.fill(password, (char) 0);
@@ -151,10 +151,8 @@ protected SecretKey engineTranslateKey(SecretKey key)
try {
return new PBKDF2KeyImpl(spec, "HmacSHA1");
} catch (InvalidKeySpecException re) {
InvalidKeyException ike = new InvalidKeyException
("Invalid key component(s)");
ike.initCause(re);
throw ike;
throw new InvalidKeyException
("Invalid key component(s)", re);
} finally {
if (password != null) {
Arrays.fill(password, (char) 0);
@@ -121,9 +121,7 @@ private static byte[] getPasswordBytes(char[] passwd) {
this.key = deriveKey(prf, passwdBytes, salt, iterCount, keyLength);
} catch (NoSuchAlgorithmException nsae) {
// not gonna happen; re-throw just in case
InvalidKeySpecException ike = new InvalidKeySpecException();
ike.initCause(nsae);
throw ike;
throw new InvalidKeySpecException(nsae);
} finally {
Arrays.fill(passwdBytes, (byte) 0x00);

@@ -26,9 +26,7 @@
package com.sun.crypto.provider;

import java.util.Arrays;
import java.nio.ByteBuffer;

import javax.crypto.MacSpi;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.PBEKeySpec;
@@ -181,10 +179,7 @@ protected void engineInit(Key key, AlgorithmParameterSpec params)
s = (PBKDF2KeyImpl)kdf.engineGenerateSecret(pbeSpec);
derivedKey = s.getEncoded();
} catch (InvalidKeySpecException ikse) {
InvalidKeyException ike =
new InvalidKeyException("Cannot construct PBE key");
ike.initCause(ikse);
throw ike;
throw new InvalidKeyException("Cannot construct PBE key", ikse);
} finally {
pbeSpec.clearPassword();
if (s != null) {
@@ -211,10 +211,7 @@ protected void engineInit(int opmode, Key key, SecureRandom random)
} catch (InvalidAlgorithmParameterException iape) {
// never thrown when null parameters are used;
// but re-throw it just in case
InvalidKeyException ike =
new InvalidKeyException("Wrong parameters");
ike.initCause(iape);
throw ike;
throw new InvalidKeyException("Wrong parameters", iape);
}
}

@@ -237,10 +234,7 @@ protected void engineInit(int opmode, Key key,
params.getParameterSpec(OAEPParameterSpec.class);
init(opmode, key, random, spec);
} catch (InvalidParameterSpecException ipse) {
InvalidAlgorithmParameterException iape =
new InvalidAlgorithmParameterException("Wrong parameter");
iape.initCause(ipse);
throw iape;
throw new InvalidAlgorithmParameterException("Wrong parameter", ipse);
}
}
}
@@ -1712,9 +1712,7 @@ private static void throwMiscException(Throwable th) throws IOException {
} else if (th instanceof Error) {
throw (Error) th;
} else {
IOException ex = new IOException("unexpected exception type");
ex.initCause(th);
throw ex;
throw new IOException("unexpected exception type", th);
}
}

@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -32,7 +32,6 @@

import sun.security.util.AnchorCertificates;
import sun.security.x509.NameConstraintsExtension;
import sun.security.x509.X500Name;

/**
* A trust anchor or most-trusted Certification Authority (CA).
@@ -286,10 +285,7 @@ private void setNameConstraints(byte[] bytes) {
try {
nc = new NameConstraintsExtension(Boolean.FALSE, bytes);
} catch (IOException ioe) {
IllegalArgumentException iae =
new IllegalArgumentException(ioe.getMessage());
iae.initCause(ioe);
throw iae;
throw new IllegalArgumentException(ioe.getMessage(), ioe);
}
}
}
@@ -371,7 +371,7 @@ private static HashSet<X500Principal> parseIssuerNames(Collection<Object> names)
try {
x500Principals.add(new X500Principal((byte[])nameObject));
} catch (IllegalArgumentException e) {
throw (IOException)new IOException("Invalid name").initCause(e);
throw new IOException("Invalid name", e);
}
}
}
@@ -411,7 +411,7 @@ public static Duration parse(CharSequence text) {
try {
return create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos);
} 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 @@ private static long parseNumber(CharSequence text, int start, int end, int multi
long val = Long.parseLong(text, start, end, 10);
return Math.multiplyExact(val, multiplier);
} 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 @@ private static int parseFraction(CharSequence text, int start, int end, int nega
}
return fraction * negate;
} 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);
}
}

@@ -372,12 +372,9 @@ public final SSLEngine createSSLEngine() {
try {
return contextSpi.engineCreateSSLEngine();
} catch (AbstractMethodError e) {
UnsupportedOperationException unsup =
new UnsupportedOperationException(
"Provider: " + getProvider() +
" doesn't support this operation");
unsup.initCause(e);
throw unsup;
throw new UnsupportedOperationException(
"Provider: " + getProvider() +
" doesn't support this operation", e);
}
}

@@ -412,12 +409,9 @@ public final SSLEngine createSSLEngine(String peerHost, int peerPort) {
try {
return contextSpi.engineCreateSSLEngine(peerHost, peerPort);
} catch (AbstractMethodError e) {
UnsupportedOperationException unsup =
new UnsupportedOperationException(
"Provider: " + getProvider() +
" does not support this operation");
unsup.initCause(e);
throw unsup;
throw new UnsupportedOperationException(
"Provider: " + getProvider() +
" does not support this operation", e);
}
}

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

1 comment on commit 1459180

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 1459180 Oct 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.