diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MemoizeConstantVisitorStateLookups.java b/core/src/main/java/com/google/errorprone/bugpatterns/MemoizeConstantVisitorStateLookups.java index 506dce96fb82..80aa91d5cee3 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MemoizeConstantVisitorStateLookups.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MemoizeConstantVisitorStateLookups.java @@ -42,7 +42,6 @@ import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.MethodInvocationTree; -import com.sun.source.util.TreeScanner; import com.sun.tools.javac.code.Symbol.MethodSymbol; import com.sun.tools.javac.code.Symbol.TypeSymbol; import com.sun.tools.javac.tree.JCTree.JCFieldAccess; @@ -157,9 +156,9 @@ private static final class CallSite { } } - private static ImmutableList findConstantLookups(ClassTree tree, VisitorState state) { + private ImmutableList findConstantLookups(ClassTree tree, VisitorState state) { ImmutableList.Builder result = ImmutableList.builder(); - new TreeScanner() { + new SuppressibleTreePathScanner(state) { @Override public Void visitMethodInvocation(MethodInvocationTree tree, Void unused) { if (CONSTANT_LOOKUP.matches(tree, state)) { @@ -186,7 +185,7 @@ private void handleConstantLookup(MethodInvocationTree tree) { } } } - }.scan(tree, null); + }.scan(state.getPath(), null); return result.build(); } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/MemoizeConstantVisitorStateLookupsTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/MemoizeConstantVisitorStateLookupsTest.java index bb00fbc96cc5..216fc5f088ad 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/MemoizeConstantVisitorStateLookupsTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/MemoizeConstantVisitorStateLookupsTest.java @@ -162,4 +162,31 @@ public void negative_doesntMemoizeTwice() { .expectUnchanged() .doTest(); } + + @Test + public void testSuppressWarnings() { + compilationTestHelper + .addSourceLines( + "Test.java", + "import com.google.errorprone.VisitorState;", + "import com.sun.tools.javac.code.Type;", + "import com.sun.tools.javac.util.Name;", + "class Test {", + " @SuppressWarnings(\"MemoizeConstantVisitorStateLookups\")", + " public Test(VisitorState state) {", + " Name className = state.getName(\"java.lang.Class\");", + " }", + " @SuppressWarnings(\"MemoizeConstantVisitorStateLookups\")", + " public void testMethod(VisitorState state) {", + " Name className = state.getName(\"java.lang.Class\");", + " }", + " @SuppressWarnings(\"MemoizeConstantVisitorStateLookups\")", + " class InnerClass {", + " void innerMethod(VisitorState state) {", + " Name className = state.getName(\"java.lang.Class\");", + " }", + " }", + "}") + .doTest(); + } }