Skip to content

Commit

Permalink
Fix TooManyParameters to ignore @autofactory on classes, not just met…
Browse files Browse the repository at this point in the history
…hods.

PiperOrigin-RevId: 681005706
  • Loading branch information
graememorgan authored and Error Prone Team committed Oct 1, 2024
1 parent f1f7955 commit 0cec00e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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<String> ANNOTATIONS_TO_IGNORE =
private static final ImmutableSet<String> METHOD_ANNOTATIONS_TO_IGNORE =
ImmutableSet.of(
"java.lang.Deprecated",
"java.lang.Override",
Expand All @@ -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<String> CLASS_ANNOTATIONS_TO_IGNORE =
ImmutableSet.of("com.google.auto.factory.AutoFactory");

private final int limit;

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,15 @@
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;

/** 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() {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0cec00e

Please sign in to comment.