Class JShell.Builder
- Enclosing class:
JShell
JShell instances.
Create custom instances of JShell by using the setter
methods on this class. After zero or more of these, use the
build() method to create a JShell instance.
These can all be chained. For example, setting the remote output and
error streams:
JShell myShell =
JShell.builder()
.out(myOutStream)
.err(myErrStream)
.build();
If no special set-up is needed, just use
JShell.builder().build() or the short-cut equivalent
JShell.create().- Since:
- 9
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds a JShell state engine.compilerOptions(String... options) Adds compiler options.console(JShellConsole console) Sets the console for the running evalution.err(PrintStream err) Sets the error output for the running evaluation (it'sSystem.err).executionEngine(String executionControlSpec) Sets the custom engine for execution.executionEngine(ExecutionControlProvider executionControlProvider, Map<String, String> executionControlParameters) Sets the custom engine for execution.Configure theFileManagerto be used by compilation and source analysis.idGenerator(BiFunction<Snippet, Integer, String> generator) Sets the generator of identifying names for Snippets.in(InputStream in) Sets the input for the running evaluation (it'sSystem.in).out(PrintStream out) Sets the output for the running evaluation (it'sSystem.out).remoteVMOptions(String... options) Sets additional VM options for launching the VM.tempVariableNameGenerator(Supplier<String> generator) Sets a generator of temp variable names forVarSnippetofSnippet.SubKind.TEMP_VAR_EXPRESSION_SUBKIND.
-
Method Details
-
in
Sets the input for the running evaluation (it'sSystem.in). Note: applications that useSystem.infor snippet or other user input cannot useSystem.inas the input stream for the remote process.The
readmethod of theInputStreammay throw theInterruptedIOExceptionto signal the user canceled the input. The currently running snippet will be automaticallystopped.The default, if this is not set, is to provide an empty input stream --
new ByteArrayInputStream(new byte[0]).- Parameters:
in- theInputStreamto be channelled toSystem.inin the remote execution process- Returns:
- the
Builderinstance (for use in chained initialization)
-
out
Sets the output for the running evaluation (it'sSystem.out). The controlling process and the remote process can shareSystem.out.The default, if this is not set, is
System.out.- Parameters:
out- thePrintStreamto be channelled toSystem.outin the remote execution process- Returns:
- the
Builderinstance (for use in chained initialization)
-
err
Sets the error output for the running evaluation (it'sSystem.err). The controlling process and the remote process can shareSystem.err.The default, if this is not set, is
System.err.- Parameters:
err- thePrintStreamto be channelled toSystem.errin the remote execution process- Returns:
- the
Builderinstance (for use in chained initialization)
-
console
Sets the console for the running evalution.The default, if this is not set, is no console (
System.console()will returnnullwhile running a snippet).- Parameters:
console- console to use while a snippet is run- Returns:
- the
Builderinstance (for use in chained initialization) - Since:
- 21
-
tempVariableNameGenerator
Sets a generator of temp variable names forVarSnippetofSnippet.SubKind.TEMP_VAR_EXPRESSION_SUBKIND.Do not use this method unless you have explicit need for it.
The generator will be used for newly created VarSnippet instances. The name of a variable is queried with
PersistentSnippet.name().The callback is sent during the processing of the snippet, the JShell state is not stable. No calls whatsoever on the
JShellinstance may be made from the callback.The generated name must be unique within active snippets.
The default behavior (if this is not set or
generatoris null) is to generate the name as a sequential number with a prefixing dollar sign ("$").- Parameters:
generator- theSupplierto generate the temporary variable name string ornull- Returns:
- the
Builderinstance (for use in chained initialization)
-
idGenerator
Sets the generator of identifying names for Snippets.Do not use this method unless you have explicit need for it.
The generator will be used for newly created Snippet instances. The identifying name (id) is accessed with
Snippet.id()and can be seen in theStackTraceElement.getFileName()for aEvalExceptionandUnresolvedReferenceException.The inputs to the generator are the
Snippetand an integer. The integer will be the same for two Snippets which would overwrite one-another, but otherwise is unique.The callback is sent during the processing of the snippet and the Snippet and the state as a whole are not stable. No calls to change system state (including Snippet state) should be made. Queries of Snippet may be made except to
Snippet.id(). No calls on theJShellinstance may be made from the callback, except tostatus(Snippet).The default behavior (if this is not set or
generatoris null) is to generate the id as the integer converted to a string.- Parameters:
generator- theBiFunctionto generate the id string ornull- Returns:
- the
Builderinstance (for use in chained initialization)
-
remoteVMOptions
Sets additional VM options for launching the VM.- Parameters:
options- The options for the remote VM- Returns:
- the
Builderinstance (for use in chained initialization)
-
compilerOptions
Adds compiler options. These additional options will be used on parsing, analysis, and code generation calls to the compiler. Options which interfere with results are not supported and have undefined effects on JShell's operation.- Parameters:
options- the addition options for compiler invocations- Returns:
- the
Builderinstance (for use in chained initialization)
-
executionEngine
Sets the custom engine for execution. Snippet execution will be provided by theExecutionControlinstance selected by the specified execution control spec. Use, at most, one of these overloadedexecutionEnginebuilder methods.- Parameters:
executionControlSpec- the execution control spec, which is documented in thejdk.jshell.spipackage documentation.- Returns:
- the
Builderinstance (for use in chained initialization)
-
executionEngine
public JShell.Builder executionEngine(ExecutionControlProvider executionControlProvider, Map<String, String> executionControlParameters) Sets the custom engine for execution. Snippet execution will be provided by the specifiedExecutionControlinstance. Use, at most, one of these overloadedexecutionEnginebuilder methods.- Parameters:
executionControlProvider- the provider to supply the execution engineexecutionControlParameters- the parameters to the provider, ornullfor default parameters- Returns:
- the
Builderinstance (for use in chained initialization)
-
fileManager
public JShell.Builder fileManager(Function<StandardJavaFileManager, StandardJavaFileManager> mapping) Configure theFileManagerto be used by compilation and source analysis. If not set or passed null, the compiler's standard file manager will be used (identity mapping). For use in special applications where the compiler's normal file handling needs to be overridden. See the file manager APIs for more information. The file manager input enables forwarding file managers, if this is not needed, the incoming file manager can be ignored (constant function).- Parameters:
mapping- a function that given the compiler's standard file manager, returns a file manager to use- Returns:
- the
Builderinstance (for use in chained initialization)
-
build
Builds a JShell state engine. This is the entry-point to all JShell functionality. This creates a remote process for execution. It is thus important to close the returned instance.- Returns:
- the state engine
- Throws:
IllegalStateException- if theJShellinstance could not be created.
-