Class SuggestedFixes

java.lang.Object
com.google.errorprone.fixes.SuggestedFixes

public final class SuggestedFixes extends Object
Factories for constructing Fixes.
  • Method Details

    • addModifiers

      public static Optional<SuggestedFix> addModifiers(com.sun.source.tree.Tree tree, VisitorState state, Modifier... modifiers)
      Adds modifiers to the given class, method, or field declaration.
    • addModifiers

      public static Optional<SuggestedFix> addModifiers(com.sun.source.tree.Tree tree, com.sun.source.tree.ModifiersTree originalModifiers, VisitorState state, Set<Modifier> modifiers)
      Adds modifiers to the given declaration and corresponding modifiers tree.
    • removeModifiers

      public static Optional<SuggestedFix> removeModifiers(com.sun.source.tree.Tree tree, VisitorState state, Modifier... modifiers)
      Removes modifiers from the given class, method, or field declaration.
    • removeModifiers

      public static Optional<SuggestedFix> removeModifiers(com.sun.source.tree.ModifiersTree originalModifiers, VisitorState state, Set<Modifier> toRemove)
      Removes modifiers to the given declaration and corresponding modifiers tree.
    • qualifyType

      public static String qualifyType(VisitorState state, SuggestedFix.Builder fix, com.sun.tools.javac.code.Symbol sym)
      Returns a human-friendly name of the given Symbol for use in fixes.
      • If the symbol is already in scope, its simple name is used.
      • If the symbol is a Symbol.TypeSymbol and an enclosing type is imported, that enclosing type is used as a qualifier.
      • Otherwise the outermost enclosing type is imported and used as a qualifier.
    • qualifyType

      public static String qualifyType(VisitorState state, SuggestedFix.Builder fix, TypeMirror type)
      Returns a human-friendly name of the given type for use in fixes.
    • qualifyType

      public static String qualifyType(VisitorState state, SuggestedFix.Builder fix, String typeName)
      Returns a human-friendly name of the given typeName for use in fixes.

      This should be used if the type may not be loaded.

      Parameters:
      typeName - a qualified canonical type name, e.g. java.util.Map.Entry.
    • qualifyStaticImport

      public static String qualifyStaticImport(String qualifiedName, SuggestedFix.Builder fix, VisitorState state)
      Provides a name to use for the (fully qualified) method provided in qualifiedName, trying to static import it if possible. Adds imports to fix as appropriate.

      The heuristic is quite conservative: it won't add a static import if an identifier with the same name is referenced anywhere in the class. Otherwise, we'd have to implement overload resolution, and ensure that adding a new static import doesn't change the semantics of existing code.

    • replaceDocTree

      public static void replaceDocTree(SuggestedFix.Builder fix, com.sun.source.util.DocTreePath docPath, String replacement)
      Replaces the leaf doctree in the given path with replacement.
    • qualifyDocReference

      public static void qualifyDocReference(SuggestedFix.Builder fix, com.sun.source.util.DocTreePath docPath, VisitorState state)
      Fully qualifies a javadoc reference, e.g. for replacing {@link List} with {@link java.util.List}.
      Parameters:
      fix - the fix builder to add to
      docPath - the path to a DCTree.DCReference element
    • removeElement

      public static SuggestedFix removeElement(com.sun.source.tree.Tree tree, List<? extends com.sun.source.tree.Tree> trees, VisitorState state)
      Removes tree from trees, assuming that trees represents a comma-separated list of expressions containing tree.

      Can be used to remove a single element from an annotation. Does not remove the enclosing parentheses if no elements are left.

    • addMembers

      public static SuggestedFix addMembers(com.sun.source.tree.ClassTree classTree, VisitorState state, String firstMember, String... otherMembers)
      Returns a Fix that adds members defined by firstMember (and optionally otherMembers) to the end of the class referenced by classTree. This method should only be called once per ClassTree as the suggestions will otherwise collide.
    • addMembers

      public static SuggestedFix addMembers(com.sun.source.tree.ClassTree classTree, VisitorState state, SuggestedFixes.AdditionPosition where, String firstMember, String... otherMembers)
      Returns a Fix that adds members defined by firstMember (and optionally otherMembers) to the class referenced by classTree. This method should only be called once per ClassTree as the suggestions will otherwise collide.
    • addMembers

      public static Optional<SuggestedFix> addMembers(com.sun.source.tree.ClassTree classTree, VisitorState state, SuggestedFixes.AdditionPosition where, Iterable<String> members)
      Returns a Fix that adds members defined by members to the class referenced by classTree. This method should only be called once per ClassTree as the suggestions will otherwise collide. It will return Optional.empty() if and only if members is empty.
    • renameVariable

      public static SuggestedFix renameVariable(com.sun.source.tree.VariableTree tree, String replacement, VisitorState state)
      Renames the given VariableTree and its usages in the current compilation unit to replacement.
    • renameVariableUsages

      public static SuggestedFix renameVariableUsages(com.sun.source.tree.VariableTree tree, String replacement, VisitorState state)
      Renames usage of the given VariableTree in the current compilation unit to replacement.
    • renameMethod

      public static SuggestedFix renameMethod(com.sun.source.tree.MethodTree tree, String replacement, VisitorState state)
      Be warned, only changes method name at the declaration.
    • renameMethodWithInvocations

      public static SuggestedFix renameMethodWithInvocations(com.sun.source.tree.MethodTree tree, String replacement, VisitorState state)
      Renames the given MethodTree and its usages in the current compilation unit to replacement.
    • renameMethodInvocation

      public static SuggestedFix renameMethodInvocation(com.sun.source.tree.MethodInvocationTree tree, String replacement, VisitorState state)
      Replaces the name of the method being invoked in tree with replacement.
    • renameTypeParameter

      public static SuggestedFix renameTypeParameter(com.sun.source.tree.TypeParameterTree typeParameter, com.sun.source.tree.Tree owningTree, String typeVarReplacement, VisitorState state)
      Renames a type parameter typeParameter owned by owningTree to typeVarReplacement. Renames occurrences in Javadoc as well.
    • deleteExceptions

      public static Fix deleteExceptions(com.sun.source.tree.MethodTree tree, VisitorState state, List<com.sun.source.tree.ExpressionTree> toDelete)
      Deletes the given exceptions from a method's throws clause.
    • addSuppressWarnings

      public static SuggestedFix addSuppressWarnings(VisitorState state, String warningToSuppress)
      Returns a fix that adds a @SuppressWarnings(warningToSuppress) to the closest suppressible element to the node pointed at by state.getPath().
      See Also:
    • addSuppressWarnings

      public static SuggestedFix addSuppressWarnings(VisitorState state, String warningToSuppress, @Nullable String lineComment)
      Returns a fix that adds a @SuppressWarnings(warningToSuppress) to the closest suppressible element to the node pointed at by state.getPath(), optionally suffixing the suppression with a comment suffix (e.g. a reason for the suppression).

      If the closest suppressible element already has a @SuppressWarning annotation, warningToSuppress will be added to the value in @SuppressWarnings instead.

      In the event that a suppressible element couldn't be found (e.g.: the state is pointing at a CompilationUnit, or some other internal inconsistency has occurred), or the enclosing suppressible element already has a @SuppressWarnings annotation with warningToSuppress, this method will throw an IllegalArgumentException.

    • addSuppressWarnings

      public static void addSuppressWarnings(SuggestedFix.Builder fixBuilder, VisitorState state, String warningToSuppress)
      Modifies fixBuilder to either create a new @SuppressWarnings element on the closest suppressible node, or add warningToSuppress to that node if there's already a SuppressWarnings annotation there.
      See Also:
    • addSuppressWarnings

      public static void addSuppressWarnings(SuggestedFix.Builder fixBuilder, VisitorState state, String warningToSuppress, @Nullable String lineComment)
      Modifies fixBuilder to either create a new @SuppressWarnings element on the closest suppressible node, or add warningToSuppress to that node if there's already a SuppressWarnings annotation there.
      Parameters:
      warningToSuppress - the warning to be suppressed, without the surrounding annotation. For example, to produce @SuppressWarnings("Foo"), pass Foo.
      lineComment - if non-null, the @SuppressWarnings will be prefixed by a line comment containing this text. Do not pass leading // or include any line breaks.
      See Also:
    • addSuppressWarnings

      public static void addSuppressWarnings(SuggestedFix.Builder fixBuilder, VisitorState state, String warningToSuppress, @Nullable String lineComment, boolean commentOnNewLine)
      Modifies fixBuilder to either create a new @SuppressWarnings element on the closest suppressible node, or add warningToSuppress to that node if there's already a SuppressWarnings annotation there.
      Parameters:
      warningToSuppress - the warning to be suppressed, without the surrounding annotation. For example, to produce @SuppressWarnings("Foo"), pass Foo.
      lineComment - if non-null, the @SuppressWarnings will have this comment associated with it. Do not pass leading // or include any line breaks.
      commentOnNewLine - if false, and this suppression results in a new annotation, the line comment will be added on the same line as the @SuppressWarnings annotation. In other cases, the line comment will be on its own line.
      See Also:
    • removeSuppressWarnings

      public static void removeSuppressWarnings(SuggestedFix.Builder fixBuilder, VisitorState state, String warningToRemove)
      Modifies fixBuilder to either remove a warningToRemove warning from the closest SuppressWarning node or remove the entire SuppressWarning node if warningToRemove is the only warning in that node.
    • addValuesToAnnotationArgument

      public static SuggestedFix.Builder addValuesToAnnotationArgument(com.sun.source.tree.AnnotationTree annotation, String parameterName, Collection<String> newValues, VisitorState state)
      Returns a fix that appends newValues to the parameterName argument for annotation, regardless of whether there is already an argument.

      N.B.: newValues are source-code strings, not string literal values.

    • updateAnnotationArgumentValues

      @Deprecated public static SuggestedFix.Builder updateAnnotationArgumentValues(com.sun.source.tree.AnnotationTree annotation, String parameterName, Collection<String> newValues)
    • updateAnnotationArgumentValues

      public static SuggestedFix.Builder updateAnnotationArgumentValues(com.sun.source.tree.AnnotationTree annotation, VisitorState state, String parameterName, Collection<String> newValues)
      Returns a fix that updates newValues to the parameterName argument for annotation, regardless of whether there is already an argument.

      N.B.: newValues are source-code strings, not string literal values.

    • compilesWithFix

      public static boolean compilesWithFix(Fix fix, VisitorState state)
      Returns true if the current compilation would succeed with the given fix applied. Note that calling this method is very expensive as it requires rerunning the entire compile, so it should be used with restraint.
    • compilesWithFix

      public static boolean compilesWithFix(Fix fix, VisitorState state, com.google.common.collect.ImmutableList<String> extraOptions, boolean onlyInSameCompilationUnit)
      Returns true if the current compilation would succeed with the given fix applied, using the given additional compiler options, optionally limiting the checking of compilation failures to the compilation unit in which the fix is applied. Note that calling this method is very expensive as it requires rerunning the entire compile, so it should be used with restraint.
    • prettyType

      public static String prettyType(com.sun.tools.javac.code.Type type, @Nullable VisitorState state)
      Pretty-prints a Type for use in diagnostic messages, qualifying any enclosed type names using qualifyType(com.google.errorprone.VisitorState, com.google.errorprone.fixes.SuggestedFix.Builder, com.sun.tools.javac.code.Symbol)}.
    • prettyType

      public static String prettyType(@Nullable VisitorState state, @Nullable SuggestedFix.Builder existingFix, com.sun.tools.javac.code.Type type)
    • suggestExemptingAnnotation

      public static Optional<SuggestedFix> suggestExemptingAnnotation(String exemptingAnnotation, com.sun.source.util.TreePath where, VisitorState state)
      Create a fix to add a suppression annotation on the surrounding class.

      No suggested fix is produced if the suppression annotation cannot be used on classes, i.e. the annotation has a @Target but does not include @Target(TYPE).

      If the suggested annotation is DontSuggestFixes, return empty.

    • suggestedExemptingAnnotationSupported

      public static boolean suggestedExemptingAnnotationSupported(Element exemptingAnnotation)
      Returns true iff suggestExemptingAnnotation() supports this annotation.
    • replaceIncludingComments

      public static SuggestedFix replaceIncludingComments(com.sun.source.util.TreePath path, String replacement, VisitorState state)
      Replaces the tree at path along with any Javadocs/associated single-line comments.

      This is the same as just deleting the tree for non-class members. For class members, we tokenize and scan backwards to try to work out which prior comments are associated with this node.

    • castTree

      public static String castTree(com.sun.source.tree.ExpressionTree expressionTree, String toType, VisitorState state)
      Casts the given expressionTree to toType, adding parentheses if necessary.