public abstract class JShell
extends java.lang.Object
implements java.lang.AutoCloseable
JShell
instance holds the evolving compilation and
execution state. The state is changed with the instance methods
eval(String)
,
drop(PersistentSnippet)
and
addToClasspath(String)
.
The majority of methods query the state.
A JShell
instance also allows registering for events with
onSnippetEvent(Consumer)
and onShutdown(Consumer)
, which
are unregistered with
unsubscribe(Subscription)
.
Access to the source analysis utilities is via
sourceCodeAnalysis()
.
When complete the instance should be closed to free resources --
close()
.
An instance of JShell
is created with
JShell.create()
.
This class is not thread safe, except as noted, all access should be through a single thread.
jdk.jshell
Modifier and Type | Class and Description |
---|---|
static class |
JShell.Builder
Builder for
JShell instances. |
class |
JShell.Subscription
Subscription is a token for referring to subscriptions so they can
be unsubscribed.
|
Constructor and Description |
---|
JShell() |
Modifier and Type | Method and Description |
---|---|
abstract void |
addToClasspath(java.lang.String path)
The specified path is added to the end of the classpath used in eval().
|
static JShell.Builder |
builder()
Factory method for
JShell.Builder which, in-turn, is used
for creating instances of JShell . |
abstract void |
close()
Close this state engine.
|
static JShell |
create()
Create a new JShell state engine.
|
abstract java.util.List<javax.tools.Diagnostic<java.lang.String>> |
diagnostics(Snippet snippet)
Return the diagnostics of the most recent evaluation of the snippet.
|
abstract java.util.List<SnippetEvent> |
drop(PersistentSnippet snippet)
Remove a declaration from the state.
|
abstract java.util.List<SnippetEvent> |
eval(java.lang.String input)
Evaluate the input String, including definition and/or execution, if
applicable.
|
abstract java.util.List<MethodSnippet> |
methods()
Returns the active method snippets.
|
abstract JShell.Subscription |
onShutdown(java.util.function.Consumer<JShell> listener)
Register a callback to be called when this JShell instance terminates.
|
abstract JShell.Subscription |
onSnippetEvent(java.util.function.Consumer<SnippetEvent> listener)
Register a callback to be called when the Status of a snippet changes.
|
abstract java.util.List<Snippet> |
snippets()
Return all snippets.
|
abstract SourceCodeAnalysis |
sourceCodeAnalysis()
Access to source code analysis functionality.
|
abstract Snippet.Status |
status(Snippet snippet)
Return the status of the snippet.
|
abstract void |
stop()
Attempt to stop currently running evaluation.
|
abstract java.util.List<TypeDeclSnippet> |
types()
Returns the active type declaration (class, interface, annotation type, and enum) snippets.
|
abstract java.util.List<java.lang.String> |
unresolvedDependencies(DeclarationSnippet snippet)
For
RECOVERABLE_DEFINED or
RECOVERABLE_NOT_DEFINED
declarations, the names of current unresolved dependencies for
the snippet. |
abstract void |
unsubscribe(JShell.Subscription token)
Unsubscribe from a callback subscription.
|
abstract java.util.List<VarSnippet> |
variables()
Returns the active variable snippets.
|
abstract java.lang.String |
varValue(VarSnippet snippet)
Get the current value of a variable.
|
public static JShell create()
JShell
.
Equivalent to JShell.builder()
.build()
.
JShell
.public static JShell.Builder builder()
JShell.Builder
which, in-turn, is used
for creating instances of JShell
.
Create a default instance of JShell
with
JShell.builder().build()
. For more construction options
see JShell.Builder
.Builder
.JShell.Builder
public abstract SourceCodeAnalysis sourceCodeAnalysis()
JShell
will always return the same
SourceCodeAnalysis
instance from
sourceCodeAnalysis()
.SourceCodeAnalysis
which can be used for source analysis such as completion detection and
completion suggestions.public abstract java.util.List<SnippetEvent> eval(java.lang.String input) throws java.lang.IllegalStateException
SourceCodeAnalysis.analyzeCompletion(String)
.
For imports, the import is added. Classes, interfaces. methods, and variables are defined. The initializer of variables, statements, and expressions are executed. The modifiers public, protected, private, static, and final are not allowed on op-level declarations and are ignored with a warning. Synchronized, native, abstract, and default top-level methods are not allowed and are errors. If a previous definition of a declaration is overwritten then there will be an event showing its status changed to OVERWRITTEN, this will not occur for dropped, rejected, or already overwritten declarations.
The execution environment is out of process. If the evaluated code
causes the execution environment to terminate, this JShell
instance will be closed but the calling process and VM remain valid.
input
- The input String to evaluatejava.lang.IllegalStateException
- if this JShell
instance is closed.SourceCodeAnalysis.analyzeCompletion(String)
,
onShutdown(java.util.function.Consumer)
public abstract void stop()
eval(java.lang.String)
method is running and the
user's code being executed, an attempt will be made to stop user's code.
Note that typically this method needs to be called from a different thread
than the one running the eval
method.
If the eval(java.lang.String)
method is not running, does nothing.
The attempt to stop the user's code may fail in some case, which may include
when the execution is blocked on an I/O operation, or when the user's code is
catching the ThreadDeath
exception.
public abstract java.util.List<SnippetEvent> drop(PersistentSnippet snippet) throws java.lang.IllegalStateException
snippet
- The snippet to removejava.lang.IllegalStateException
- if this JShell
instance is closed.java.lang.IllegalArgumentException
- if the snippet is not associated with
this JShell
instance.public abstract java.lang.String varValue(VarSnippet snippet) throws java.lang.IllegalStateException
snippet
- the variable Snippet whose value is queried.java.lang.IllegalStateException
- if this JShell
instance is closed.java.lang.IllegalArgumentException
- if the snippet is not associated with
this JShell
instance.java.lang.IllegalArgumentException
- if the variable's status is anything but
Snippet.Status.VALID
.public abstract void addToClasspath(java.lang.String path)
path
- the path to add to the classpath.public abstract void close()
close
in interface java.lang.AutoCloseable
public abstract java.util.List<Snippet> snippets() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if this JShell instance is closed.public abstract java.util.List<VarSnippet> variables() throws java.lang.IllegalStateException
snippets()
filtered for
status(snippet).isActive
&& snippet.kind() == Kind.VARIABLE
and cast to VarSnippet
.java.lang.IllegalStateException
- if this JShell instance is closed.public abstract java.util.List<MethodSnippet> methods() throws java.lang.IllegalStateException
snippets()
filtered for
status(snippet).isActive
&& snippet.kind() == Kind.METHOD
and cast to MethodSnippet.java.lang.IllegalStateException
- if this JShell instance is closed.public abstract java.util.List<TypeDeclSnippet> types() throws java.lang.IllegalStateException
snippets()
filtered for
status(snippet).isActive
&& snippet.kind() == Kind.TYPE_DECL
and cast to TypeDeclSnippet.java.lang.IllegalStateException
- if this JShell instance is closed.public abstract Snippet.Status status(Snippet snippet)
eval()
call or
an automatic update triggered by a dependency.snippet
- the Snippet
to look upjava.lang.IllegalStateException
- if this JShell
instance is closed.java.lang.IllegalArgumentException
- if the snippet is not associated with
this JShell
instance.public abstract java.util.List<java.lang.String> unresolvedDependencies(DeclarationSnippet snippet)
RECOVERABLE_DEFINED
or
RECOVERABLE_NOT_DEFINED
declarations, the names of current unresolved dependencies for
the snippet.
The returned value of this method, for a given method may change when an
eval()
or drop()
of another snippet causes
an update of a dependency.snippet
- the declaration Snippet
to look upjava.lang.IllegalStateException
- if this JShell
instance is closed.java.lang.IllegalArgumentException
- if the snippet is not associated with
this JShell
instance.public abstract java.util.List<javax.tools.Diagnostic<java.lang.String>> diagnostics(Snippet snippet)
eval()
call or
an automatic update triggered by a dependency.snippet
- the Snippet
to look upunresolvedDependencies()
.java.lang.IllegalStateException
- if this JShell
instance is closed.java.lang.IllegalArgumentException
- if the snippet is not associated with
this JShell
instance.public abstract JShell.Subscription onSnippetEvent(java.util.function.Consumer<SnippetEvent> listener) throws java.lang.IllegalStateException
listener
- Action to perform when the Status changes.java.lang.IllegalStateException
- if this JShell
instance is closed.public abstract JShell.Subscription onShutdown(java.util.function.Consumer<JShell> listener) throws java.lang.IllegalStateException
listener
- Action to perform when the state terminates.java.lang.IllegalStateException
- if this JShell instance is closedpublic abstract void unsubscribe(JShell.Subscription token)
token
- The token corresponding to the subscription to be unsubscribed.