Skip to content
Permalink
Browse files
8268967: Update java.security to use switch expressions
Reviewed-by: xuelei
  • Loading branch information
pconcannon committed Jun 25, 2021
1 parent b565459 commit 35c4702055ccf11975391df01f62a70e06ecae83
Showing 3 changed files with 46 additions and 107 deletions.
@@ -447,12 +447,8 @@ public String toString() {
PrintStream p = new PrintStream(baos);
p.print(algorithm+" Message Digest from "+getProviderName()+", ");
switch (state) {
case INITIAL:
p.print("<initialized>");
break;
case IN_PROGRESS:
p.print("<in progress>");
break;
case INITIAL -> p.print("<initialized>");
case IN_PROGRESS -> p.print("<in progress>");
}
p.println();
return (baos.toString());
@@ -947,18 +947,12 @@ public final String getAlgorithm() {
* @return a string representation of this signature object.
*/
public String toString() {
String initState = "";
switch (state) {
case UNINITIALIZED:
initState = "<not initialized>";
break;
case VERIFY:
initState = "<initialized for verifying>";
break;
case SIGN:
initState = "<initialized for signing>";
break;
}
String initState = switch (state) {
case UNINITIALIZED -> "<not initialized>";
case VERIFY -> "<initialized for verifying>";
case SIGN -> "<initialized for signing>";
default -> "";
};
return "Signature object: " + getAlgorithm() + initState;
}

@@ -1317,26 +1311,13 @@ private void tryOperation(SignatureSpi spi, int type, Key key,
AlgorithmParameterSpec params, SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException {
switch (type) {
case I_PUB:
spi.engineInitVerify((PublicKey)key);
break;
case I_PUB_PARAM:
spi.engineInitVerify((PublicKey)key, params);
break;
case I_PRIV:
spi.engineInitSign((PrivateKey)key);
break;
case I_PRIV_SR:
spi.engineInitSign((PrivateKey)key, random);
break;
case I_PRIV_PARAM_SR:
spi.engineInitSign((PrivateKey)key, params, random);
break;
case S_PARAM:
spi.engineSetParameter(params);
break;
default:
throw new AssertionError("Internal error: " + type);
case I_PUB -> spi.engineInitVerify((PublicKey) key);
case I_PUB_PARAM -> spi.engineInitVerify((PublicKey) key, params);
case I_PRIV -> spi.engineInitSign((PrivateKey) key);
case I_PRIV_SR -> spi.engineInitSign((PrivateKey) key, random);
case I_PRIV_PARAM_SR -> spi.engineInitSign((PrivateKey) key, params, random);
case S_PARAM -> spi.engineSetParameter(params);
default -> throw new AssertionError("Internal error: " + type);
}
}

@@ -889,29 +889,16 @@ static GeneralNameInterface makeGeneralNameInterface(int type, Object name)
debug.println("X509CertSelector.makeGeneralNameInterface() "
+ "name is String: " + nameAsString);
}
switch (type) {
case NAME_RFC822:
result = new RFC822Name(nameAsString);
break;
case NAME_DNS:
result = new DNSName(nameAsString);
break;
case NAME_DIRECTORY:
result = new X500Name(nameAsString);
break;
case NAME_URI:
result = new URIName(nameAsString);
break;
case NAME_IP:
result = new IPAddressName(nameAsString);
break;
case NAME_OID:
result = new OIDName(nameAsString);
break;
default:
throw new IOException("unable to parse String names of type "
+ type);
}
result = switch (type) {
case NAME_RFC822 -> new RFC822Name(nameAsString);
case NAME_DNS -> new DNSName(nameAsString);
case NAME_DIRECTORY -> new X500Name(nameAsString);
case NAME_URI -> new URIName(nameAsString);
case NAME_IP -> new IPAddressName(nameAsString);
case NAME_OID -> new OIDName(nameAsString);
default -> throw new IOException("unable to parse String names of type "
+ type);
};
if (debug != null) {
debug.println("X509CertSelector.makeGeneralNameInterface() "
+ "result: " + result.toString());
@@ -923,38 +910,19 @@ static GeneralNameInterface makeGeneralNameInterface(int type, Object name)
("X509CertSelector.makeGeneralNameInterface() is byte[]");
}

switch (type) {
case NAME_ANY:
result = new OtherName(val);
break;
case NAME_RFC822:
result = new RFC822Name(val);
break;
case NAME_DNS:
result = new DNSName(val);
break;
case NAME_X400:
result = new X400Address(val);
break;
case NAME_DIRECTORY:
result = new X500Name(val);
break;
case NAME_EDI:
result = new EDIPartyName(val);
break;
case NAME_URI:
result = new URIName(val);
break;
case NAME_IP:
result = new IPAddressName(val);
break;
case NAME_OID:
result = new OIDName(val);
break;
default:
throw new IOException("unable to parse byte array names of "
+ "type " + type);
}
result = switch (type) {
case NAME_ANY -> new OtherName(val);
case NAME_RFC822 -> new RFC822Name(val);
case NAME_DNS -> new DNSName(val);
case NAME_X400 -> new X400Address(val);
case NAME_DIRECTORY -> new X500Name(val);
case NAME_EDI -> new EDIPartyName(val);
case NAME_URI -> new URIName(val);
case NAME_IP -> new IPAddressName(val);
case NAME_OID -> new OIDName(val);
default -> throw new IOException("unable to parse byte array names of "
+ "type " + type);
};
if (debug != null) {
debug.println("X509CertSelector.makeGeneralNameInterface() result: "
+ result.toString());
@@ -1928,20 +1896,14 @@ private static String keyUsageToString(boolean[] k) {
private static Extension getExtensionObject(X509Certificate cert, KnownOIDs extId)
throws IOException {
if (cert instanceof X509CertImpl impl) {
switch (extId) {
case PrivateKeyUsage:
return impl.getPrivateKeyUsageExtension();
case SubjectAlternativeName:
return impl.getSubjectAlternativeNameExtension();
case NameConstraints:
return impl.getNameConstraintsExtension();
case CertificatePolicies:
return impl.getCertificatePoliciesExtension();
case extendedKeyUsage:
return impl.getExtendedKeyUsageExtension();
default:
return null;
}
return switch (extId) {
case PrivateKeyUsage -> impl.getPrivateKeyUsageExtension();
case SubjectAlternativeName -> impl.getSubjectAlternativeNameExtension();
case NameConstraints -> impl.getNameConstraintsExtension();
case CertificatePolicies -> impl.getCertificatePoliciesExtension();
case extendedKeyUsage -> impl.getExtendedKeyUsageExtension();
default -> null;
};
}
byte[] rawExtVal = cert.getExtensionValue(extId.value());
if (rawExtVal == null) {

0 comments on commit 35c4702

Please sign in to comment.