Skip to content

Commit

Permalink
Don't fire TooManyParameters on records.
Browse files Browse the repository at this point in the history
#java21

PiperOrigin-RevId: 695858248
  • Loading branch information
kluever authored and Error Prone Team committed Nov 12, 2024
1 parent 6f6a638 commit 5cd4330
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
import static com.google.errorprone.util.ASTHelpers.isRecord;
import static com.google.errorprone.util.ASTHelpers.methodIsPublicAndNotAnOverride;

import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -84,12 +85,13 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
return NO_MATCH;
}

String name = getSymbol(tree).isConstructor() ? "constructor" : "method";
String message =
String.format(
"Consider using a builder pattern instead of a method with %s parameters. Data shows"
+ " that defining methods with > 5 parameters often leads to bugs. See also"
"Consider using a builder pattern instead of a %s with %s parameters. Data shows"
+ " that defining %s with > 5 parameters often leads to bugs. See also"
+ " Effective Java, Item 2.",
paramCount);
name, paramCount, name);
return buildDescription(tree).setMessage(message).build();
}

Expand All @@ -100,6 +102,9 @@ private static boolean shouldApplyApiChecks(MethodTree tree, VisitorState state)
.anyMatch(a -> hasAnnotation(symbol.owner, a, state))) {
return false;
}
if (isRecord(symbol)) {
return false;
}
return METHOD_ANNOTATIONS_TO_IGNORE.stream().noneMatch(a -> hasAnnotation(tree, a, state))
&& methodIsPublicAndNotAnOverride(symbol, state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ private ConstructorTest(int a, int b, int c, int d, int e, int f, int g) {}
.doTest();
}

@Test
public void recordConstructor() {
compilationHelper
.setArgs(ImmutableList.of("-XepOpt:" + TOO_MANY_PARAMETERS_FLAG_NAME + "=3"))
.addSourceLines(
"RecordExample.java",
"""
public record RecordExample(int p0, int p1, int p2, int p3, int p4, int p5) {
public RecordExample {}
}
""")
.doTest();
}

@Test
public void constructor_withAtInject() {
compilationHelper
Expand Down Expand Up @@ -114,6 +128,7 @@ public void ignoresAutoFactory() {
"AutoFactory.java",
"""
package com.google.auto.factory;
public @interface AutoFactory {}
""")
.addSourceLines(
Expand Down

0 comments on commit 5cd4330

Please sign in to comment.