Skip to content

Commit 1459180

Browse files
turbanoffwangweij
authored andcommittedOct 5, 2021
8274079: Cleanup unnecessary calls to Throwable.initCause() in java.base module
Reviewed-by: weijun
1 parent 8609ea5 commit 1459180

22 files changed

+55
-132
lines changed
 

‎src/java.base/share/classes/com/sun/crypto/provider/AESCipher.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ protected OidImpl(int keySize, String mode, String padding) {
7272
engineSetPadding(padding);
7373
} catch (GeneralSecurityException gse) {
7474
// internal error; re-throw as provider exception
75-
ProviderException pe =new ProviderException("Internal Error");
76-
pe.initCause(gse);
77-
throw pe;
75+
throw new ProviderException("Internal Error", gse);
7876
}
7977
}
8078
}

‎src/java.base/share/classes/com/sun/crypto/provider/ConstructKeys.java

+4-16
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,10 @@ private static final PublicKey constructPublicKey(byte[] encodedKey,
7676
encodedKeyAlgorithm +
7777
"algorithm");
7878
} catch (InvalidKeySpecException ikse2) {
79-
InvalidKeyException ike =
80-
new InvalidKeyException("Cannot construct public key");
81-
ike.initCause(ikse2);
82-
throw ike;
79+
throw new InvalidKeyException("Cannot construct public key", ikse2);
8380
}
8481
} catch (InvalidKeySpecException ikse) {
85-
InvalidKeyException ike =
86-
new InvalidKeyException("Cannot construct public key");
87-
ike.initCause(ikse);
88-
throw ike;
82+
throw new InvalidKeyException("Cannot construct public key", ikse);
8983
}
9084

9185
return key;
@@ -116,16 +110,10 @@ private static final PrivateKey constructPrivateKey(byte[] encodedKey,
116110
encodedKeyAlgorithm +
117111
"algorithm");
118112
} catch (InvalidKeySpecException ikse2) {
119-
InvalidKeyException ike =
120-
new InvalidKeyException("Cannot construct private key");
121-
ike.initCause(ikse2);
122-
throw ike;
113+
throw new InvalidKeyException("Cannot construct private key", ikse2);
123114
}
124115
} catch (InvalidKeySpecException ikse) {
125-
InvalidKeyException ike =
126-
new InvalidKeyException("Cannot construct private key");
127-
ike.initCause(ikse);
128-
throw ike;
116+
throw new InvalidKeyException("Cannot construct private key", ikse);
129117
} finally {
130118
SharedSecrets.getJavaSecuritySpecAccess().clearEncodedKeySpec(keySpec);
131119
if (keyBytes != encodedKey) {

‎src/java.base/share/classes/com/sun/crypto/provider/DESedeWrapCipher.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,7 @@ protected void engineInit(int opmode, Key key, SecureRandom random)
181181
engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
182182
} catch (InvalidAlgorithmParameterException iape) {
183183
// should never happen
184-
InvalidKeyException ike =
185-
new InvalidKeyException("Parameters required");
186-
ike.initCause(iape);
187-
throw ike;
184+
throw new InvalidKeyException("Parameters required", iape);
188185
}
189186
}
190187

@@ -285,11 +282,8 @@ protected void engineInit(int opmode, Key key,
285282
paramsEng.engineInit(params.getEncoded());
286283
ivSpec = paramsEng.engineGetParameterSpec(IvParameterSpec.class);
287284
} catch (Exception ex) {
288-
InvalidAlgorithmParameterException iape =
289-
new InvalidAlgorithmParameterException
290-
("Wrong parameter type: IV expected");
291-
iape.initCause(ex);
292-
throw iape;
285+
throw new InvalidAlgorithmParameterException
286+
("Wrong parameter type: IV expected", ex);
293287
}
294288
}
295289
engineInit(opmode, key, ivSpec, random);

