Skip to content

Commit

Permalink
[PATCH] Cleanup unnecessary calls to Throwable.initCause() in java.ba…
Browse files Browse the repository at this point in the history
…se module

Pass cause exception as constructor parameter is shorter and easier to read.
  • Loading branch information
turbanoff committed Sep 16, 2021
1 parent 7e92abe commit 9e0e045
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 133 deletions.
Expand Up @@ -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);
}
}
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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);
}
}

Expand Down
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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();
}
Expand Down
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down
@@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}

Expand Down
Expand Up @@ -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);
Expand Down
Expand Up @@ -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);
Expand Down
Expand Up @@ -119,9 +119,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);

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/java.base/share/classes/java/io/ObjectStreamClass.java
Expand Up @@ -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);
}
}

Expand Down
@@ -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
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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
Expand Down Expand Up @@ -374,7 +374,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);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/java.base/share/classes/java/time/Duration.java
Expand Up @@ -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);
}
}
}
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
18 changes: 6 additions & 12 deletions src/java.base/share/classes/javax/net/ssl/SSLContext.java
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 9e0e045

Please sign in to comment.