8264222: Use switch expression in jshell where possible
Reviewed-by: briangoetz
This commit is contained in:
parent
39f0b27a12
commit
3997c99e0a
@ -43,10 +43,10 @@ class ArgTokenizer {
|
||||
private final String prefix;
|
||||
private final int length;
|
||||
private int next = 0;
|
||||
private char buf[] = new char[20];
|
||||
private char[] buf = new char[20];
|
||||
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_WHITESPACE = 1;
|
||||
private static final byte CT_QUOTE = 8;
|
||||
@ -258,24 +258,17 @@ class ArgTokenizer {
|
||||
}
|
||||
|
||||
private int unicode2ctype(int c) {
|
||||
switch (c) {
|
||||
case 0x1680:
|
||||
case 0x180E:
|
||||
case 0x200A:
|
||||
case 0x202F:
|
||||
case 0x205F:
|
||||
case 0x3000:
|
||||
return CT_WHITESPACE;
|
||||
default:
|
||||
return CT_ALPHA;
|
||||
}
|
||||
return switch (c) {
|
||||
case 0x1680, 0x180E, 0x200A, 0x202F, 0x205F, 0x3000 -> CT_WHITESPACE;
|
||||
default -> CT_ALPHA;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the next token of this tokenizer.
|
||||
*/
|
||||
public void nextToken() {
|
||||
byte ct[] = ctype;
|
||||
byte[] ct = ctype;
|
||||
int c;
|
||||
int lctype;
|
||||
sval = null;
|
||||
@ -330,29 +323,16 @@ class ArgTokenizer {
|
||||
} else
|
||||
d = c2;
|
||||
} else {
|
||||
switch (c) {
|
||||
case 'a':
|
||||
c = 0x7;
|
||||
break;
|
||||
case 'b':
|
||||
c = '\b';
|
||||
break;
|
||||
case 'f':
|
||||
c = 0xC;
|
||||
break;
|
||||
case 'n':
|
||||
c = '\n';
|
||||
break;
|
||||
case 'r':
|
||||
c = '\r';
|
||||
break;
|
||||
case 't':
|
||||
c = '\t';
|
||||
break;
|
||||
case 'v':
|
||||
c = 0xB;
|
||||
break;
|
||||
}
|
||||
c = switch (c) {
|
||||
case 'a' -> 0x7;
|
||||
case 'b' -> '\b';
|
||||
case 'f' -> 0xC;
|
||||
case 'n' -> '\n';
|
||||
case 'r' -> '\r';
|
||||
case 't' -> '\t';
|
||||
case 'v' -> 0xB;
|
||||
default -> c;
|
||||
};
|
||||
d = read();
|
||||
}
|
||||
} else {
|
||||
|
@ -2802,14 +2802,11 @@ public class JShellTool implements MessageHandler {
|
||||
*/
|
||||
int order(String id) {
|
||||
try {
|
||||
switch (id.charAt(0)) {
|
||||
case 's':
|
||||
return Integer.parseInt(id.substring(1));
|
||||
case 'e':
|
||||
return 0x40000000 + Integer.parseInt(id.substring(1));
|
||||
default:
|
||||
return 0x20000000 + Integer.parseInt(id);
|
||||
}
|
||||
return switch (id.charAt(0)) {
|
||||
case 's' -> Integer.parseInt(id.substring(1));
|
||||
case 'e' -> 0x40000000 + Integer.parseInt(id.substring(1));
|
||||
default -> 0x20000000 + Integer.parseInt(id);
|
||||
};
|
||||
} catch (Exception ex) {
|
||||
return 0x60000000;
|
||||
}
|
||||
|
@ -230,17 +230,11 @@ class Startup {
|
||||
} else if (all.length % 4 == 0) {
|
||||
List<StartupEntry> e = new ArrayList<>(all.length / 4);
|
||||
for (int i = 0; i < all.length; i += 4) {
|
||||
final boolean isBuiltIn;
|
||||
switch (all[i]) {
|
||||
case "*":
|
||||
isBuiltIn = true;
|
||||
break;
|
||||
case "-":
|
||||
isBuiltIn = false;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unexpected StartupEntry kind: " + all[i]);
|
||||
}
|
||||
final boolean isBuiltIn = switch (all[i]) {
|
||||
case "*" -> true;
|
||||
case "-" -> false;
|
||||
default -> throw new IllegalArgumentException("Unexpected StartupEntry kind: " + all[i]);
|
||||
};
|
||||
String name = all[i + 1];
|
||||
String timeStamp = all[i + 2];
|
||||
String content = all[i + 3];
|
||||
|
@ -76,7 +76,6 @@ import static java.util.Collections.singletonList;
|
||||
import com.sun.tools.javac.code.Symbol.TypeSymbol;
|
||||
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
|
||||
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.Util.DOIT_METHOD_NAME;
|
||||
import static jdk.jshell.Util.PREFIX_PATTERN;
|
||||
@ -212,30 +211,28 @@ class Eval {
|
||||
String compileSourceInt = new MaskCommentsAndModifiers(compileSource, true).cleared();
|
||||
|
||||
state.debug(DBG_GEN, "Kind: %s -- %s\n", unitTree.getKind(), unitTree);
|
||||
switch (unitTree.getKind()) {
|
||||
case IMPORT:
|
||||
return processImport(userSource, compileSourceInt);
|
||||
case VARIABLE:
|
||||
return processVariables(userSource, units, compileSourceInt, pt);
|
||||
case EXPRESSION_STATEMENT:
|
||||
return processExpression(userSource, unitTree, compileSourceInt, pt);
|
||||
case CLASS:
|
||||
return processClass(userSource, unitTree, compileSourceInt, SubKind.CLASS_SUBKIND, pt);
|
||||
case ENUM:
|
||||
return processClass(userSource, unitTree, compileSourceInt, SubKind.ENUM_SUBKIND, pt);
|
||||
case ANNOTATION_TYPE:
|
||||
return processClass(userSource, unitTree, compileSourceInt, SubKind.ANNOTATION_TYPE_SUBKIND, pt);
|
||||
case INTERFACE:
|
||||
return processClass(userSource, unitTree, compileSourceInt, SubKind.INTERFACE_SUBKIND, pt);
|
||||
case RECORD:
|
||||
@SuppressWarnings("preview")
|
||||
List<Snippet> snippets = processClass(userSource, unitTree, compileSourceInt, SubKind.RECORD_SUBKIND, pt);
|
||||
return snippets;
|
||||
case METHOD:
|
||||
return processMethod(userSource, unitTree, compileSourceInt, pt);
|
||||
default:
|
||||
return processStatement(userSource, compileSourceInt);
|
||||
}
|
||||
return switch (unitTree.getKind()) {
|
||||
case IMPORT
|
||||
-> processImport(userSource, compileSourceInt);
|
||||
case VARIABLE
|
||||
-> processVariables(userSource, units, compileSourceInt, pt);
|
||||
case EXPRESSION_STATEMENT
|
||||
-> processExpression(userSource, unitTree, compileSourceInt, pt);
|
||||
case CLASS
|
||||
-> processClass(userSource, unitTree, compileSourceInt, SubKind.CLASS_SUBKIND, pt);
|
||||
case ENUM
|
||||
-> processClass(userSource, unitTree, compileSourceInt, SubKind.ENUM_SUBKIND, pt);
|
||||
case ANNOTATION_TYPE
|
||||
-> processClass(userSource, unitTree, compileSourceInt, SubKind.ANNOTATION_TYPE_SUBKIND, pt);
|
||||
case INTERFACE
|
||||
-> processClass(userSource, unitTree, compileSourceInt, SubKind.INTERFACE_SUBKIND, pt);
|
||||
case RECORD
|
||||
-> processClass(userSource, unitTree, compileSourceInt, SubKind.RECORD_SUBKIND, pt);
|
||||
case METHOD
|
||||
-> processMethod(userSource, unitTree, compileSourceInt, pt);
|
||||
default
|
||||
-> processStatement(userSource, compileSourceInt);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@ -370,32 +367,17 @@ class Eval {
|
||||
winit = winit == null ? Wrap.rangeWrap(compileSource, rinit) : winit;
|
||||
nameMax = rinit.begin - 1;
|
||||
} else {
|
||||
String sinit;
|
||||
switch (typeName) {
|
||||
case "byte":
|
||||
case "short":
|
||||
case "int":
|
||||
sinit = "0";
|
||||
break;
|
||||
case "long":
|
||||
sinit = "0L";
|
||||
break;
|
||||
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;
|
||||
}
|
||||
String sinit = switch (typeName) {
|
||||
case "byte",
|
||||
"short",
|
||||
"int" -> "0";
|
||||
case "long" -> "0L";
|
||||
case "float" -> "0.0f";
|
||||
case "double" -> "0.0d";
|
||||
case "boolean" -> "false";
|
||||
case "char" -> "'\\u0000'";
|
||||
default -> "null";
|
||||
};
|
||||
winit = Wrap.simpleWrap(sinit);
|
||||
subkind = SubKind.VAR_DECLARATION_SUBKIND;
|
||||
}
|
||||
@ -862,23 +844,14 @@ class Eval {
|
||||
|
||||
// Install wrapper for query by SourceCodeAnalysis.wrapper
|
||||
String compileSource = Util.trimEnd(new MaskCommentsAndModifiers(userSource, true).cleared());
|
||||
OuterWrap outer;
|
||||
switch (probableKind) {
|
||||
case IMPORT:
|
||||
outer = state.outerMap.wrapImport(Wrap.simpleWrap(compileSource), snip);
|
||||
break;
|
||||
case EXPRESSION:
|
||||
outer = state.outerMap.wrapInTrialClass(Wrap.methodReturnWrap(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;
|
||||
}
|
||||
OuterWrap outer = switch (probableKind) {
|
||||
case IMPORT -> state.outerMap.wrapImport(Wrap.simpleWrap(compileSource), snip);
|
||||
case EXPRESSION -> state.outerMap.wrapInTrialClass(Wrap.methodReturnWrap(compileSource));
|
||||
case VAR,
|
||||
TYPE_DECL,
|
||||
METHOD -> state.outerMap.wrapInTrialClass(Wrap.classMemberWrap(compileSource));
|
||||
default -> state.outerMap.wrapInTrialClass(Wrap.methodWrap(compileSource));
|
||||
};
|
||||
snip.setOuterWrap(outer);
|
||||
|
||||
return singletonList(snip);
|
||||
|
@ -275,19 +275,11 @@ class SourceCodeAnalysisImpl extends SourceCodeAnalysis {
|
||||
if (code.trim().isEmpty()) { //TODO: comment handling
|
||||
code += ";";
|
||||
}
|
||||
OuterWrap codeWrap;
|
||||
switch (guessKind(code)) {
|
||||
case IMPORT:
|
||||
codeWrap = proc.outerMap.wrapImport(Wrap.simpleWrap(code + "any.any"), null);
|
||||
break;
|
||||
case CLASS:
|
||||
case METHOD:
|
||||
codeWrap = proc.outerMap.wrapInTrialClass(Wrap.classMemberWrap(code));
|
||||
break;
|
||||
default:
|
||||
codeWrap = proc.outerMap.wrapInTrialClass(Wrap.methodWrap(code));
|
||||
break;
|
||||
}
|
||||
OuterWrap codeWrap = switch (guessKind(code)) {
|
||||
case IMPORT -> proc.outerMap.wrapImport(Wrap.simpleWrap(code + "any.any"), null);
|
||||
case CLASS, METHOD -> proc.outerMap.wrapInTrialClass(Wrap.classMemberWrap(code));
|
||||
default -> proc.outerMap.wrapInTrialClass(Wrap.methodWrap(code));
|
||||
};
|
||||
String requiredPrefix = identifier;
|
||||
return computeSuggestions(codeWrap, cursor, anchor).stream()
|
||||
.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);
|
||||
|
||||
Tree parent = tp.getParentPath().getLeaf();
|
||||
switch (parent.getKind()) {
|
||||
case VARIABLE:
|
||||
accept = ((VariableTree)parent).getType() == tp.getLeaf() ?
|
||||
IS_VOID.negate() :
|
||||
TRUE;
|
||||
break;
|
||||
case PARAMETERIZED_TYPE: // TODO: JEP 218: Generics over Primitive Types
|
||||
case TYPE_PARAMETER:
|
||||
case CLASS:
|
||||
case INTERFACE:
|
||||
case ENUM:
|
||||
accept = FALSE;
|
||||
break;
|
||||
default:
|
||||
accept = TRUE;
|
||||
break;
|
||||
}
|
||||
accept = switch (parent.getKind()) {
|
||||
case VARIABLE -> ((VariableTree) parent).getType() == tp.getLeaf() ?
|
||||
IS_VOID.negate() :
|
||||
TRUE;
|
||||
case PARAMETERIZED_TYPE -> FALSE; // TODO: JEP 218: Generics over Primitive Types
|
||||
case TYPE_PARAMETER, CLASS, INTERFACE, ENUM -> FALSE;
|
||||
default -> TRUE;
|
||||
};
|
||||
addElements(primitivesOrVoid(at), accept, smartFilter, result);
|
||||
break;
|
||||
}
|
||||
@ -1103,15 +1086,12 @@ class SourceCodeAnalysisImpl extends SourceCodeAnalysis {
|
||||
|
||||
private TypeMirror resultTypeOf(Element el) {
|
||||
//TODO: should reflect the type of site!
|
||||
switch (el.getKind()) {
|
||||
case METHOD:
|
||||
return ((ExecutableElement) el).getReturnType();
|
||||
case CONSTRUCTOR:
|
||||
case INSTANCE_INIT: case STATIC_INIT: //TODO: should be filtered out
|
||||
return el.getEnclosingElement().asType();
|
||||
default:
|
||||
return el.asType();
|
||||
}
|
||||
return switch (el.getKind()) {
|
||||
case METHOD -> ((ExecutableElement) el).getReturnType();
|
||||
// TODO: INSTANCE_INIT and STATIC_INIT should be filtered out
|
||||
case CONSTRUCTOR, INSTANCE_INIT, STATIC_INIT -> el.getEnclosingElement().asType();
|
||||
default -> el.asType();
|
||||
};
|
||||
}
|
||||
|
||||
private void addScopeElements(AnalyzeTask at, Scope scope, Function<Element, Iterable<? extends Element>> elementConvertor, Predicate<Element> filter, Predicate<Element> smartFilter, List<Suggestion> result) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user