SourceCodeAnalysis (Java SE 19 & JDK 19) (original) (raw)
public abstract class SourceCodeAnalysis extends Object
Provides analysis utilities for source code input. Optional functionality that provides for a richer interactive experience. Includes completion analysis: Is the input a complete snippet of code? Do I need to prompt for more input? Would adding a semicolon make it complete? Is there more than one snippet? etc. Also includes completion suggestions, as might be used in tab-completion.
Since:
9
Nested Class Summary
Nested Classesstatic enum
A span attribute which can be used to derive a coloring.static enum
Describes the completeness of the given input.static interface
The result of analyzeCompletion(String input)
.static interface
A documentation for a candidate for continuation of the given user's input.static final record
Assigns attributes usable for coloring to spans inside a snippet.static final class
List of possible qualified names.static interface
The wrapping of a snippet of Java source into valid top-level Java source.static interface
A candidate for continuation of the given user's input.
Method Summary
Given an input string, find the first snippet of code (one statement, definition, import, or expression) and evaluate if it is complete.
Infer the type of the given expression.[completionSuggestions](#completionSuggestions%28java.lang.String,int,int%5B%5D%29)([String](../../../java.base/java/lang/String.html "class in java.lang") input, int cursor, int[] anchor)
Compute possible follow-ups for the given input.
Returns a collection of Snippet
s which might need updating if the given Snippet
is updated.[documentation](#documentation%28java.lang.String,int,boolean%29)([String](../../../java.base/java/lang/String.html "class in java.lang") input, int cursor, boolean computeJavadoc)
Compute documentation for the given user's input.
Returns a collection of Highlight
s which can be used to color the given snippet.
List qualified names known for the simple name in the given code immediately to the left of the given cursor position.
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;).
Returns the wrapper information for the Snippet
.
Returns the wrapper information for the snippet within the input source string.
Methods declared in class java.lang.Object
[clone](../../../java.base/java/lang/Object.html#clone%28%29), [equals](../../../java.base/java/lang/Object.html#equals%28java.lang.Object%29), [finalize](../../../java.base/java/lang/Object.html#finalize%28%29), [getClass](../../../java.base/java/lang/Object.html#getClass%28%29), [hashCode](../../../java.base/java/lang/Object.html#hashCode%28%29), [notify](../../../java.base/java/lang/Object.html#notify%28%29), [notifyAll](../../../java.base/java/lang/Object.html#notifyAll%28%29), [toString](../../../java.base/java/lang/Object.html#toString%28%29), [wait](../../../java.base/java/lang/Object.html#wait%28%29), [wait](../../../java.base/java/lang/Object.html#wait%28long%29), [wait](../../../java.base/java/lang/Object.html#wait%28long,int%29)
Method Details
analyzeCompletion
Given an input string, find the first snippet of code (one statement, definition, import, or expression) and evaluate if it is complete.
Parameters:
input
- the input source string
Returns:
a CompletionInfo instance with location and completeness infocompletionSuggestions
Compute possible follow-ups for the given input. Uses information from the current
JShell
state, including type information, to filter the suggestions.
Parameters:
input
- the user input, so far
cursor
- the current position of the cursors in the giveninput
text
anchor
- outgoing parameter - when an option will be completed, the text between the anchor and cursor will be deleted and replaced with the given option
Returns:
list of candidate continuations of the given input.documentation
Compute documentation for the given user's input. Multiple
Documentation
objects may be returned when multiple elements match the user's input (like for overloaded methods).
Parameters:
input
- the snippet the user wrote so far
cursor
- the current position of the cursors in the giveninput
text
computeJavadoc
- true if the javadoc for the given input should be computed in addition to the signature
Returns:
the documentations for the given user's input, if multiple elements match the input, multipleDocumentation
objects are returned.analyzeType
public abstract String analyzeType(String code, int cursor)
Infer the type of the given expression. The expression spans from the beginning ofcode
to the givencursor
position. Returns null if the type of the expression cannot be inferred.
Parameters:
code
- the expression for which the type should be inferred
cursor
- current cursor position in the given code
Returns:
the inferred type, or null if it cannot be inferredlistQualifiedNames
List qualified names known for the simple name in the given code immediately to the left of the given cursor position. The qualified names are gathered by inspecting the classpath used by eval (see JShell.addToClasspath(java.lang.String)).
Parameters:
code
- the expression for which the candidate qualified names should be computed
cursor
- current cursor position in the given code
Returns:
the known qualified nameswrapper
Returns the wrapper information for the
Snippet
. The wrapper changes as the environment changes, so calls to this method at different times may yield different results.
Parameters:
snippet
- theSnippet
from which to retrieve the wrapper
Returns:
information on the wrapperwrappers
Returns the wrapper information for the snippet within the input source string.
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, useanalyzeCompletion(String).
The wrapper may not match that returned bywrapper(Snippet), were the source converted to aSnippet
.
Parameters:
input
- the source input from which to generate wrappers
Returns:
a list of wrapper informationsourceToSnippets
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;). 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 theSnippet
object. The Snippet.id() will be"*UNASSOCIATED*"
. The returned snippets are not associated with theJShell instance, so attempts to pass them toJShell
methods will throw anIllegalArgumentException
. They will not appear in queries for snippets -- for example, JShell.snippets().
Restrictions on the input are as inJShell.eval
.
Only preliminary compilation is performed, sufficient to build theSnippet
. Snippets known to be erroneous, are returned asErroneousSnippet, other snippets may or may not be in error.
Parameters:
input
- The input String to convert
Returns:
usually a singleton list of Snippet, but may be empty or multiple
Throws:
[IllegalStateException](../../../java.base/java/lang/IllegalStateException.html "class in java.lang")
- if theJShell
instance is closed.dependents
Returns a collection of
Snippet
s which might need updating if the givenSnippet
is updated. The returned collection is designed to be inclusive and may include many false positives.
Parameters:
snippet
- theSnippet
whose dependents are requested
Returns:
the collection of dependentshighlights
Returns a collection of
Highlight
s which can be used to color the given snippet.
The returnedHighlight
s do not overlap, and are sorted by their start position.
Parameters:
snippet
- the snippet for which theHighlight
s should be computed
Returns:
the computedHighlight
s.
Since:
19