AutoCloseable
The JShell evaluation state engine. This is the central class in the JShell API. A
JShell
instance holds the evolving compilation and execution state. The state is changed with the instance methods
eval(String)
,
drop(Snippet)
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.
Nested Classes
static class
Builder for JShell
instances.
class
Subscription is a token for referring to subscriptions so they can be
unsubscribed.
void
The specified path is added to the end of the classpath used in eval().
Factory method for JShell.Builder
which, in-turn, is used for creating instances of JShell
.
void
Create a new JShell state engine.
Return the diagnostics of the most recent evaluation of the snippet.
Remove a declaration from the state.
Evaluate the input String, including definition and/or execution, if applicable.
Returns the active import snippets.
Returns the active method snippets.
Register a callback to be called when this JShell instance terminates.
Register a callback to be called when the Status of a snippet changes.
Access to source code analysis functionality.
Return the status of the snippet.
void
Attempt to stop currently running evaluation.
Returns the active type declaration (class, interface, annotation type, and enum) snippets.
void
Cancel a callback subscription.
Returns the active variable snippets.
Get the current value of a variable.
Methods declared in class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
JShell
.
IllegalStateException
- if the JShell
instance could not be created.
Factory method for
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
.
Access to source code analysis functionality. An instance of 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.
Evaluate the input String, including definition and/or execution, if applicable. The input is checked for errors, unless the errors can be deferred (as is the case with some unresolvedDependencies references), errors will abort evaluation.
The input should be exactly one complete snippet of source code, that is, one expression, statement, variable declaration, method declaration, class declaration, or import. To break arbitrary input into individual complete snippets, use 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.
If execution environment is out of process, as is the default case, then 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 evaluate
IllegalStateException
- if this JShell
instance is closed.
Remove a declaration from the state. That is, if the snippet is an
active persistentsnippet, remove the snippet and update the JShell evaluation state accordingly. For all active snippets, change the
statusto
DROPPED
.
snippet
- The snippet to remove
IllegalStateException
- if this JShell
instance is closed.
IllegalArgumentException
- if the snippet is not associated with this JShell
instance.
The specified path is added to the end of the classpath used in eval(). Note that the unnamed package is not accessible from the package in which
eval(String)
code is placed.
path
- the path to add to the classpath.
IllegalStateException
- if this JShell
instance is closed.
public void stop()
Attempt to stop currently running evaluation. When called while the
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 void close()
Close this state engine. Frees resources. Should be called when this state engine is no longer needed.
close
in interface AutoCloseable
Return all snippets.
Returns the active variable snippets. This convenience method is equivalent to
snippets()
filtered for
status(snippet).isActive()
&& snippet.kind() == Kind.VARIABLE
and cast to
VarSnippet
.
Returns the active method snippets. This convenience method is equivalent to
snippets()
filtered for
status(snippet).isActive()
&& snippet.kind() == Kind.METHOD
and cast to MethodSnippet.
Returns the active type declaration (class, interface, annotation type, and enum) snippets. This convenience method is equivalent to
snippets()
filtered for
status(snippet).isActive()
&& snippet.kind() == Kind.TYPE_DECL
and cast to TypeDeclSnippet.
Returns the active import snippets. This convenience method is equivalent to
snippets()
filtered for
status(snippet).isActive()
&& snippet.kind() == Kind.IMPORT
and cast to ImportSnippet.
Return the status of the snippet. This is updated either because of an explicit eval()
call or an automatic update triggered by a dependency.
snippet
- the Snippet
to look up
IllegalStateException
- if this JShell
instance is closed.
IllegalArgumentException
- if the snippet is not associated with this JShell
instance.
Return the diagnostics of the most recent evaluation of the snippet. The evaluation can either because of an explicit eval()
call or an automatic update triggered by a dependency.
snippet
- the Snippet
to look up
unresolvedDependencies()
.
IllegalStateException
- if this JShell
instance is closed.
IllegalArgumentException
- if the snippet is not associated with this JShell
instance.
For
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 up
IllegalStateException
- if this JShell
instance is closed.
IllegalArgumentException
- if the snippet is not associated with this JShell
instance.
Get the current value of a variable.
snippet
- the variable Snippet whose value is queried.
IllegalStateException
- if this JShell
instance is closed.
IllegalArgumentException
- if the snippet is not associated with this JShell
instance.
IllegalArgumentException
- if the variable's status is anything but Snippet.Status.VALID
.
Register a callback to be called when the Status of a snippet changes. Each call adds a new subscription.
listener
- Action to perform when the Status changes.
IllegalStateException
- if this JShell
instance is closed.
Register a callback to be called when this JShell instance terminates. This occurs either because the client process has ended (e.g. called System.exit(0)) or the connection has been shutdown, as by close(). Each call adds a new subscription.
listener
- Action to perform when the state terminates.
IllegalStateException
- if this JShell instance is closed
Cancel a callback subscription.
token
- The token corresponding to the subscription to be unsubscribed.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4