8264222: Use switch expression in jshell where possible

Reviewed-by: briangoetz
This commit is contained in:
Tagir F. Valeev 2021-04-01 06:02:21 +00:00
parent 39f0b27a12
commit 3997c99e0a
5 changed files with 87 additions and 163 deletions

View File

@ -43,10 +43,10 @@ class ArgTokenizer {
private final String prefix; private final String prefix;
private final int length; private final int length;
private int next = 0; private int next = 0;
private char buf[] = new char[20]; private char[] buf = new char[20];
private int mark; private int mark;
private final byte ctype[] = new byte[256]; private final byte[] ctype = new byte[256];
private static final byte CT_ALPHA = 0; private static final byte CT_ALPHA = 0;
private static final byte CT_WHITESPACE = 1; private static final byte CT_WHITESPACE = 1;
private static final byte CT_QUOTE = 8; private static final byte CT_QUOTE = 8;
@ -258,24 +258,17 @@ class ArgTokenizer {
} }
private int unicode2ctype(int c) { private int unicode2ctype(int c) {
switch (c) { return switch (c) {
case 0x1680: case 0x1680, 0x180E, 0x200A, 0x202F, 0x205F, 0x3000 -> CT_WHITESPACE;
case 0x180E: default -> CT_ALPHA;
case 0x200A: };
case 0x202F:
case 0x205F:
case 0x3000:
return CT_WHITESPACE;
default:
return CT_ALPHA;
}
} }
/** /**
* Parses the next token of this tokenizer. * Parses the next token of this tokenizer.
*/ */
public void nextToken() { public void nextToken() {
byte ct[] = ctype; byte[] ct = ctype;
int c; int c;
int lctype; int lctype;
sval = null; sval = null;
@ -330,29 +323,16 @@ class ArgTokenizer {
} else } else
d = c2; d = c2;
} else { } else {
switch (c) { c = switch (c) {
case 'a': case 'a' -> 0x7;
c = 0x7; case 'b' -> '\b';
break; case 'f' -> 0xC;
case 'b': case 'n' -> '\n';
c = '\b'; case 'r' -> '\r';
break; case 't' -> '\t';
case 'f': case 'v' -> 0xB;
c = 0xC; default -> c;
break; };
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'v':
c = 0xB;
break;
}
d = read(); d = read();
} }
} else { } else {

View File

@ -2802,14 +2802,11 @@ public class JShellTool implements MessageHandler {
*/ */
int order(String id) { int order(String id) {
try { try {
switch (id.charAt(0)) { return switch (id.charAt(0)) {
case 's': case 's' -> Integer.parseInt(id.substring(1));
return Integer.parseInt(id.substring(1)); case 'e' -> 0x40000000 + Integer.parseInt(id.substring(1));
case 'e': default -> 0x20000000 + Integer.parseInt(id);
return 0x40000000 + Integer.parseInt(id.substring(1)); };
default:
return 0x20000000 + Integer.parseInt(id);
}
} catch (Exception ex) { } catch (Exception ex) {
return 0x60000000; return 0x60000000;
} }

View File

@ -230,17 +230,11 @@ class Startup {
} else if (all.length % 4 == 0) { } else if (all.length % 4 == 0) {
List<StartupEntry> e = new ArrayList<>(all.length / 4); List<StartupEntry> e = new ArrayList<>(all.length / 4);
for (int i = 0; i < all.length; i += 4) { for (int i = 0; i < all.length; i += 4) {
final boolean isBuiltIn; final boolean isBuiltIn = switch (all[i]) {
switch (all[i]) { case "*" -> true;
case "*": case "-" -> false;
isBuiltIn = true; default -> throw new IllegalArgumentException("Unexpected StartupEntry kind: " + all[i]);
break; };
case "-":
isBuiltIn = false;
break;
default:
throw new IllegalArgumentException("Unexpected StartupEntry kind: " + all[i]);
}
String name = all[i + 1]; String name = all[i + 1];
String timeStamp = all[i + 2]; String timeStamp = all[i + 2];
String content = all[i + 3]; String content = all[i + 3];

View File

@ -76,7 +76,6 @@ import static java.util.Collections.singletonList;
import com.sun.tools.javac.code.Symbol.TypeSymbol; import com.sun.tools.javac.code.Symbol.TypeSymbol;
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN; import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
import static jdk.jshell.Snippet.Status.RECOVERABLE_DEFINED; import static jdk.jshell.Snippet.Status.RECOVERABLE_DEFINED;
import static jdk.jshell.Snippet.Status.RECOVERABLE_NOT_DEFINED;
import static jdk.jshell.Snippet.Status.VALID; import static jdk.jshell.Snippet.Status.VALID;
import static jdk.jshell.Util.DOIT_METHOD_NAME; import static jdk.jshell.Util.DOIT_METHOD_NAME;
import static jdk.jshell.Util.PREFIX_PATTERN; import static jdk.jshell.Util.PREFIX_PATTERN;
@ -212,30 +211,28 @@ class Eval {
String compileSourceInt = new MaskCommentsAndModifiers(compileSource, true).cleared(); String compileSourceInt = new MaskCommentsAndModifiers(compileSource, true).cleared();
state.debug(DBG_GEN, "Kind: %s -- %s\n", unitTree.getKind(), unitTree); state.debug(DBG_GEN, "Kind: %s -- %s\n", unitTree.getKind(), unitTree);
switch (unitTree.getKind()) { return switch (unitTree.getKind()) {
case IMPORT: case IMPORT
return processImport(userSource, compileSourceInt); -> processImport(userSource, compileSourceInt);
case VARIABLE: case VARIABLE
return processVariables(userSource, units, compileSourceInt, pt); -> processVariables(userSource, units, compileSourceInt, pt);
case EXPRESSION_STATEMENT: case EXPRESSION_STATEMENT
return processExpression(userSource, unitTree, compileSourceInt, pt); -> processExpression(userSource, unitTree, compileSourceInt, pt);
case CLASS: case CLASS
return processClass(userSource, unitTree, compileSourceInt, SubKind.CLASS_SUBKIND, pt); -> processClass(userSource, unitTree, compileSourceInt, SubKind.CLASS_SUBKIND, pt);
case ENUM: case ENUM
return processClass(userSource, unitTree, compileSourceInt, SubKind.ENUM_SUBKIND, pt); -> processClass(userSource, unitTree, compileSourceInt, SubKind.ENUM_SUBKIND, pt);
case ANNOTATION_TYPE: case ANNOTATION_TYPE
return processClass(userSource, unitTree, compileSourceInt, SubKind.ANNOTATION_TYPE_SUBKIND, pt); -> processClass(userSource, unitTree, compileSourceInt, SubKind.ANNOTATION_TYPE_SUBKIND, pt);
case INTERFACE: case INTERFACE
return processClass(userSource, unitTree, compileSourceInt, SubKind.INTERFACE_SUBKIND, pt); -> processClass(userSource, unitTree, compileSourceInt, SubKind.INTERFACE_SUBKIND, pt);
case RECORD: case RECORD
@SuppressWarnings("preview") -> processClass(userSource, unitTree, compileSourceInt, SubKind.RECORD_SUBKIND, pt);
List<Snippet> snippets = processClass(userSource, unitTree, compileSourceInt, SubKind.RECORD_SUBKIND, pt); case METHOD
return snippets; -> processMethod(userSource, unitTree, compileSourceInt, pt);
case METHOD: default
return processMethod(userSource, unitTree, compileSourceInt, pt); -> processStatement(userSource, compileSourceInt);
default: };
return processStatement(userSource, compileSourceInt);
}
}); });
} }
@ -370,32 +367,17 @@ class Eval {
winit = winit == null ? Wrap.rangeWrap(compileSource, rinit) : winit; winit = winit == null ? Wrap.rangeWrap(compileSource, rinit) : winit;
nameMax = rinit.begin - 1; nameMax = rinit.begin - 1;
} else { } else {
String sinit; String sinit = switch (typeName) {
switch (typeName) { case "byte",
case "byte": "short",
case "short": "int" -> "0";
case "int": case "long" -> "0L";
sinit = "0"; case "float" -> "0.0f";
break; case "double" -> "0.0d";
case "long": case "boolean" -> "false";
sinit = "0L"; case "char" -> "'\\u0000'";
break; default -> "null";
case "float": };
sinit = "0.0f";
break;
case "double":
sinit = "0.0d";
break;
case "boolean":
sinit = "false";
break;
case "char":
sinit = "'\\u0000'";
break;
default:
sinit = "null";
break;
}
winit = Wrap.simpleWrap(sinit); winit = Wrap.simpleWrap(sinit);
subkind = SubKind.VAR_DECLARATION_SUBKIND; subkind = SubKind.VAR_DECLARATION_SUBKIND;
} }
@ -862,23 +844,14 @@ class Eval {
// Install wrapper for query by SourceCodeAnalysis.wrapper // Install wrapper for query by SourceCodeAnalysis.wrapper
String compileSource = Util.trimEnd(new MaskCommentsAndModifiers(userSource, true).cleared()); String compileSource = Util.trimEnd(new MaskCommentsAndModifiers(userSource, true).cleared());
OuterWrap outer; OuterWrap outer = switch (probableKind) {
switch (probableKind) { case IMPORT -> state.outerMap.wrapImport(Wrap.simpleWrap(compileSource), snip);
case IMPORT: case EXPRESSION -> state.outerMap.wrapInTrialClass(Wrap.methodReturnWrap(compileSource));
outer = state.outerMap.wrapImport(Wrap.simpleWrap(compileSource), snip); case VAR,
break; TYPE_DECL,
case EXPRESSION: METHOD -> state.outerMap.wrapInTrialClass(Wrap.classMemberWrap(compileSource));
outer = state.outerMap.wrapInTrialClass(Wrap.methodReturnWrap(compileSource)); default -> state.outerMap.wrapInTrialClass(Wrap.methodWrap(compileSource));
break; };
case VAR:
case TYPE_DECL:
case METHOD:
outer = state.outerMap.wrapInTrialClass(Wrap.classMemberWrap(compileSource));
break;
default:
outer = state.outerMap.wrapInTrialClass(Wrap.methodWrap(compileSource));
break;
}
snip.setOuterWrap(outer); snip.setOuterWrap(outer);
return singletonList(snip); return singletonList(snip);

View File

@ -275,19 +275,11 @@ class SourceCodeAnalysisImpl extends SourceCodeAnalysis {
if (code.trim().isEmpty()) { //TODO: comment handling if (code.trim().isEmpty()) { //TODO: comment handling
code += ";"; code += ";";
} }
OuterWrap codeWrap; OuterWrap codeWrap = switch (guessKind(code)) {
switch (guessKind(code)) { case IMPORT -> proc.outerMap.wrapImport(Wrap.simpleWrap(code + "any.any"), null);
case IMPORT: case CLASS, METHOD -> proc.outerMap.wrapInTrialClass(Wrap.classMemberWrap(code));
codeWrap = proc.outerMap.wrapImport(Wrap.simpleWrap(code + "any.any"), null); default -> proc.outerMap.wrapInTrialClass(Wrap.methodWrap(code));
break; };
case CLASS:
case METHOD:
codeWrap = proc.outerMap.wrapInTrialClass(Wrap.classMemberWrap(code));
break;
default:
codeWrap = proc.outerMap.wrapInTrialClass(Wrap.methodWrap(code));
break;
}
String requiredPrefix = identifier; String requiredPrefix = identifier;
return computeSuggestions(codeWrap, cursor, anchor).stream() return computeSuggestions(codeWrap, cursor, anchor).stream()
.filter(s -> s.continuation().startsWith(requiredPrefix) && !s.continuation().equals(REPL_DOESNOTMATTER_CLASS_NAME)) .filter(s -> s.continuation().startsWith(requiredPrefix) && !s.continuation().equals(REPL_DOESNOTMATTER_CLASS_NAME))
@ -502,23 +494,14 @@ class SourceCodeAnalysisImpl extends SourceCodeAnalysis {
addScopeElements(at, scope, IDENTITY, accept, smartFilter, result); addScopeElements(at, scope, IDENTITY, accept, smartFilter, result);
Tree parent = tp.getParentPath().getLeaf(); Tree parent = tp.getParentPath().getLeaf();
switch (parent.getKind()) { accept = switch (parent.getKind()) {
case VARIABLE: case VARIABLE -> ((VariableTree) parent).getType() == tp.getLeaf() ?
accept = ((VariableTree)parent).getType() == tp.getLeaf() ? IS_VOID.negate() :
IS_VOID.negate() : TRUE;
TRUE; case PARAMETERIZED_TYPE -> FALSE; // TODO: JEP 218: Generics over Primitive Types
break; case TYPE_PARAMETER, CLASS, INTERFACE, ENUM -> FALSE;
case PARAMETERIZED_TYPE: // TODO: JEP 218: Generics over Primitive Types default -> TRUE;
case TYPE_PARAMETER: };
case CLASS:
case INTERFACE:
case ENUM:
accept = FALSE;
break;
default:
accept = TRUE;
break;
}
addElements(primitivesOrVoid(at), accept, smartFilter, result); addElements(primitivesOrVoid(at), accept, smartFilter, result);
break; break;
} }
@ -1103,15 +1086,12 @@ class SourceCodeAnalysisImpl extends SourceCodeAnalysis {
private TypeMirror resultTypeOf(Element el) { private TypeMirror resultTypeOf(Element el) {
//TODO: should reflect the type of site! //TODO: should reflect the type of site!
switch (el.getKind()) { return switch (el.getKind()) {
case METHOD: case METHOD -> ((ExecutableElement) el).getReturnType();
return ((ExecutableElement) el).getReturnType(); // TODO: INSTANCE_INIT and STATIC_INIT should be filtered out
case CONSTRUCTOR: case CONSTRUCTOR, INSTANCE_INIT, STATIC_INIT -> el.getEnclosingElement().asType();
case INSTANCE_INIT: case STATIC_INIT: //TODO: should be filtered out default -> el.asType();
return el.getEnclosingElement().asType(); };
default:
return el.asType();
}
} }
private void addScopeElements(AnalyzeTask at, Scope scope, Function<Element, Iterable<? extends Element>> elementConvertor, Predicate<Element> filter, Predicate<Element> smartFilter, List<Suggestion> result) { private void addScopeElements(AnalyzeTask at, Scope scope, Function<Element, Iterable<? extends Element>> elementConvertor, Predicate<Element> filter, Predicate<Element> smartFilter, List<Suggestion> result) {