On Thu, 24 Nov 2022 08:24:37 GMT, Per Minborg <pminb...@openjdk.org> wrote:
> During the work of another PR (https://github.com/openjdk/jdk/pull/11260), > several improvement areas were identified. These are now adressed in this > separate PR proposing the use of more modern Java constructs as well as > simplifying a large number of logical expressions that were previously > non-normative. > > > This branch has been tested and passed tier1-4 tests. These can all be `switch` expressions: src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/Config.java line 487: > 485: case "noDb" -> nssDbMode = Secmod.DbMode.NO_DB; > 486: default -> throw excToken("nssDbMode must be one of > readWrite, readOnly, and noDb:"); > 487: } Suggestion: nssDbMode = switch (mode) { case "readWrite" -> Secmod.DbMode.READ_WRITE; case "readOnly" -> Secmod.DbMode.READ_ONLY; case "noDb" -> Secmod.DbMode.NO_DB; default -> throw excToken("nssDbMode must be one of readWrite, readOnly, and noDb:"); }; src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/Config.java line 1042: > 1040: case "halt" -> handleStartupErrors = ERR_HALT; > 1041: default -> throw excToken("Invalid value for > handleStartupErrors:"); > 1042: } Suggestion: handleStartupErrors = switch (val) { case "ignoreAll" -> ERR_IGNORE_ALL; case "ignoreMissingLibrary" -> ERR_IGNORE_LIB; case "halt" -> ERR_HALT; default -> throw excToken("Invalid value for handleStartupErrors:"); }; src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Cipher.java line 241: > 239: default -> throw new NoSuchAlgorithmException("Unsupported > mode " + mode); > 240: } > 241: return result; Suggestion: return switch (mode) { case "ECB" -> MODE_ECB; case "CBC" -> { if (blockSize == 0) { throw new NoSuchAlgorithmException ("CBC mode not supported with stream ciphers"); } yield MODE_CBC; } case "CTR" -> MODE_CTR; default -> throw new NoSuchAlgorithmException("Unsupported mode " + mode); }; src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Cipher.java line 373: > 371: // should never happen; checked by Cipher.init() > 372: throw new AssertionError("Unknown mode: " + opmode); > 373: } Suggestion: encrypt = switch (opmode) { case Cipher.ENCRYPT_MODE -> true; case Cipher.DECRYPT_MODE -> false; case Cipher.WRAP_MODE, Cipher.UNWRAP_MODE -> throw new UnsupportedOperationException ("Unsupported mode: " + opmode); default -> // should never happen; checked by Cipher.init() throw new AssertionError("Unknown mode: " + opmode); }; src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyWrapCipher.java line 756: > 754: throw new AssertionError(); > 755: } > 756: ; Empty statement: Suggestion: src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11RSACipher.java line 392: > 390: default -> throw new ProviderException("internal error"); > 391: } > 392: return n; Suggestion: return switch (mode) { case MODE_ENCRYPT -> p11.C_Encrypt (session.id(), 0, buffer, 0, bufOfs, 0, out, outOfs, outLen); case MODE_DECRYPT -> p11.C_Decrypt (session.id(), 0, buffer, 0, bufOfs, 0, out, outOfs, outLen); case MODE_SIGN -> { byte[] tmpBuffer = new byte[bufOfs]; System.arraycopy(buffer, 0, tmpBuffer, 0, bufOfs); tmpBuffer = p11.C_Sign(session.id(), tmpBuffer); if (tmpBuffer.length > outLen) { throw new BadPaddingException( "Output buffer (" + outLen + ") is too small to " + "hold the produced data (" + tmpBuffer.length + ")"); } System.arraycopy(tmpBuffer, 0, out, outOfs, tmpBuffer.length); yield tmpBuffer.length; } case MODE_VERIFY -> p11.C_VerifyRecover (session.id(), buffer, 0, bufOfs, out, outOfs, outLen); default -> throw new ProviderException("internal error"); }; ------------- PR: https://git.openjdk.org/jdk/pull/11348