‎src/java.base/share/classes/com/sun/crypto/provider/DHPrivateKey.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,8 @@ private void parseKeyBits() throws InvalidKeyException {
291291
DerInputStream in = new DerInputStream(this.key);
292292
this.x = in.getBigInteger();
293293
} catch (IOException e) {
294-
InvalidKeyException ike = new InvalidKeyException(
295-
"Error parsing key encoding: " + e.getMessage());
296-
ike.initCause(e);
297-
throw ike;
294+
throw new InvalidKeyException(
295+
"Error parsing key encoding: " + e.getMessage(), e);
298296
}
299297
}
300298

‎src/java.base/share/classes/com/sun/crypto/provider/PBES2Core.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ protected void engineInit(int opmode, Key key, SecureRandom random)
162162
try {
163163
engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
164164
} catch (InvalidAlgorithmParameterException ie) {
165-
InvalidKeyException ike =
166-
new InvalidKeyException("requires PBE parameters");
167-
ike.initCause(ie);
168-
throw ike;
165+
throw new InvalidKeyException("requires PBE parameters", ie);
169166
}
170167
}
171168

@@ -279,10 +276,7 @@ protected void engineInit(int opmode, Key key,
279276
try {
280277
s = (PBKDF2KeyImpl)kdf.engineGenerateSecret(pbeSpec);
281278
} catch (InvalidKeySpecException ikse) {
282-
InvalidKeyException ike =
283-
new InvalidKeyException("Cannot construct PBE key");
284-
ike.initCause(ikse);
285-
throw ike;
279+
throw new InvalidKeyException("Cannot construct PBE key", ikse);
286280
} finally {
287281
pbeSpec.clearPassword();
288282
}

‎src/java.base/share/classes/com/sun/crypto/provider/PBEWithMD5AndDESCipher.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,7 +28,6 @@
2828
import java.security.*;
2929
import java.security.spec.*;
3030
import javax.crypto.*;
31-
import javax.crypto.spec.*;
3231

3332
/**
3433
* 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)
183182
try {
184183
engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
185184
} catch (InvalidAlgorithmParameterException ie) {
186-
InvalidKeyException ike =
187-
new InvalidKeyException("requires PBE parameters");
188-
ike.initCause(ie);
189-
throw ike;
185+
throw new InvalidKeyException("requires PBE parameters", ie);
190186
}
191187
}
192188

‎src/java.base/share/classes/com/sun/crypto/provider/PBEWithMD5AndTripleDESCipher.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,7 +28,6 @@
2828
import java.security.*;
2929
import java.security.spec.*;
3030
import javax.crypto.*;
31-
import javax.crypto.spec.*;
3231

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

‎src/java.base/share/classes/com/sun/crypto/provider/PBKDF2Core.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,8 @@ protected SecretKey engineTranslateKey(SecretKey key)
151151
try {
152152
return new PBKDF2KeyImpl(spec, prfAlgo);
153153
} catch (InvalidKeySpecException re) {
154-
InvalidKeyException ike = new InvalidKeyException
155-
("Invalid key component(s)");
156-
ike.initCause(re);
157-
throw ike;
154+
throw new InvalidKeyException
155+
("Invalid key component(s)", re);
158156
} finally {
159157
if (password != null) {
160158
Arrays.fill(password, (char) 0);

‎src/java.base/share/classes/com/sun/crypto/provider/PBKDF2HmacSHA1Factory.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,8 @@ protected SecretKey engineTranslateKey(SecretKey key)
151151
try {
152152
return new PBKDF2KeyImpl(spec, "HmacSHA1");
153153
} catch (InvalidKeySpecException re) {
154-
InvalidKeyException ike = new InvalidKeyException
155-
("Invalid key component(s)");
156-
ike.initCause(re);
157-
throw ike;
154+
throw new InvalidKeyException
155+
("Invalid key component(s)", re);
158156
} finally {
159157
if (password != null) {
160158
Arrays.fill(password, (char) 0);

‎src/java.base/share/classes/com/sun/crypto/provider/PBKDF2KeyImpl.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ private static byte[] getPasswordBytes(char[] passwd) {
121121
this.key = deriveKey(prf, passwdBytes, salt, iterCount, keyLength);
122122
} catch (NoSuchAlgorithmException nsae) {
123123
// not gonna happen; re-throw just in case
124-
InvalidKeySpecException ike = new InvalidKeySpecException();
125-
ike.initCause(nsae);
126-
throw ike;
124+
throw new InvalidKeySpecException(nsae);
127125
} finally {
128126
Arrays.fill(passwdBytes, (byte) 0x00);
129127

‎src/java.base/share/classes/com/sun/crypto/provider/PBMAC1Core.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
package com.sun.crypto.provider;
2727

2828
import java.util.Arrays;
29-
import java.nio.ByteBuffer;
3029

31-
import javax.crypto.MacSpi;
3230
import javax.crypto.SecretKey;
3331
import javax.crypto.spec.SecretKeySpec;
3432
import javax.crypto.spec.PBEKeySpec;
@@ -181,10 +179,7 @@ protected void engineInit(Key key, AlgorithmParameterSpec params)
181179
s = (PBKDF2KeyImpl)kdf.engineGenerateSecret(pbeSpec);
182180
derivedKey = s.getEncoded();
183181
} catch (InvalidKeySpecException ikse) {
184-
InvalidKeyException ike =
185-
new InvalidKeyException("Cannot construct PBE key");
186-
ike.initCause(ikse);
187-
throw ike;
182+
throw new InvalidKeyException("Cannot construct PBE key", ikse);
188183
} finally {
189184
pbeSpec.clearPassword();
190185
if (s != null) {

‎src/java.base/share/classes/com/sun/crypto/provider/RSACipher.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,7 @@ protected void engineInit(int opmode, Key key, SecureRandom random)
211211
} catch (InvalidAlgorithmParameterException iape) {
212212
// never thrown when null parameters are used;
213213
// but re-throw it just in case
214-
InvalidKeyException ike =
215-
new InvalidKeyException("Wrong parameters");
216-
ike.initCause(iape);
217-
throw ike;
214+
throw new InvalidKeyException("Wrong parameters", iape);
218215
}
219216
}
220217

@@ -237,10 +234,7 @@ protected void engineInit(int opmode, Key key,
237234
params.getParameterSpec(OAEPParameterSpec.class);
238235
init(opmode, key, random, spec);
239236
} catch (InvalidParameterSpecException ipse) {
240-
InvalidAlgorithmParameterException iape =
241-
new InvalidAlgorithmParameterException("Wrong parameter");
242-
iape.initCause(ipse);
243-
throw iape;
237+
throw new InvalidAlgorithmParameterException("Wrong parameter", ipse);
244238
}
245239
}
246240
}

‎src/java.base/share/classes/java/io/ObjectStreamClass.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1712,9 +1712,7 @@ private static void throwMiscException(Throwable th) throws IOException {
17121712
} else if (th instanceof Error) {
17131713
throw (Error) th;
17141714
} else {
1715-
IOException ex = new IOException("unexpected exception type");
1716-
ex.initCause(th);
1717-
throw ex;
1715+
throw new IOException("unexpected exception type", th);
17181716
}
17191717
}
17201718

‎src/java.base/share/classes/java/security/cert/TrustAnchor.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,7 +32,6 @@
3232

3333
import sun.security.util.AnchorCertificates;
3434
import sun.security.x509.NameConstraintsExtension;
35-
import sun.security.x509.X500Name;
3635

3736
/**
3837
* A trust anchor or most-trusted Certification Authority (CA).
@@ -286,10 +285,7 @@ private void setNameConstraints(byte[] bytes) {
286285
try {
287286
nc = new NameConstraintsExtension(Boolean.FALSE, bytes);
288287
} catch (IOException ioe) {
289-
IllegalArgumentException iae =
290-
new IllegalArgumentException(ioe.getMessage());
291-
iae.initCause(ioe);
292-
throw iae;
288+
throw new IllegalArgumentException(ioe.getMessage(), ioe);
293289
}
294290
}
295291
}

‎src/java.base/share/classes/java/security/cert/X509CRLSelector.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ private static HashSet<X500Principal> parseIssuerNames(Collection<Object> names)
371371
try {
372372
x500Principals.add(new X500Principal((byte[])nameObject));
373373
} catch (IllegalArgumentException e) {
374-
throw (IOException)new IOException("Invalid name").initCause(e);
374+
throw new IOException("Invalid name", e);
375375
}
376376
}
377377
}

‎src/java.base/share/classes/java/time/Duration.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public static Duration parse(CharSequence text) {
411411
try {
412412
return create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos);
413413
} catch (ArithmeticException ex) {
414-
throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: overflow", text, 0).initCause(ex);
414+
throw new DateTimeParseException("Text cannot be parsed to a Duration: overflow", text, 0, ex);
415415
}
416416
}
417417
}
@@ -432,7 +432,7 @@ private static long parseNumber(CharSequence text, int start, int end, int multi
432432
long val = Long.parseLong(text, start, end, 10);
433433
return Math.multiplyExact(val, multiplier);
434434
} catch (NumberFormatException | ArithmeticException ex) {
435-
throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: " + errorText, text, 0).initCause(ex);
435+
throw new DateTimeParseException("Text cannot be parsed to a Duration: " + errorText, text, 0, ex);
436436
}
437437
}
438438

@@ -451,7 +451,7 @@ private static int parseFraction(CharSequence text, int start, int end, int nega
451451
}
452452
return fraction * negate;
453453
} catch (NumberFormatException | ArithmeticException ex) {
454-
throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: fraction", text, 0).initCause(ex);
454+
throw new DateTimeParseException("Text cannot be parsed to a Duration: fraction", text, 0, ex);
455455
}
456456
}
457457

‎src/java.base/share/classes/javax/net/ssl/SSLContext.java

+6-12
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,9 @@ public final SSLEngine createSSLEngine() {
372372
try {
373373
return contextSpi.engineCreateSSLEngine();
374374
} catch (AbstractMethodError e) {
375-
UnsupportedOperationException unsup =
376-
new UnsupportedOperationException(
377-
"Provider: " + getProvider() +
378-
" doesn't support this operation");
379-
unsup.initCause(e);
380-
throw unsup;
375+
throw new UnsupportedOperationException(
376+
"Provider: " + getProvider() +
377+
" doesn't support this operation", e);
381378
}
382379
}
383380

@@ -412,12 +409,9 @@ public final SSLEngine createSSLEngine(String peerHost, int peerPort) {
412409
try {
413410
return contextSpi.engineCreateSSLEngine(peerHost, peerPort);
414411
} catch (AbstractMethodError e) {
415-
UnsupportedOperationException unsup =
416-
new UnsupportedOperationException(
417-
"Provider: " + getProvider() +
418-
" does not support this operation");
419-
unsup.initCause(e);
420-
throw unsup;
412+
throw new UnsupportedOperationException(
413+
"Provider: " + getProvider() +
414+
" does not support this operation", e);
421415
}
422416
}
423417

‎src/java.base/share/classes/javax/security/auth/login/Configuration.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,15 @@ public Void run() {
270270
} catch (PrivilegedActionException e) {
271271
Exception ee = e.getException();
272272
if (ee instanceof InstantiationException) {
273-
throw (SecurityException) new
274-
SecurityException
273+
throw new SecurityException
275274
("Configuration error:" +
276275
ee.getCause().getMessage() +
277-
"\n").initCause(ee.getCause());
276+
"\n", ee.getCause());
278277
} else {
279-
throw (SecurityException) new
280-
SecurityException
278+
throw new SecurityException
281279
("Configuration error: " +
282280
ee.toString() +
283-
"\n").initCause(ee);
281+
"\n", ee);
284282
}
285283
}
286284
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Oct 5, 2021

@openjdk-notifier[bot]
Please sign in to comment.