Skip to content
Permalink
Browse files
8255273: jshell crashes with UnsupportedOperationException: Should no…
…t get here.

Reviewed-by: vromero
  • Loading branch information
Jan Lahoda committed Apr 15, 2021
1 parent e43aee5 commit 325eecbccbe526e370d9bc95f23f82cf8508e665
Showing with 74 additions and 41 deletions.
  1. +64 −40 src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java
  2. +10 −1 test/langtools/jdk/jshell/ToolTabSnippetTest.java
@@ -364,11 +364,49 @@ private boolean complete(CompletionState completionState) {
.distinct()
.count() == 2;
boolean tooManyItems = suggestions.size() > /*in.getAutoprintThreshold()*/AUTOPRINT_THRESHOLD;
CompletionTask ordinaryCompletion =
new OrdinaryCompletionTask(suggestions,
anchor[0],
!command && !doc.isEmpty(),
hasBoth);
CompletionTask ordinaryCompletion;
List<? extends CharSequence> ordinaryCompletionToShow;

if (hasBoth) {
ordinaryCompletionToShow =
suggestions.stream()
.filter(Suggestion::matchesType)
.map(Suggestion::continuation)
.distinct()
.toList();
} else {
ordinaryCompletionToShow =
suggestions.stream()
.map(Suggestion::continuation)
.distinct()
.toList();
}

if (ordinaryCompletionToShow.isEmpty()) {
ordinaryCompletion = new ContinueCompletionTask();
} else {
Optional<String> prefixOpt =
suggestions.stream()
.map(Suggestion::continuation)
.reduce(ConsoleIOContext::commonPrefix);

String prefix =
prefixOpt.orElse("").substring(cursor - anchor[0]);

if (!prefix.isEmpty() && !command) {
//the completion will fill in the prefix, which will invalidate
//the documentation, avoid adding documentation tasks into the
//todo list:
doc = List.of();
}

ordinaryCompletion =
new OrdinaryCompletionTask(ordinaryCompletionToShow,
prefix,
!command && !doc.isEmpty(),
hasBoth);
}

CompletionTask allCompletion = new AllSuggestionsCompletionTask(suggestions, anchor[0]);

todo = new ArrayList<>();
@@ -567,17 +605,17 @@ public Result perform(String text, int cursor) throws IOException {
}

private final class OrdinaryCompletionTask implements CompletionTask {
private final List<Suggestion> suggestions;
private final int anchor;
private final List<? extends CharSequence> toShow;
private final String prefix;
private final boolean cont;
private final boolean showSmart;

public OrdinaryCompletionTask(List<Suggestion> suggestions,
int anchor,
public OrdinaryCompletionTask(List<? extends CharSequence> toShow,
String prefix,
boolean cont,
boolean showSmart) {
this.suggestions = suggestions;
this.anchor = anchor;
this.toShow = toShow;
this.prefix = prefix;
this.cont = cont;
this.showSmart = showSmart;
}
@@ -589,34 +627,7 @@ public String description() {

@Override
public Result perform(String text, int cursor) throws IOException {
List<? extends CharSequence> toShow;

if (showSmart) {
toShow =
suggestions.stream()
.filter(Suggestion::matchesType)
.map(Suggestion::continuation)
.distinct()
.toList();
} else {
toShow =
suggestions.stream()
.map(Suggestion::continuation)
.distinct()
.toList();
}

if (toShow.isEmpty()) {
return Result.CONTINUE;
}

Optional<String> prefix =
suggestions.stream()
.map(Suggestion::continuation)
.reduce(ConsoleIOContext::commonPrefix);

String prefixStr = prefix.orElse("").substring(cursor - anchor);
in.putString(prefixStr);
in.putString(prefix);

boolean showItems = toShow.size() > 1 || showSmart;

@@ -625,7 +636,7 @@ public Result perform(String text, int cursor) throws IOException {
printColumns(toShow);
}

if (!prefixStr.isEmpty())
if (!prefix.isEmpty())
return showItems ? Result.FINISH : Result.SKIP_NOREPAINT;

return cont ? Result.CONTINUE : Result.FINISH;
@@ -798,6 +809,19 @@ public Result perform(String text, int cursor) throws IOException {

}

private static class ContinueCompletionTask implements ConsoleIOContext.CompletionTask {

@Override
public String description() {
throw new UnsupportedOperationException("Should not get here.");
}

@Override
public CompletionTask.Result perform(String text, int cursor) throws IOException {
return CompletionTask.Result.CONTINUE;
}
}

@Override
public boolean terminalEditorRunning() {
Terminal terminal = in.getTerminal();
@@ -23,7 +23,7 @@

/**
* @test
* @bug 8177076 8185426 8189595 8188072 8221759
* @bug 8177076 8185426 8189595 8188072 8221759 8255273
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
@@ -331,4 +331,13 @@ private Path prepareZip() {
//where:
private final Compiler compiler = new Compiler();

public void testDocumentationAfterInsert() throws Exception {
doRunTest((inputSink, out) -> {
inputSink.write("import java.time.*\n");
waitOutput(out, PROMPT);

inputSink.write("new Instant" + TAB);
waitOutput(out, PROMPT + "new InstantiationE");
});
}
}

1 comment on commit 325eecb

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 325eecb Apr 15, 2021

Please sign in to comment.