On this page
Class JShell
- All Implemented Interfaces:
-
AutoCloseable
public class JShell extends Object implements AutoCloseable
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.
- Since:
- 9
Nested Class Summary
Modifier and Type | Class | 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.
|
Method Summary
Modifier and Type | Method | Description |
---|---|---|
void |
addToClasspath |
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 .
|
void |
close() |
Close this state engine.
|
static JShell |
create() |
Create a new JShell state engine.
|
Stream<Diag> |
diagnostics |
Return the diagnostics of the most recent evaluation of the snippet.
|
List<SnippetEvent> |
drop |
Remove a declaration from the state.
|
List<SnippetEvent> |
eval |
Evaluate the input String, including definition and/or execution, if applicable.
|
Stream<ImportSnippet> |
imports() |
Returns the active import snippets.
|
Stream<MethodSnippet> |
methods() |
Returns the active method snippets.
|
JShell.Subscription |
onShutdown |
Register a callback to be called when this JShell instance terminates.
|
JShell.Subscription |
onSnippetEvent |
Register a callback to be called when the Status of a snippet changes.
|
Stream<Snippet> |
snippets() |
Return all snippets.
|
SourceCodeAnalysis |
sourceCodeAnalysis() |
Access to source code analysis functionality.
|
Snippet.Status |
status |
Return the status of the snippet.
|
void |
stop() |
Attempt to stop currently running evaluation.
|
Stream<TypeDeclSnippet> |
types() |
Returns the active type declaration (class, interface, annotation type, and enum) snippets.
|
Stream<String> |
unresolvedDependencies |
For RECOVERABLE_DEFINED or RECOVERABLE_NOT_DEFINED declarations, the names of current unresolved dependencies for the snippet.
|
void |
unsubscribe |
Cancel a callback subscription.
|
Stream<VarSnippet> |
variables() |
Returns the active variable snippets.
|
String |
varValue |
Get the current value of a variable.
|
Method Details
create
public static JShell create() throws IllegalStateException
JShell
.
Equivalent to JShell.builder()
.build()
.
- Returns:
-
an instance of
JShell
. - Throws:
IllegalStateException
- if theJShell
instance could not be created.
builder
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
.
- Returns:
-
an instance of
Builder
. - See Also:
sourceCodeAnalysis
public SourceCodeAnalysis sourceCodeAnalysis()
JShell
will always return the same SourceCodeAnalysis
instance from sourceCodeAnalysis()
.
- Returns:
-
an instance of
SourceCodeAnalysis
which can be used for source analysis such as completion detection and completion suggestions.
eval
public List<SnippetEvent> eval(String input) throws IllegalStateException
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.
- Parameters:
input
- The input String to evaluate- Returns:
- the list of events directly or indirectly caused by this evaluation.
- Throws:
IllegalStateException
- if thisJShell
instance is closed.- See Also:
drop
public List<SnippetEvent> drop(Snippet snippet) throws IllegalStateException
DROPPED
.
- Parameters:
snippet
- The snippet to remove- Returns:
- The list of events from updating declarations dependent on the dropped snippet.
- Throws:
IllegalStateException
- if thisJShell
instance is closed.IllegalArgumentException
- if the snippet is not associated with thisJShell
instance.
addToClasspath
public void addToClasspath(String path)
eval(String)
code is placed.
- Parameters:
path
- the path to add to the classpath.- Throws:
IllegalStateException
- if thisJShell
instance is closed.
stop
public 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.
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
snippets
public Stream<Snippet> snippets()
- Returns:
- the snippets for all current snippets in id order.
variables
public Stream<VarSnippet> variables()
snippets()
filtered for status(snippet).isActive()
&& snippet.kind() == Kind.VARIABLE
and cast to VarSnippet
.
- Returns:
- the active declared variables.
methods
public Stream<MethodSnippet> methods()
snippets()
filtered for status(snippet).isActive()
&& snippet.kind() == Kind.METHOD
and cast to MethodSnippet.
- Returns:
- the active declared methods.
types
public Stream<TypeDeclSnippet> types()
snippets()
filtered for status(snippet).isActive()
&& snippet.kind() == Kind.TYPE_DECL
and cast to TypeDeclSnippet.
- Returns:
- the active declared type declarations.
imports
public Stream<ImportSnippet> imports()
snippets()
filtered for status(snippet).isActive()
&& snippet.kind() == Kind.IMPORT
and cast to ImportSnippet.
- Returns:
- the active declared import declarations.
status
public Snippet.Status status(Snippet snippet)
eval()
call or an automatic update triggered by a dependency.
- Parameters:
snippet
- theSnippet
to look up- Returns:
- the status corresponding to this snippet
- Throws:
IllegalStateException
- if thisJShell
instance is closed.IllegalArgumentException
- if the snippet is not associated with thisJShell
instance.
diagnostics
public Stream<Diag> diagnostics(Snippet snippet)
eval()
call or an automatic update triggered by a dependency.
- Parameters:
snippet
- theSnippet
to look up- Returns:
-
the diagnostics corresponding to this snippet. This does not include unresolvedDependencies references reported in
unresolvedDependencies()
. - Throws:
IllegalStateException
- if thisJShell
instance is closed.IllegalArgumentException
- if the snippet is not associated with thisJShell
instance.
unresolvedDependencies
public Stream<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.
- Parameters:
snippet
- the declarationSnippet
to look up- Returns:
- a stream of symbol names that are currently unresolvedDependencies.
- Throws:
IllegalStateException
- if thisJShell
instance is closed.IllegalArgumentException
- if the snippet is not associated with thisJShell
instance.
varValue
public String varValue(VarSnippet snippet) throws IllegalStateException
- Parameters:
snippet
- the variable Snippet whose value is queried.- Returns:
- the current value of the variable referenced by snippet.
- Throws:
IllegalStateException
- if thisJShell
instance is closed.IllegalArgumentException
- if the snippet is not associated with thisJShell
instance.IllegalArgumentException
- if the variable's status is anything butSnippet.Status.VALID
.
onSnippetEvent
public JShell.Subscription onSnippetEvent(Consumer<SnippetEvent> listener) throws IllegalStateException
- Parameters:
listener
- Action to perform when the Status changes.- Returns:
- A token which can be used to unsubscribe this subscription.
- Throws:
IllegalStateException
- if thisJShell
instance is closed.
onShutdown
public JShell.Subscription onShutdown(Consumer<JShell> listener) throws IllegalStateException
- Parameters:
listener
- Action to perform when the state terminates.- Returns:
- A token which can be used to unsubscribe this subscription.
- Throws:
IllegalStateException
- if this JShell instance is closed
unsubscribe
public void unsubscribe(JShell.Subscription token)
- Parameters:
token
- The token corresponding to the subscription to be unsubscribed.
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/jdk.jshell/jdk/jshell/JShell.html