Class SuggestedFixes
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
InstructsaddMembers(ClassTree, VisitorState, AdditionPosition, String, String...)
whether to add the new member(s) at the beginning of the class, or at the end.static final class
A class to hold the files from the compilation context, with a diff applied to the currently-processed one; the files can then be recompiled. -
Method Summary
Modifier and TypeMethodDescriptionstatic Optional
<SuggestedFix> addMembers
(com.sun.source.tree.ClassTree classTree, VisitorState state, SuggestedFixes.AdditionPosition where, Iterable<String> members) static SuggestedFix
addMembers
(com.sun.source.tree.ClassTree classTree, VisitorState state, SuggestedFixes.AdditionPosition where, String firstMember, String... otherMembers) Returns aFix
that adds members defined byfirstMember
(and optionallyotherMembers
) to the class referenced byclassTree
.static SuggestedFix
addMembers
(com.sun.source.tree.ClassTree classTree, VisitorState state, String firstMember, String... otherMembers) Returns aFix
that adds members defined byfirstMember
(and optionallyotherMembers
) to the end of the class referenced byclassTree
.static Optional
<SuggestedFix> addModifiers
(com.sun.source.tree.Tree tree, VisitorState state, Modifier... modifiers) Adds modifiers to the given class, method, or field declaration.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.static void
addSuppressWarnings
(SuggestedFix.Builder fixBuilder, VisitorState state, String warningToSuppress) ModifiesfixBuilder
to either create a new@SuppressWarnings
element on the closest suppressible node, or addwarningToSuppress
to that node if there's already aSuppressWarnings
annotation there.static void
addSuppressWarnings
(SuggestedFix.Builder fixBuilder, VisitorState state, String warningToSuppress, @Nullable String lineComment) ModifiesfixBuilder
to either create a new@SuppressWarnings
element on the closest suppressible node, or addwarningToSuppress
to that node if there's already aSuppressWarnings
annotation there.static void
addSuppressWarnings
(SuggestedFix.Builder fixBuilder, VisitorState state, String warningToSuppress, @Nullable String lineComment, boolean commentOnNewLine) ModifiesfixBuilder
to either create a new@SuppressWarnings
element on the closest suppressible node, or addwarningToSuppress
to that node if there's already aSuppressWarnings
annotation there.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 bystate.getPath()
.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 bystate.getPath()
, optionally suffixing the suppression with a comment suffix (e.g.static SuggestedFix.Builder
addValuesToAnnotationArgument
(com.sun.source.tree.AnnotationTree annotation, String parameterName, Collection<String> newValues, VisitorState state) Returns a fix that appendsnewValues
to theparameterName
argument forannotation
, regardless of whether there is already an argument.static String
castTree
(com.sun.source.tree.ExpressionTree expressionTree, String toType, VisitorState state) Casts the givenexpressionTree
totoType
, adding parentheses if necessary.static boolean
compilesWithFix
(Fix fix, VisitorState state) Returns true if the current compilation would succeed with the given fix applied.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.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.static String
prettyType
(@Nullable VisitorState state, @Nullable SuggestedFix.Builder existingFix, com.sun.tools.javac.code.Type type) Pretty-prints a Type for use in fixes, qualifying any enclosed type names usingqualifyType(com.google.errorprone.VisitorState, com.google.errorprone.fixes.SuggestedFix.Builder, com.sun.tools.javac.code.Symbol)
}.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 usingqualifyType(com.google.errorprone.VisitorState, com.google.errorprone.fixes.SuggestedFix.Builder, com.sun.tools.javac.code.Symbol)
}.static void
qualifyDocReference
(SuggestedFix.Builder fix, com.sun.source.util.DocTreePath docPath, VisitorState state) Fully qualifies a javadoc reference, e.g.static String
qualifyStaticImport
(String qualifiedName, SuggestedFix.Builder fix, VisitorState state) Provides a name to use for the (fully qualified) method provided inqualifiedName
, trying to static import it if possible.static String
qualifyType
(VisitorState state, SuggestedFix.Builder fix, com.sun.tools.javac.code.Symbol sym) Returns a human-friendly name of the givenSymbol
for use in fixes.static String
qualifyType
(VisitorState state, SuggestedFix.Builder fix, String typeName) Returns a human-friendly name of the giventypeName
for use in fixes.static String
qualifyType
(VisitorState state, SuggestedFix.Builder fix, TypeMirror type) Returns a human-friendly name of the given type for use in fixes.static SuggestedFix
removeElement
(com.sun.source.tree.Tree tree, List<? extends com.sun.source.tree.Tree> trees, VisitorState state) Removestree
fromtrees
, assuming thattrees
represents a comma-separated list of expressions containingtree
.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.static Optional
<SuggestedFix> removeModifiers
(com.sun.source.tree.Tree tree, VisitorState state, Modifier... modifiers) Removes modifiers from the given class, method, or field declaration.static void
removeSuppressWarnings
(SuggestedFix.Builder fixBuilder, VisitorState state, String warningToRemove) ModifiesfixBuilder
to either remove awarningToRemove
warning from the closestSuppressWarning
node or remove the entireSuppressWarning
node ifwarningToRemove
is the only warning in that node.static SuggestedFix
renameMethod
(com.sun.source.tree.MethodTree tree, String replacement, VisitorState state) Be warned, only changes method name at the declaration.static SuggestedFix
renameMethodInvocation
(com.sun.source.tree.MethodInvocationTree tree, String replacement, VisitorState state) Replaces the name of the method being invoked intree
withreplacement
.static SuggestedFix
renameMethodWithInvocations
(com.sun.source.tree.MethodTree tree, String replacement, VisitorState state) Renames the givenMethodTree
and its usages in the current compilation unit toreplacement
.static SuggestedFix
renameTypeParameter
(com.sun.source.tree.TypeParameterTree typeParameter, com.sun.source.tree.Tree owningTree, String typeVarReplacement, VisitorState state) Renames a type parametertypeParameter
owned byowningTree
totypeVarReplacement
.static SuggestedFix
renameVariable
(com.sun.source.tree.VariableTree tree, String replacement, VisitorState state) Renames the givenVariableTree
and its usages in the current compilation unit toreplacement
.static SuggestedFix
renameVariableUsages
(com.sun.source.tree.VariableTree tree, String replacement, VisitorState state) Renames usage of the givenVariableTree
in the current compilation unit toreplacement
.static void
replaceDocTree
(SuggestedFix.Builder fix, com.sun.source.util.DocTreePath docPath, String replacement) Replaces the leaf doctree in the given path withreplacement
.static SuggestedFix
replaceIncludingComments
(com.sun.source.util.TreePath path, String replacement, VisitorState state) Replaces the tree atpath
along with any Javadocs/associated single-line comments.static boolean
suggestedExemptingAnnotationSupported
(Element exemptingAnnotation) Returns true iffsuggestExemptingAnnotation()
supports this annotation.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.static SuggestedFix.Builder
updateAnnotationArgumentValues
(com.sun.source.tree.AnnotationTree annotation, VisitorState state, String parameterName, Collection<String> newValues) Returns a fix that updatesnewValues
to theparameterName
argument forannotation
, regardless of whether there is already an argument.static SuggestedFix.Builder
updateAnnotationArgumentValues
(com.sun.source.tree.AnnotationTree annotation, String parameterName, Collection<String> newValues) Deprecated.
-
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 givenSymbol
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
Returns a human-friendly name of the given type for use in fixes. -
qualifyType
Returns a human-friendly name of the giventypeName
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 inqualifiedName
, trying to static import it if possible. Adds imports tofix
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 withreplacement
. -
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 todocPath
- the path to aDCTree.DCReference
element
-
removeElement
public static SuggestedFix removeElement(com.sun.source.tree.Tree tree, List<? extends com.sun.source.tree.Tree> trees, VisitorState state) Removestree
fromtrees
, assuming thattrees
represents a comma-separated list of expressions containingtree
.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 aFix
that adds members defined byfirstMember
(and optionallyotherMembers
) to the end of the class referenced byclassTree
. This method should only be called once perClassTree
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 aFix
that adds members defined byfirstMember
(and optionallyotherMembers
) to the class referenced byclassTree
. This method should only be called once perClassTree
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 aFix
that adds members defined bymembers
to the class referenced byclassTree
. This method should only be called once perClassTree
as the suggestions will otherwise collide. It will returnOptional.empty()
if and only ifmembers
is empty. -
renameVariable
public static SuggestedFix renameVariable(com.sun.source.tree.VariableTree tree, String replacement, VisitorState state) Renames the givenVariableTree
and its usages in the current compilation unit toreplacement
. -
renameVariableUsages
public static SuggestedFix renameVariableUsages(com.sun.source.tree.VariableTree tree, String replacement, VisitorState state) Renames usage of the givenVariableTree
in the current compilation unit toreplacement
. -
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 givenMethodTree
and its usages in the current compilation unit toreplacement
. -
renameMethodInvocation
public static SuggestedFix renameMethodInvocation(com.sun.source.tree.MethodInvocationTree tree, String replacement, VisitorState state) Replaces the name of the method being invoked intree
withreplacement
. -
renameTypeParameter
public static SuggestedFix renameTypeParameter(com.sun.source.tree.TypeParameterTree typeParameter, com.sun.source.tree.Tree owningTree, String typeVarReplacement, VisitorState state) Renames a type parametertypeParameter
owned byowningTree
totypeVarReplacement
. 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
Returns a fix that adds a@SuppressWarnings(warningToSuppress)
to the closest suppressible element to the node pointed at bystate.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 bystate.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 withwarningToSuppress
, this method will throw anIllegalArgumentException
. -
addSuppressWarnings
public static void addSuppressWarnings(SuggestedFix.Builder fixBuilder, VisitorState state, String warningToSuppress) ModifiesfixBuilder
to either create a new@SuppressWarnings
element on the closest suppressible node, or addwarningToSuppress
to that node if there's already aSuppressWarnings
annotation there.- See Also:
-
addSuppressWarnings
public static void addSuppressWarnings(SuggestedFix.Builder fixBuilder, VisitorState state, String warningToSuppress, @Nullable String lineComment) ModifiesfixBuilder
to either create a new@SuppressWarnings
element on the closest suppressible node, or addwarningToSuppress
to that node if there's already aSuppressWarnings
annotation there.- Parameters:
warningToSuppress
- the warning to be suppressed, without the surrounding annotation. For example, to produce@SuppressWarnings("Foo")
, passFoo
.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) ModifiesfixBuilder
to either create a new@SuppressWarnings
element on the closest suppressible node, or addwarningToSuppress
to that node if there's already aSuppressWarnings
annotation there.- Parameters:
warningToSuppress
- the warning to be suppressed, without the surrounding annotation. For example, to produce@SuppressWarnings("Foo")
, passFoo
.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) ModifiesfixBuilder
to either remove awarningToRemove
warning from the closestSuppressWarning
node or remove the entireSuppressWarning
node ifwarningToRemove
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 appendsnewValues
to theparameterName
argument forannotation
, 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) Deprecated. -
updateAnnotationArgumentValues
public static SuggestedFix.Builder updateAnnotationArgumentValues(com.sun.source.tree.AnnotationTree annotation, VisitorState state, String parameterName, Collection<String> newValues) Returns a fix that updatesnewValues
to theparameterName
argument forannotation
, regardless of whether there is already an argument.N.B.:
newValues
are source-code strings, not string literal values. -
compilesWithFix
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
Pretty-prints a Type for use in diagnostic messages, qualifying any enclosed type names usingqualifyType(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) Pretty-prints a Type for use in fixes, qualifying any enclosed type names usingqualifyType(com.google.errorprone.VisitorState, com.google.errorprone.fixes.SuggestedFix.Builder, com.sun.tools.javac.code.Symbol)
}. -
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
Returns true iffsuggestExemptingAnnotation()
supports this annotation. -
replaceIncludingComments
public static SuggestedFix replaceIncludingComments(com.sun.source.util.TreePath path, String replacement, VisitorState state) Replaces the tree atpath
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 givenexpressionTree
totoType
, adding parentheses if necessary.
-
updateAnnotationArgumentValues(AnnotationTree, VisitorState, String, Collection)
instead