Class SuggestedFixData

java.lang.Object
org.checkerframework.framework.source.SuggestedFixData

public final class SuggestedFixData extends Object
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.

  • Constructor Details

    • SuggestedFixData

      public SuggestedFixData(List<SuggestedFixData.Replacement> replacements)
      Creates a fix from the given replacements.
      Parameters:
      replacements - the replacements that make up this fix
  • Method Details

    • getReplacements

      public List<SuggestedFixData.Replacement> getReplacements()
      Returns the replacements that make up this fix. The returned list is unmodifiable.
      Returns:
      the replacements, applied together
    • replace

      public static SuggestedFixData replace(int startPosition, int endPosition, String text)
      Convenience factory for a single-replacement fix.
      Parameters:
      startPosition - start source offset, inclusive
      endPosition - end source offset, exclusive
      text - 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 of tree with text. All arguments are JDK types, so a checker can build a fix without referencing any host framework.
      Parameters:
      root - the compilation unit containing tree
      tree - the tree whose source range to replace
      text - the replacement text (empty to delete)
      Returns:
      the fix, or null if tree's source position is unavailable
    • deleteTree

      public static @Nullable SuggestedFixData deleteTree(CompilationUnitTree root, Tree tree)
      Returns a fix that deletes tree along with any whitespace immediately following it. Deleting the trailing whitespace avoids leaving a stray space behind: for example, deleting the @Nullable annotation from @Nullable int x leaves int x rather than a leading space before int. Uses the compilation unit's source text; all arguments are JDK types. If the source text is unavailable, only tree's own source range is deleted.
      Parameters:
      root - the compilation unit containing tree
      tree - the tree to delete
      Returns:
      the fix, or null if tree's source position is unavailable
    • merge

      public static @Nullable SuggestedFixData merge(List<? extends @Nullable SuggestedFixData> parts)
      Returns a fix that applies the replacements of all parts together, 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 as deleteTree(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, or null if any part is null
    • sourceText

      public static @Nullable CharSequence sourceText(CompilationUnitTree root)
      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 null if the source file or its content is unavailable or cannot be read