Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -258,24 +258,17 @@ private void quoteChar(int ch) {
}

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;
Expand Down Expand Up @@ -330,29 +323,16 @@ public void nextToken() {
} 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2802,14 +2802,11 @@ String[] allIds() {
*/
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;
}
Expand Down
16 changes: 5 additions & 11 deletions src/jdk.jshell/share/classes/jdk/internal/jshell/tool/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,11 @@ static Startup unpack(String storedForm, MessageHandler mh) {
} 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];
Expand Down
109 changes: 41 additions & 68 deletions src/jdk.jshell/share/classes/jdk/jshell/Eval.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
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;
Expand Down Expand Up @@ -212,30 +211,28 @@ private List<Snippet> sourceToSnippets(String userSource) {
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);
};
});
}

Expand Down Expand Up @@ -370,32 +367,17 @@ private List<Snippet> processVariables(String userSource, List<? extends Tree> u
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;
}
Expand Down Expand Up @@ -862,23 +844,14 @@ private List<Snippet> compileFailResult(DiagList diags, String userSource, Kind

// 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);
Expand Down
58 changes: 19 additions & 39 deletions src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysisImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,11 @@ private List<Suggestion> completionSuggestionsImpl(String code, int cursor, int[
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))
Expand Down Expand Up @@ -502,23 +494,14 @@ private List<Suggestion> computeSuggestions(OuterWrap code, int cursor, int[] an
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;
}
Expand Down Expand Up @@ -1103,15 +1086,12 @@ private List<TypeMirror> computeSmartTypesForExecutableType(AnalyzeTask at, Iter

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) {
Expand Down