Class SuggestedFixData
java.lang.Object
org.checkerframework.framework.source.SuggestedFixData
A machine-applicable source edit that a
SourceChecker may attach to a finding, so a host
(such as the Error Prone plugin) can offer it as a suggested fix through its own fix / patch
pipeline.
A fix is a list of text replacements, each expressed with javac
source-offset positions (the same character offsets used by
com.sun.source.util.SourcePositions and com.sun.source.tree.EndPosTable). These are
JDK-neutral values: this class references no host-framework (in particular no Error Prone) type,
so the Checker Framework core stays framework-agnostic. Translating a SuggestedFixData
into a host-specific fix (e.g. an Error Prone SuggestedFix) is the host's responsibility.
Most Checker Framework findings do not carry a fix. A checker that can compute one attaches it
with DiagMessage.withFix(org.checkerframework.framework.source.SuggestedFixData); see NullnessNoInitVisitor for an example.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA single text replacement: replace the source between two offsets with new text. -
Constructor Summary
ConstructorsConstructorDescriptionSuggestedFixData(List<SuggestedFixData.Replacement> replacements) Creates a fix from the given replacements. -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable SuggestedFixDatadeleteTree(CompilationUnitTree root, Tree tree) Returns a fix that deletestreealong with any whitespace immediately following it.Returns the replacements that make up this fix.static @Nullable SuggestedFixDatamerge(List<? extends @Nullable SuggestedFixData> parts) Returns a fix that applies the replacements of allpartstogether, for a fix that is built from several edits, such as deleting an annotation in one place and inserting it in another.static SuggestedFixDataConvenience factory for a single-replacement fix.static @Nullable SuggestedFixDatareplaceTree(CompilationUnitTree root, Tree tree, String text) Returns a fix that replaces the source range oftreewithtext.static @Nullable CharSequenceReturns the source text of the given compilation unit, which javac source offsets index into.
-
Constructor Details
-
SuggestedFixData
Creates a fix from the given replacements.- Parameters:
replacements- the replacements that make up this fix
-
-
Method Details
-
getReplacements
Returns the replacements that make up this fix. The returned list is unmodifiable.- Returns:
- the replacements, applied together
-
replace
Convenience factory for a single-replacement fix.- Parameters:
startPosition- start source offset, inclusiveendPosition- end source offset, exclusivetext- the replacement text- Returns:
- a fix consisting of the one replacement
-
replaceTree
public static @Nullable SuggestedFixData replaceTree(CompilationUnitTree root, Tree tree, String text) Returns a fix that replaces the source range oftreewithtext. All arguments are JDK types, so a checker can build a fix without referencing any host framework.- Parameters:
root- the compilation unit containingtreetree- the tree whose source range to replacetext- the replacement text (empty to delete)- Returns:
- the fix, or
nulliftree's source position is unavailable
-
deleteTree
Returns a fix that deletestreealong with any whitespace immediately following it. Deleting the trailing whitespace avoids leaving a stray space behind: for example, deleting the@Nullableannotation from@Nullable int xleavesint xrather than a leading space beforeint. Uses the compilation unit's source text; all arguments are JDK types. If the source text is unavailable, onlytree's own source range is deleted.- Parameters:
root- the compilation unit containingtreetree- the tree to delete- Returns:
- the fix, or
nulliftree's source position is unavailable
-
merge
Returns a fix that applies the replacements of allpartstogether, for a fix that is built from several edits, such as deleting an annotation in one place and inserting it in another. Applying only some of the parts could change the meaning of the code, so if any part is null (because a factory such asdeleteTree(com.sun.source.tree.CompilationUnitTree, com.sun.source.tree.Tree)could not compute it), there is no fix.- Parameters:
parts- the fixes to combine- Returns:
- a fix with the replacements of all
parts, ornullif any part is null
-
sourceText
Returns the source text of the given compilation unit, which javac source offsets index into. A fix that copies text from the source (for example, to move an annotation elsewhere) reads it from here.- Parameters:
root- a compilation unit- Returns:
- its source text, or
nullif the source file or its content is unavailable or cannot be read
-