public abstract class SourceCodeAnalysis extends Object
Modifier and Type | Class | Description |
---|---|---|
static enum |
SourceCodeAnalysis.Attribute |
A span attribute which can be used to derive a coloring. |
static enum |
SourceCodeAnalysis.Completeness |
Describes the completeness of the given input. |
static interface |
SourceCodeAnalysis.CompletionInfo |
The result of analyzeCompletion(String input) . |
static interface |
SourceCodeAnalysis.Documentation |
A documentation for a candidate for continuation of the given user's input. |
static final record |
SourceCodeAnalysis.Highlight |
Assigns attributes usable for coloring to spans inside a snippet. |
static final class |
SourceCodeAnalysis.QualifiedNames |
List of possible qualified names. |
static interface |
SourceCodeAnalysis.SnippetWrapper |
The wrapping of a snippet of Java source into valid top-level Java source. |
static interface |
SourceCodeAnalysis.Suggestion |
A candidate for continuation of the given user's input. |
Modifier and Type | Method | Description |
---|---|---|
abstract SourceCodeAnalysis.CompletionInfo |
analyzeCompletion |
Given an input string, find the first snippet of code (one statement, definition, import, or expression) and evaluate if it is complete. |
abstract String |
analyzeType |
Infer the type of the given expression. |
abstract List |
completionSuggestions |
Compute possible follow-ups for the given input. |
abstract Collection |
dependents |
Returns a collection of Snippet s which might need updating if the given Snippet is updated. |
abstract List |
documentation |
Compute documentation for the given user's input. |
abstract List |
highlights |
Returns a collection of Highlight s which can be used to color the given snippet. |
abstract SourceCodeAnalysis.QualifiedNames |
listQualifiedNames |
List qualified names known for the simple name in the given code immediately to the left of the given cursor position. |
abstract List |
sourceToSnippets |
Converts the source code of a snippet into a Snippet object (or list of Snippet objects in the case of some var declarations, e.g.: int x, y, z;). |
abstract SourceCodeAnalysis.SnippetWrapper |
wrapper |
Returns the wrapper information for the Snippet . |
abstract List |
wrappers |
Returns the wrapper information for the snippet within the input source string. |
public abstract SourceCodeAnalysis.CompletionInfo analyzeCompletion(String input)
input
- the input source stringpublic abstract List<SourceCodeAnalysis.Suggestion> completionSuggestions(String input, int cursor, int[] anchor)
JShell
state, including type information, to filter the suggestions.input
- the user input, so farcursor
- the current position of the cursors in the given input
textanchor
- outgoing parameter - when an option will be completed, the text between the anchor and cursor will be deleted and replaced with the given optionpublic abstract List<SourceCodeAnalysis.Documentation> documentation(String input, int cursor, boolean computeJavadoc)
Documentation
objects may be returned when multiple elements match the user's input (like for overloaded methods).input
- the snippet the user wrote so farcursor
- the current position of the cursors in the given input
textcomputeJavadoc
- true if the javadoc for the given input should be computed in addition to the signatureDocumentation
objects are returned.public abstract String analyzeType(String code, int cursor)
code
to the given cursor
position. Returns null if the type of the expression cannot be inferred.code
- the expression for which the type should be inferredcursor
- current cursor position in the given codepublic abstract SourceCodeAnalysis.QualifiedNames listQualifiedNames(String code, int cursor)
JShell.addToClasspath(java.lang.String)
).code
- the expression for which the candidate qualified names should be computedcursor
- current cursor position in the given codepublic abstract SourceCodeAnalysis.SnippetWrapper wrapper(Snippet snippet)
Snippet
. The wrapper changes as the environment changes, so calls to this method at different times may yield different results.snippet
- the Snippet
from which to retrieve the wrapperpublic abstract List<SourceCodeAnalysis.SnippetWrapper> wrappers(String input)
Wrapper information for malformed and incomplete snippets also generate wrappers. The list is in snippet encounter order. The wrapper changes as the environment changes, so calls to this method at different times may yield different results.
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 analyzeCompletion(String)
.
The wrapper may not match that returned by wrapper(Snippet)
, were the source converted to a Snippet
.
input
- the source input from which to generate wrapperspublic abstract List<Snippet> sourceToSnippets(String input)
Snippet
object (or list of Snippet
objects in the case of some var declarations, e.g.: int x, y, z;). Does not install the snippets: declarations are not accessible by other snippets; imports are not added. Does not execute the snippets. Queries may be done on the Snippet
object. The Snippet.id()
will be "*UNASSOCIATED*"
. The returned snippets are not associated with the JShell
instance, so attempts to pass them to JShell
methods will throw an IllegalArgumentException
. They will not appear in queries for snippets -- for example, JShell.snippets()
.
Restrictions on the input are as in JShell.eval
.
Only preliminary compilation is performed, sufficient to build the Snippet
. Snippets known to be erroneous, are returned as ErroneousSnippet
, other snippets may or may not be in error.
input
- The input String to convertIllegalStateException
- if the JShell
instance is closed.public abstract Collection<Snippet> dependents(Snippet snippet)
Snippet
s which might need updating if the given Snippet
is updated. The returned collection is designed to be inclusive and may include many false positives.snippet
- the Snippet
whose dependents are requestedpublic abstract List<SourceCodeAnalysis.Highlight> highlights(String snippet)
Highlight
s which can be used to color the given snippet. The returned Highlight
s do not overlap, and are sorted by their start position.
snippet
- the snippet for which the Highlight
s should be computedHighlight
s.
© 1993, 2023, 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/21/docs/api/jdk.jshell/jdk/jshell/SourceCodeAnalysis.html