8268056: Update java.net and java.nio to use switch expressions

Reviewed-by: dfuchs, michaelm, chegar, iris, alanb
This commit is contained in:
Patrick Concannon 2021-06-09 10:13:25 +00:00
parent 9cfd560bb1
commit 438895903b
2 changed files with 30 additions and 41 deletions

View file

@ -211,25 +211,17 @@ class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
throw new SocketException("Reply from SOCKS server has bad length: " + n);
if (data[0] != 0 && data[0] != 4)
throw new SocketException("Reply from SOCKS server has bad version");
SocketException ex = null;
switch (data[1]) {
case 90:
// Success!
external_address = endpoint;
break;
case 91:
ex = new SocketException("SOCKS request rejected");
break;
case 92:
ex = new SocketException("SOCKS server couldn't reach destination");
break;
case 93:
ex = new SocketException("SOCKS authentication failed");
break;
default:
ex = new SocketException("Reply from SOCKS server contains bad status");
break;
}
SocketException ex = switch (data[1]) {
case 90 -> {
// Success!
external_address = endpoint;
yield null;
}
case 91 -> new SocketException("SOCKS request rejected");
case 92 -> new SocketException("SOCKS server couldn't reach destination");
case 93 -> new SocketException("SOCKS authentication failed");
default -> new SocketException("Reply from SOCKS server contains bad status");
};
if (ex != null) {
in.close();
out.close();

View file

@ -2803,40 +2803,37 @@ public final class Files {
try (FileTreeWalker walker = new FileTreeWalker(options, maxDepth)) {
FileTreeWalker.Event ev = walker.walk(start);
do {
FileVisitResult result;
switch (ev.type()) {
case ENTRY :
FileVisitResult result = switch (ev.type()) {
case ENTRY -> {
IOException ioe = ev.ioeException();
if (ioe == null) {
assert ev.attributes() != null;
result = visitor.visitFile(ev.file(), ev.attributes());
yield visitor.visitFile(ev.file(), ev.attributes());
} else {
result = visitor.visitFileFailed(ev.file(), ioe);
yield visitor.visitFileFailed(ev.file(), ioe);
}
break;
case START_DIRECTORY :
result = visitor.preVisitDirectory(ev.file(), ev.attributes());
}
case START_DIRECTORY -> {
var res = visitor.preVisitDirectory(ev.file(), ev.attributes());
// if SKIP_SIBLINGS and SKIP_SUBTREE is returned then
// there shouldn't be any more events for the current
// directory.
if (result == FileVisitResult.SKIP_SUBTREE ||
result == FileVisitResult.SKIP_SIBLINGS)
if (res == FileVisitResult.SKIP_SUBTREE ||
res == FileVisitResult.SKIP_SIBLINGS)
walker.pop();
break;
case END_DIRECTORY :
result = visitor.postVisitDirectory(ev.file(), ev.ioeException());
yield res;
}
case END_DIRECTORY -> {
var res = visitor.postVisitDirectory(ev.file(), ev.ioeException());
// SKIP_SIBLINGS is a no-op for postVisitDirectory
if (result == FileVisitResult.SKIP_SIBLINGS)
result = FileVisitResult.CONTINUE;
break;
default :
throw new AssertionError("Should not get here");
}
if (res == FileVisitResult.SKIP_SIBLINGS)
res = FileVisitResult.CONTINUE;
yield res;
}
default -> throw new AssertionError("Should not get here");
};
if (Objects.requireNonNull(result) != FileVisitResult.CONTINUE) {
if (result == FileVisitResult.TERMINATE) {