Skip to content
Permalink
Browse files
8263411: Convert jshell tool to use Stream.toList()
Reviewed-by: jlahoda
  • Loading branch information
Ian Graves authored and Jan Lahoda committed Mar 24, 2021
1 parent 06d46d6 commit fad8484058f6ab1d9df655ace995b9b343cd3aef
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 74 deletions.
@@ -32,7 +32,6 @@
import java.util.Map;
import java.util.Map.Entry;

import static java.util.stream.Collectors.toList;

/**
* Parse command arguments, derived from StreamTokenizer by
@@ -102,7 +101,7 @@ private void foundOption(String opt) {
options.entrySet()
.stream()
.filter(e -> e.getKey().startsWith(opt))
.collect(toList());
.toList();
if (matches.size() == 1) {
matches.get(0).setValue(true);
} else {
@@ -247,8 +247,8 @@ public boolean interactiveOutput() {
public Iterable<String> history(boolean currentSession) {
return StreamSupport.stream(getHistory().spliterator(), false)
.filter(entry -> !currentSession || !historyLoad.equals(entry.time()))
.map(entry -> entry.line())
.collect(Collectors.toList());
.map(History.Entry::line)
.toList();
}

@Override
@@ -263,7 +263,7 @@ public void close() throws IOException {
StreamSupport.stream(in.getHistory().spliterator(), false)
.map(History.Entry::line)
.flatMap(this::toSplitEntries)
.collect(Collectors.toList());
.toList();
if (!savedHistory.isEmpty()) {
int len = (int) Math.ceil(Math.log10(savedHistory.size()+1));
String format = HISTORY_LINE_PREFIX + "%0" + len + "d";
@@ -354,7 +354,7 @@ private boolean complete(CompletionState completionState) {
doc = repl.analysis.documentation(prefix + text, cursor + prefix.length(), false)
.stream()
.map(Documentation::signature)
.collect(Collectors.toList());
.toList();
}
long smartCount = suggestions.stream().filter(Suggestion::matchesType).count();
boolean hasSmart = smartCount > 0 && smartCount <= /*in.getAutoprintThreshold()*/AUTOPRINT_THRESHOLD;
@@ -589,21 +589,21 @@ public String description() {

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

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

if (toShow.isEmpty()) {
@@ -658,7 +658,7 @@ public Result perform(String text, int cursor) throws IOException {
suggestions.stream()
.map(Suggestion::continuation)
.distinct()
.collect(Collectors.toList());
.toList();

Optional<String> prefix =
candidates.stream()
@@ -792,7 +792,7 @@ public Result perform(String text, int cursor) throws IOException {
List<String> doc = repl.analysis.documentation(prefix + text, cursor + prefix.length(), true)
.stream()
.map(convertor)
.collect(Collectors.toList());
.toList();
return doPrintFullDocumentation(todo, doc, false);
}

@@ -30,7 +30,6 @@
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Supplier;
import static java.util.stream.Collectors.toList;
import java.util.stream.Stream;
import jdk.internal.jshell.tool.JShellTool.CompletionProvider;
import jdk.jshell.SourceCodeAnalysis;
@@ -79,7 +78,7 @@ public List<Suggestion> completionSuggestions(String input, int cursor, int[] an
List<CompletionProvider> candidates = wordCompletionProvider.entrySet().stream()
.filter(e -> matcher.test(e.getKey(), word))
.map(Map.Entry::getValue)
.collect(toList());
.toList();
if (candidates.size() == 1) {
result = candidates.get(0).completionSuggestions(rest, cursor - space - 1, anchor).stream();
} else {
@@ -89,7 +88,7 @@ public List<Suggestion> completionSuggestions(String input, int cursor, int[] an
}

return result.sorted(comparing(Suggestion::continuation))
.collect(toList());
.toList();
}

}
@@ -121,7 +121,6 @@
import static java.util.Arrays.stream;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static jdk.jshell.Snippet.SubKind.TEMP_VAR_EXPRESSION_SUBKIND;
import static jdk.jshell.Snippet.SubKind.VAR_VALUE_SUBKIND;
import static java.util.stream.Collectors.toMap;
@@ -407,7 +406,7 @@ private Collection<String> validPaths(Collection<String> vals, String context, b
if (failed) {
return Collections.emptyList();
} else {
return result.collect(toList());
return result.toList();
}
}

@@ -458,7 +457,7 @@ Options parse(OptionSet options) {
addOptions(OptionKind.ADD_MODULES, options.valuesOf(argAddModules));
addOptions(OptionKind.ADD_EXPORTS, options.valuesOf(argAddExports).stream()
.map(mp -> mp.contains("=") ? mp : mp + "=ALL-UNNAMED")
.collect(toList())
.toList()
);
if (options.has(argEnablePreview)) {
opts.addAll(OptionKind.ENABLE_PREVIEW, List.of(
@@ -486,7 +485,7 @@ void addOptions(OptionKind kind, Collection<String> vals) {
if (kind.passFlag) {
vals = vals.stream()
.flatMap(mp -> Stream.of(kind.optionFlag, mp))
.collect(toList());
.toList();
}
opts.addAll(kind, vals);
}
@@ -1590,8 +1589,8 @@ private CompletionProvider snippetCompletion(Supplier<Stream<? extends Snippet>>
? Stream.of(String.valueOf(k.id()) + " ", ((DeclarationSnippet) k).name() + " ")
: Stream.of(String.valueOf(k.id()) + " "))
.filter(k -> k.startsWith(argPrefix))
.map(ArgSuggestion::new)
.collect(Collectors.toList());
.<Suggestion>map(ArgSuggestion::new)
.toList();
};
}

@@ -1659,8 +1658,8 @@ private static CompletionProvider optionCompletion(CompletionProvider provider)
String flag = ovm.group("flag");
List<CompletionProvider> ps = ARG_OPTIONS.entrySet().stream()
.filter(es -> es.getKey().startsWith(flag))
.map(es -> es.getValue())
.collect(toList());
.map(Map.Entry::getValue)
.toList();
if (ps.size() == 1) {
int pastSpace = ovm.start("val");
List<Suggestion> result = ps.get(0).completionSuggestions(
@@ -1676,7 +1675,7 @@ private static CompletionProvider optionCompletion(CompletionProvider provider)
om.group("flag"), cursor - pastSpace, anchor);
if (!om.group("dd").isEmpty()) {
result = result.stream()
.map(sug -> new Suggestion() {
.<Suggestion>map(sug -> new Suggestion() {
@Override
public String continuation() {
return "-" + sug.continuation();
@@ -1687,7 +1686,7 @@ public boolean matchesType() {
return false;
}
})
.collect(toList());
.toList();
--pastSpace;
}
anchor[0] += pastSpace;
@@ -1920,15 +1919,15 @@ public List<String> commandDocumentation(String code, int cursor, boolean shortD
String prefix = space != (-1) ? stripped.substring(0, space) : stripped;
List<String> result = new ArrayList<>();

List<Entry<String, String>> toShow;
List<? extends Entry<String, String>> toShow;

if (SET_SUB.matcher(stripped).matches()) {
String setSubcommand = stripped.replaceFirst("/?set ([^ ]*)($| .*)", "$1");
toShow =
Arrays.stream(SET_SUBCOMMANDS)
.filter(s -> s.startsWith(setSubcommand))
.map(s -> new SimpleEntry<>("/set " + s, "help.set." + s))
.collect(toList());
.toList();
} else if (RERUN_ID.matcher(stripped).matches()) {
toShow =
singletonList(new SimpleEntry<>("/<id>", "help.rerun"));
@@ -1945,7 +1944,7 @@ public List<String> commandDocumentation(String code, int cursor, boolean shortD
|| (inHelp && c.kind == CommandKind.HELP_SUBJECT))
.sorted((c1, c2) -> c1.command.compareTo(c2.command))
.map(c -> new SimpleEntry<>(c.command, c.helpKey))
.collect(toList());
.toList();
}

if (toShow.size() == 1 && !inHelp) {
@@ -2367,7 +2366,7 @@ private boolean cmdExit(String arg) {
Snippet sn = e.snippet();

// Show any diagnostics
List<Diag> diagnostics = state.diagnostics(sn).collect(toList());
List<Diag> diagnostics = state.diagnostics(sn).toList();
String source = sn.source();
displayDiagnostics(source, diagnostics);

@@ -2769,7 +2768,7 @@ void initIdMapping() {
if (allIds == null) {
allSnippets = snippetSupplier.get()
.sorted((a, b) -> order(a) - order(b))
.collect(toList());
.toList();
allIds = allSnippets.stream()
.map(sn -> sn.id())
.toArray(n -> new String[n]);
@@ -3394,7 +3393,7 @@ private boolean cmdImports() {
}

private boolean cmdUseHistoryEntry(int index) {
List<Snippet> keys = state.snippets().collect(toList());
List<Snippet> keys = state.snippets().toList();
if (index < 0)
index += keys.size();
else
@@ -3455,7 +3454,7 @@ private void rerunSnippet(Snippet snippet) {
List<Diag> errorsOnly(List<Diag> diagnostics) {
return diagnostics.stream()
.filter(Diag::isError)
.collect(toList());
.toList();
}

/**
@@ -3633,7 +3632,7 @@ private boolean handleEvent(SnippetEvent ste) {
debug("Event with null key: %s", ste);
return false;
}
List<Diag> diagnostics = state.diagnostics(sn).collect(toList());
List<Diag> diagnostics = state.diagnostics(sn).toList();
String source = sn.source();
if (ste.causeSnippet() == null) {
// main event
@@ -3742,7 +3741,7 @@ private FormatAction toAction(Status status, Status previousStatus, boolean isSi
}

void printSnippetStatus(DeclarationSnippet sn, boolean resolve) {
List<Diag> otherErrors = errorsOnly(state.diagnostics(sn).collect(toList()));
List<Diag> otherErrors = errorsOnly(state.diagnostics(sn).toList());
new DisplayEvent(sn, state.status(sn), resolve, otherErrors)
.displayDeclarationAndValue();
}
@@ -3811,7 +3810,7 @@ private DisplayEvent(Snippet sn, Status status, boolean resolve,
}

private String unresolved(DeclarationSnippet key) {
List<String> unr = state.unresolvedDependencies(key).collect(toList());
List<String> unr = state.unresolvedDependencies(key).toList();
StringBuilder sb = new StringBuilder();
int fromLast = unr.size();
if (fromLast > 0) {
@@ -31,7 +31,6 @@
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
* Selector is the representation of the selector in a "/set format" command. This class, among other things, provides
@@ -36,7 +36,6 @@
import java.util.List;
import java.util.Objects;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static jdk.internal.jshell.tool.JShellTool.RECORD_SEPARATOR;
import static jdk.internal.jshell.tool.JShellTool.getResource;
import static jdk.internal.jshell.tool.JShellTool.readResource;
@@ -276,7 +275,7 @@ static Startup unpack(String storedForm, MessageHandler mh) {
static Startup fromFileList(List<String> fns, String context, MessageHandler mh) {
List<StartupEntry> entries = fns.stream()
.map(fn -> readFile(fn, context, mh))
.collect(toList());
.toList();
if (entries.stream().anyMatch(sue -> sue == null)) {
return null;
}
@@ -71,7 +71,6 @@
import jdk.jshell.spi.ExecutionControl.ResolutionException;
import jdk.jshell.spi.ExecutionControl.RunException;
import jdk.jshell.spi.ExecutionControl.UserException;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static java.util.Collections.singletonList;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
@@ -1005,11 +1004,11 @@ private List<SnippetEvent> events(Unit c, Collection<Unit> outs, String value, J
.filter(u -> u != c)
.map(u -> u.event(null, null))
.filter(this::interestingEvent)
.collect(Collectors.toList()));
.toList());
events.addAll(outs.stream()
.flatMap(u -> u.secondaryEvents().stream())
.filter(this::interestingEvent)
.collect(Collectors.toList()));
.toList());
//System.err.printf("Events: %s\n", events);
return events;
}
@@ -1052,7 +1051,7 @@ private Set<Unit> compileAndLoad(Set<Unit> ins) {
while (true) {
List<Unit> legit = ins.stream()
.filter(Unit::isDefined)
.collect(toList());
.toList();
state.debug(DBG_GEN, "compileAndLoad ins = %s -- legit = %s\n",
ins, legit);
if (legit.isEmpty()) {
@@ -1084,7 +1083,7 @@ private Set<Unit> compileAndLoad(Set<Unit> ins) {
// attempt to redefine the remaining classes
List<Unit> toReplace = legit.stream()
.filter(u -> !u.doRedefines())
.collect(toList());
.toList();

// prevent alternating redefine/replace cyclic dependency
// loop by replacing all that have been replaced
@@ -1111,7 +1110,7 @@ private Set<Unit> compileAndLoad(Set<Unit> ins) {
// add any new dependencies to the working set
List<Unit> newDependencies = ins.stream()
.flatMap(Unit::effectedDependents)
.collect(toList());
.toList();
state.debug(DBG_GEN, "compileAndLoad %s -- deps: %s success: %s\n",
ins, newDependencies, success);
if (!ins.addAll(newDependencies) && success) {
@@ -164,23 +164,20 @@ String fullClassNameAndPackageToClass(String full, String pkg) {
List<String> klasses = importSnippets()
.filter(isi -> !isi.isStar)
.map(isi -> isi.fullname)
.collect(toList());
.toList();
for (String k : klasses) {
if (k.equals(full)) {
return full.substring(full.lastIndexOf(".")+1, full.length());
}
}
List<String> pkgs = importSnippets()
Stream<String> pkgs = importSnippets()
.filter(isi -> isi.isStar)
.map(isi -> isi.fullname.substring(0, isi.fullname.lastIndexOf(".")))
.collect(toList());
pkgs.add(0, "java.lang");
for (String ipkg : pkgs) {
if (!ipkg.isEmpty() && ipkg.equals(pkg)) {
return full.substring(pkg.length() + 1);
}
}
return full;
.map(isi -> isi.fullname.substring(0, isi.fullname.lastIndexOf(".")));
return Stream.concat(Stream.of("java.lang"), pkgs)
.filter(ipkg -> !ipkg.isEmpty() && ipkg.equals(pkg))
.map(ipkg -> full.substring(pkg.length() + 1))
.findFirst()
.orElse(full);
}

/**

0 comments on commit fad8484

Please sign in to comment.