diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/AbstractSuppressWarningsMatcher.java b/core/src/main/java/com/google/errorprone/bugpatterns/AbstractSuppressWarningsMatcher.java deleted file mode 100644 index 47f56289b79..00000000000 --- a/core/src/main/java/com/google/errorprone/bugpatterns/AbstractSuppressWarningsMatcher.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2012 The Error Prone Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.errorprone.bugpatterns; - -import com.google.common.base.Joiner; -import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher; -import com.google.errorprone.fixes.Fix; -import com.google.errorprone.fixes.SuggestedFix; -import com.google.errorprone.util.ASTHelpers; -import com.sun.source.tree.AnnotationTree; -import com.sun.source.tree.AssignmentTree; -import com.sun.source.tree.ExpressionTree; -import com.sun.source.tree.NewArrayTree; -import com.sun.tools.javac.tree.JCTree; -import java.util.ArrayList; -import java.util.List; - -/** Abstract matcher which can process changes to a SuppressWarnings annotation. */ -abstract class AbstractSuppressWarningsMatcher extends BugChecker implements AnnotationTreeMatcher { - - /** - * Processes the list of SuppressWarnings values in-place when creating a {@link SuggestedFix}. - * Items may be added, removed or re-ordered as necessary. The initial input are the values in the - * order specified in the code being processed. - * - * @param values list of suppressed warnings in the order in which they appear in the code - */ - protected abstract void processSuppressWarningsValues(List values); - - protected final Fix getSuggestedFix(AnnotationTree annotationTree) { - List values = new ArrayList<>(); - for (ExpressionTree argumentTree : annotationTree.getArguments()) { - AssignmentTree assignmentTree = (AssignmentTree) argumentTree; - if (ASTHelpers.getSymbol(assignmentTree.getVariable()) - .getSimpleName() - .contentEquals("value")) { - ExpressionTree expressionTree = assignmentTree.getExpression(); - switch (expressionTree.getKind()) { - case STRING_LITERAL: - values.add(((String) ((JCTree.JCLiteral) expressionTree).value)); - break; - case NEW_ARRAY: - NewArrayTree newArrayTree = (NewArrayTree) expressionTree; - for (ExpressionTree elementTree : newArrayTree.getInitializers()) { - values.add((String) ((JCTree.JCLiteral) elementTree).value); - } - break; - default: - throw new AssertionError("Unknown kind: " + expressionTree.getKind()); - } - processSuppressWarningsValues(values); - } else { - throw new AssertionError("SuppressWarnings has an element other than value="); - } - } - - if (values.isEmpty()) { - return SuggestedFix.delete(annotationTree); - } else if (values.size() == 1) { - return SuggestedFix.replace(annotationTree, "@SuppressWarnings(\"" + values.get(0) + "\")"); - } else { - return SuggestedFix.replace( - annotationTree, "@SuppressWarnings({\"" + Joiner.on("\", \"").join(values) + "\"})"); - } - } -} diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/SuppressWarningsDeprecated.java b/core/src/main/java/com/google/errorprone/bugpatterns/SuppressWarningsDeprecated.java index bdb781180e3..2c8960a2754 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/SuppressWarningsDeprecated.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/SuppressWarningsDeprecated.java @@ -21,13 +21,20 @@ import static com.google.errorprone.matchers.Matchers.hasArgumentWithValue; import static com.google.errorprone.matchers.Matchers.isType; import static com.google.errorprone.matchers.Matchers.stringLiteral; +import static java.util.stream.Collectors.toList; +import com.google.auto.common.AnnotationMirrors; import com.google.errorprone.BugPattern; import com.google.errorprone.VisitorState; +import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher; +import com.google.errorprone.fixes.SuggestedFixes; import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Matcher; +import com.google.errorprone.util.ASTHelpers; +import com.google.errorprone.util.MoreAnnotations; import com.sun.source.tree.AnnotationTree; import java.util.List; +import javax.lang.model.element.AnnotationMirror; /** * Find uses of SuppressWarnings with "deprecated". @@ -38,9 +45,8 @@ name = "SuppressWarningsDeprecated", summary = "Suppressing \"deprecated\" is probably a typo for \"deprecation\"", severity = ERROR) -public class SuppressWarningsDeprecated extends AbstractSuppressWarningsMatcher { +public class SuppressWarningsDeprecated extends BugChecker implements AnnotationTreeMatcher { - @SuppressWarnings("varargs") private static final Matcher matcher = allOf( isType("java.lang.SuppressWarnings"), @@ -48,18 +54,19 @@ public class SuppressWarningsDeprecated extends AbstractSuppressWarningsMatcher @Override public final Description matchAnnotation(AnnotationTree annotationTree, VisitorState state) { - if (matcher.matches(annotationTree, state)) { - return describeMatch(annotationTree, getSuggestedFix(annotationTree)); + if (!matcher.matches(annotationTree, state)) { + return Description.NO_MATCH; } - return Description.NO_MATCH; - } - @Override - protected void processSuppressWarningsValues(List values) { - for (int i = 0; i < values.size(); i++) { - if (values.get(i).equals("deprecated")) { - values.set(i, "deprecation"); - } - } + AnnotationMirror mirror = ASTHelpers.getAnnotationMirror(annotationTree); + List values = + MoreAnnotations.asStrings(AnnotationMirrors.getAnnotationValue(mirror, "value")) + .map(v -> v.equals("deprecated") ? "deprecation" : v) + .map(state::getConstantExpression) + .collect(toList()); + + return describeMatch( + annotationTree, + SuggestedFixes.updateAnnotationArgumentValues(annotationTree, "value", values).build()); } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/SuppressWarningsDeprecatedPositiveCases.java b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/SuppressWarningsDeprecatedPositiveCases.java index b716717ff7f..bfa796fc7d7 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/SuppressWarningsDeprecatedPositiveCases.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/SuppressWarningsDeprecatedPositiveCases.java @@ -58,11 +58,11 @@ public static void positiveCase7() { class Foo {}; } - // BUG: Diagnostic contains: @SuppressWarnings("deprecation") + // BUG: Diagnostic contains: @SuppressWarnings(value = "deprecation") @SuppressWarnings(value = {"deprecated"}) public static void positiveCase8() {} - // BUG: Diagnostic contains: @SuppressWarnings("deprecation") + // BUG: Diagnostic contains: @SuppressWarnings(value = "deprecation") @SuppressWarnings(value = "deprecated") public static void positiveCase9() {} }