diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/TooManyParameters.java b/core/src/main/java/com/google/errorprone/bugpatterns/TooManyParameters.java index 855094f2487..592cec9a7e0 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/TooManyParameters.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/TooManyParameters.java @@ -30,6 +30,7 @@ import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; import com.google.errorprone.matchers.Description; import com.sun.source.tree.MethodTree; +import com.sun.tools.javac.code.Symbol.ClassSymbol; import javax.inject.Inject; /** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ @@ -46,7 +47,7 @@ public class TooManyParameters extends BugChecker implements MethodTreeMatcher { static final String TOO_MANY_PARAMETERS_FLAG_NAME = "TooManyParameters:ParameterLimit"; - private static final ImmutableSet ANNOTATIONS_TO_IGNORE = + private static final ImmutableSet METHOD_ANNOTATIONS_TO_IGNORE = ImmutableSet.of( "java.lang.Deprecated", "java.lang.Override", @@ -60,8 +61,10 @@ public class TooManyParameters extends BugChecker implements MethodTreeMatcher { "org.junit.Test", // dagger provider / producers "dagger.Provides", - "dagger.producers.Produces", - "com.google.auto.factory.AutoFactory"); + "dagger.producers.Produces"); + + private static final ImmutableSet CLASS_ANNOTATIONS_TO_IGNORE = + ImmutableSet.of("com.google.auto.factory.AutoFactory"); private final int limit; @@ -91,11 +94,13 @@ public Description matchMethod(MethodTree tree, VisitorState state) { } private static boolean shouldApplyApiChecks(MethodTree tree, VisitorState state) { - for (String annotation : ANNOTATIONS_TO_IGNORE) { - if (hasAnnotation(tree, annotation, state)) { - return false; - } + var symbol = getSymbol(tree); + if (symbol.owner instanceof ClassSymbol + && CLASS_ANNOTATIONS_TO_IGNORE.stream() + .anyMatch(a -> hasAnnotation(symbol.owner, a, state))) { + return false; } - return methodIsPublicAndNotAnOverride(getSymbol(tree), state); + return METHOD_ANNOTATIONS_TO_IGNORE.stream().noneMatch(a -> hasAnnotation(tree, a, state)) + && methodIsPublicAndNotAnOverride(symbol, state); } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/TooManyParametersTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/TooManyParametersTest.java index 262414d527f..3dad94d74c5 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/TooManyParametersTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/TooManyParametersTest.java @@ -22,7 +22,6 @@ import com.google.common.collect.ImmutableList; import com.google.errorprone.CompilationTestHelper; import com.google.errorprone.ErrorProneFlags; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -30,13 +29,8 @@ /** Tests for {@link TooManyParameters}. */ @RunWith(JUnit4.class) public class TooManyParametersTest { - - private CompilationTestHelper compilationHelper; - - @Before - public void setup() { - compilationHelper = CompilationTestHelper.newInstance(TooManyParameters.class, getClass()); - } + private final CompilationTestHelper compilationHelper = + CompilationTestHelper.newInstance(TooManyParameters.class, getClass()); @Test public void zeroLimit() { @@ -112,6 +106,27 @@ public ConstructorTest(int a, int b, int c) {} .doTest(); } + @Test + public void ignoresAutoFactory() { + compilationHelper + .setArgs(ImmutableList.of("-XepOpt:" + TOO_MANY_PARAMETERS_FLAG_NAME + "=3")) + .addSourceLines( + "AutoFactory.java", + """ + package com.google.auto.factory; + public @interface AutoFactory {} + """) + .addSourceLines( + "Test.java", + """ + @com.google.auto.factory.AutoFactory + public class Test { + Test(int a, int b, int c, int d) {} + } + """) + .doTest(); + } + @Test public void method() { compilationHelper