RFR: 8267670: Update java.io, java.math, and java.text to use switch expressions [v2]
Patrick Concannon
pconcannon at openjdk.java.net
Tue May 25 14:57:24 UTC 2021
On Tue, 25 May 2021 12:31:38 GMT, Chris Hegarty <chegar at openjdk.org> wrote:
>> Patrick Concannon has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains three additional commits since the last revision:
>>
>> - 8267670: Updated code to use yield
>> - Merge remote-tracking branch 'origin/master' into JDK-8267670
>> - 8267670: Update java.io, java.math, and java.text to use switch expressions
>
> src/java.base/share/classes/java/io/ObjectInputStream.java line 1877:
>
>> 1875: descriptor.checkInitialized();
>> 1876: }
>> 1877: default -> throw new StreamCorruptedException(
>
> What would you think of assigning descriptor with the value returned from evaluating the switch?
>
> Either:
>
> 1)
>
> ObjectStreamClass descriptor = switch (tc) {
> case TC_NULL -> (ObjectStreamClass) readNull();
> case TC_PROXYCLASSDESC -> readProxyDesc(unshared);
> case TC_CLASSDESC -> readNonProxyDesc(unshared);
> case TC_REFERENCE -> readAndCheckHandle(unshared);
> default -> throw new StreamCorruptedException(String.format("invalid type code: %02X", tc));
> };
> return descriptor;
> }
>
> , where the body of TC_REFERENCE is enclosed in readAndCheckHandle, OR
>
> 2) Simply
>
> case TC_REFERENCE -> {
> var d = (ObjectStreamClass)readHandle(unshared);
> // Should only reference initialized class descriptors
> d.checkInitialized();
> yield d; }
Code updated as suggested. See adc8af4
> src/java.base/share/classes/java/io/StreamTokenizer.java line 795:
>
>> 793: * case statements
>> 794: */
>> 795: if (ttype < 256 && ((ctype[ttype] & CT_QUOTE) != 0)) {
>
> Maybe (since its easier to grok the yield rather than the assignment of ret in branches):
>
> String ret = switch (ttype) {
> case TT_EOF -> "EOF";
> case TT_EOL -> "EOL";
> case TT_WORD -> sval;
> case TT_NUMBER -> "n=" + nval;
> case TT_NOTHING -> "NOTHING";
> default -> {
> /*
> * ttype is the first character of either a quoted string or
> * is an ordinary character. ttype can definitely not be less
> * than 0, since those are reserved values used in the previous
> * case statements
> */
> if (ttype < 256 && ((ctype[ttype] & CT_QUOTE) != 0)) {
> yield sval;
> }
> char s[] = new char[3];
> s[0] = s[2] = ''';
> s[1] = (char) ttype;
> yield new String(s);
> }
> };
> return "Token[" + ret + "], line " + LINENO;
Code updated as suggested. See adc8af4
-------------
PR: https://git.openjdk.java.net/jdk/pull/4182
More information about the core-libs-dev
mailing list