diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionParameterNumberTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionParameterNumberTest.java
index 365be83b633..b0d15ffc11b 100644
--- a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionParameterNumberTest.java
+++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionParameterNumberTest.java
@@ -103,4 +103,30 @@ public void testIgnoreOverriddenMethods() throws Exception {
runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
}
+ @Test
+ public void testIgnoreAnnotatedBy() throws Exception {
+ final String filePath =
+ getPath("SuppressionXpathRegressionParameterNumberIgnoreAnnotatedBy.java");
+ final File fileToProcess = new File(filePath);
+
+ final DefaultConfiguration moduleConfig = createModuleConfig(ParameterNumberCheck.class);
+ moduleConfig.addProperty("ignoreAnnotatedBy", "MyAnno");
+ moduleConfig.addProperty("max", "2");
+
+ final String[] expectedViolations = {
+ "15:34: " + getCheckMessage(ParameterNumberCheck.class, MSG_KEY, 2, 3),
+ };
+
+ final List expectedXpathQueries = Collections.singletonList(
+ "/COMPILATION_UNIT/CLASS_DEF"
+ + "[./IDENT[@text='SuppressionXpathRegressionParameterNumberIgnoreAnnotatedBy']]"
+ + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]"
+ + "/OBJBLOCK/STATIC_INIT/SLIST/EXPR/LITERAL_NEW[./IDENT[@text='Object']]"
+ + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
+ + "/SLIST/LITERAL_IF/SLIST/EXPR/LITERAL_NEW[./IDENT[@text='Object']]"
+ + "/OBJBLOCK/METHOD_DEF/IDENT[@text='checkedMethod']"
+ );
+
+ runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
+ }
}
diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberIgnoreAnnotatedBy.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberIgnoreAnnotatedBy.java
new file mode 100644
index 00000000000..984da4cb800
--- /dev/null
+++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberIgnoreAnnotatedBy.java
@@ -0,0 +1,26 @@
+package org.checkstyle.suppressionxpathfilter.parameternumber;
+
+public class SuppressionXpathRegressionParameterNumberIgnoreAnnotatedBy {
+ static class InnerClass {
+ static {
+ new Object() {
+ void method() {
+ if (true) {
+ new Object() {
+ @MyAnno
+ void ignoredMethod(int a, @MyAnno int b, int c) {
+
+ }
+
+ void checkedMethod(int a, @MyAnno int b, int c) { // warn
+
+ }
+ };
+ }
+ }
+ };
+ }
+ }
+
+ @interface MyAnno {}
+}
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheck.java
index c281efdd64b..e4a3fe16dc9 100644
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheck.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheck.java
@@ -19,6 +19,9 @@
package com.puppycrawl.tools.checkstyle.checks.sizes;
+import java.util.Collections;
+import java.util.Set;
+
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
@@ -32,6 +35,12 @@
*
*
*
+ * Property {@code ignoreAnnotatedBy} - Ignore methods and constructors
+ * annotated with the specified annotation(s).
+ * Type is {@code java.lang.String[]}.
+ * Default value is {@code ""}.
+ *
+ *
* Property {@code ignoreOverriddenMethods} - Ignore number of parameters for
* methods with {@code @Override} annotation.
* Type is {@code boolean}.
@@ -86,6 +95,11 @@ public class ParameterNumberCheck
/** Ignore number of parameters for methods with {@code @Override} annotation. */
private boolean ignoreOverriddenMethods;
+ /**
+ * Ignore methods and constructors annotated with the specified annotation(s).
+ */
+ private Set ignoreAnnotatedBy = Collections.emptySet();
+
/**
* Setter to specify the maximum number of parameters allowed.
*
@@ -106,6 +120,16 @@ public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) {
this.ignoreOverriddenMethods = ignoreOverriddenMethods;
}
+ /**
+ * Setter to ignore methods and constructors annotated with the specified annotation(s).
+ *
+ * @param annotationNames specified annotation(s)
+ * @since 10.15.0
+ */
+ public void setIgnoreAnnotatedBy(String... annotationNames) {
+ ignoreAnnotatedBy = Set.of(annotationNames);
+ }
+
@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
@@ -132,16 +156,33 @@ public void visitToken(DetailAST ast) {
}
/**
- * Determine whether to ignore number of parameters for the method.
+ * Determine whether to ignore number of parameters.
*
* @param ast the token to process
- * @return true if this is overridden method and number of parameters should be ignored
- * false otherwise
+ * @return true if number of parameters should be ignored.
*/
private boolean shouldIgnoreNumberOfParameters(DetailAST ast) {
- // if you override a method, you have no power over the number of parameters
- return ignoreOverriddenMethods
- && AnnotationUtil.hasOverrideAnnotation(ast);
+ return isIgnoredOverriddenMethod(ast) || isAnnotatedByIgnoredAnnotations(ast);
+ }
+
+ /**
+ * Checks if method is overridden and should be ignored.
+ *
+ * @param ast method definition to check
+ * @return true if method is overridden and should be ignored.
+ */
+ private boolean isIgnoredOverriddenMethod(DetailAST ast) {
+ return ignoreOverriddenMethods && AnnotationUtil.hasOverrideAnnotation(ast);
+ }
+
+ /**
+ * Checks if method or constructor is annotated by ignored annotation(s).
+ *
+ * @param ast method or constructor definition to check
+ * @return true if annotated by ignored annotation(s).
+ */
+ private boolean isAnnotatedByIgnoredAnnotations(DetailAST ast) {
+ return AnnotationUtil.containsAnnotation(ast, ignoreAnnotatedBy);
}
}
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/sizes/ParameterNumberCheck.xml b/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/sizes/ParameterNumberCheck.xml
index b12bf9520ad..b07bfce241a 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/sizes/ParameterNumberCheck.xml
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/sizes/ParameterNumberCheck.xml
@@ -8,6 +8,10 @@
Checks the number of parameters of a method or constructor.
</p>
+
+ Ignore methods and constructors
+ annotated with the specified annotation(s).
+ Ignore number of parameters for
methods with {@code @Override} annotation.
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheckTest.java
index c8d2e33ec41..31100f8917a 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheckTest.java
@@ -123,4 +123,35 @@ public void testIgnoreOverriddenMethodsFalse()
getPath("InputParameterNumber2.java"), expected);
}
+ @Test
+ public void testIgnoreAnnotatedBy() throws Exception {
+ final String[] expected = {
+ "23:10: " + getCheckMessage(MSG_KEY, 2, 3),
+ "30:10: " + getCheckMessage(MSG_KEY, 2, 4),
+ "35:14: " + getCheckMessage(MSG_KEY, 2, 3),
+ "43:9: " + getCheckMessage(MSG_KEY, 2, 4),
+ "58:30: " + getCheckMessage(MSG_KEY, 2, 3),
+ "62:29: " + getCheckMessage(MSG_KEY, 2, 3),
+ "77:34: " + getCheckMessage(MSG_KEY, 2, 4),
+ "97:10: " + getCheckMessage(MSG_KEY, 2, 3),
+ "106:14: " + getCheckMessage(MSG_KEY, 2, 3),
+ };
+ verifyWithInlineConfigParser(
+ getPath("InputParameterNumberIgnoreAnnotatedBy.java"), expected);
+ }
+
+ @Test
+ public void testIgnoreAnnotatedByFullyQualifiedClassName() throws Exception {
+ final String[] expected = {
+ "15:10: " + getCheckMessage(MSG_KEY, 2, 3),
+ "17:10: " + getCheckMessage(MSG_KEY, 2, 3),
+ "23:10: " + getCheckMessage(MSG_KEY, 2, 3),
+ "27:10: " + getCheckMessage(MSG_KEY, 2, 3),
+ "41:14: " + getCheckMessage(MSG_KEY, 2, 3),
+ "45:14: " + getCheckMessage(MSG_KEY, 2, 3),
+ };
+ verifyWithInlineConfigParser(
+ getPath("InputParameterNumberIgnoreAnnotatedByFullyQualifiedClassName.java"),
+ expected);
+ }
}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber/InputParameterNumberIgnoreAnnotatedBy.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber/InputParameterNumberIgnoreAnnotatedBy.java
new file mode 100644
index 00000000000..ceabfd2a60c
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber/InputParameterNumberIgnoreAnnotatedBy.java
@@ -0,0 +1,115 @@
+/*
+ParameterNumber
+max = 2
+ignoreAnnotatedBy = MyAnno, Session
+
+*/
+
+package com.puppycrawl.tools.checkstyle.checks.sizes.parameternumber;
+
+public class InputParameterNumberIgnoreAnnotatedBy {
+ @MyAnno
+ InputParameterNumberIgnoreAnnotatedBy(int a, int b, int c) {}
+
+ void method1(int a, int b) {}
+
+ @MyAnno
+ void method2(int a, int b, int c) {}
+
+ @Session(uid = "Y2K")
+ void method3(int a, int b, int c, int d) {}
+
+ @Wawa
+ void method5(int a, int b, int c) { // violation, 'More than 2 parameters (found 3).'
+ }
+
+ @Wawa
+ void method6(int a, int b) {}
+
+
+ void method7(int a, int b, int c, int d) { // violation, 'More than 2 parameters (found 4).'
+ }
+
+ class InnerClass extends InputParameterNumberIgnoreAnnotatedBy {
+ @Override
+ void method2(int a, int b, int c) { // violation, 'More than 2 parameters (found 3).'
+ int d = a + c;
+ }
+
+ InnerClass(int a, int b) {
+ super(1, 2, 3);
+ }
+
+ InnerClass(int a, int b, int c, int d) { // violation, 'More than 2 parameters (found 4).'
+ super(1, 2, 3);
+ }
+ }
+
+ static class Very {
+ static class Deep {
+ static class Rabbit {
+ enum Colors {
+ GOLDEN(1, 2, 3) {
+ void jump(int a, int b) {}
+ @MyAnno
+ void roll(int a, int b, int c) {}
+
+ // violation below, 'More than 2 parameters (found 3).'
+ void loaf(int a, int b, int c) {}
+ };
+
+ @Wawa
+ private Colors(@MyAnno int a, int b, int c) {}
+ // violation above, 'More than 2 parameters (found 3).'
+
+ @Session(uid = ":D")
+ private Colors(@Wawa int a, @Wawa int b, @Wawa int c, @Wawa int d) {}
+
+ private Colors(int a) {}
+ }
+ static class Hole {
+ static {
+ new Object() {
+ @MyAnno @Session(uid = "G0F")
+ void method1(int a, int b, int c) {}
+
+ // violation below, 'More than 2 parameters (found 4).'
+ void method3(@MyAnno int a, @MyAnno int b, @MyAnno int c, int d) {}
+ };
+ }
+ }
+ }
+ }
+ }
+
+ @Wawa
+ @MyAnno
+ void method8(int a, int b, int c) {
+ }
+
+ @Session(uid = "H1") @Bit
+ @Wawa
+ void method9(int a, int b, int c) {
+ }
+
+ @Bit
+ @Wawa
+ void method10(int a, int b, int c) { // violation, 'More than 2 parameters (found 3).'
+ }
+
+ interface Reader {
+ void method1 (int a, int b);
+
+ @MyAnno
+ void method2 (int a, int b, int c);
+
+ void method3 (int a, int b, int c); // violation, 'More than 2 parameters (found 3).'
+ }
+
+ @interface Wawa {}
+ @interface Bit {}
+ @interface MyAnno {}
+ @interface Session {
+ String uid();
+ }
+}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber/InputParameterNumberIgnoreAnnotatedByFullyQualifiedClassName.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber/InputParameterNumberIgnoreAnnotatedByFullyQualifiedClassName.java
new file mode 100644
index 00000000000..9a678fb3624
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber/InputParameterNumberIgnoreAnnotatedByFullyQualifiedClassName.java
@@ -0,0 +1,62 @@
+/*
+ParameterNumber
+max = 2
+ignoreAnnotatedBy = java.lang.Deprecated, InnerClass.InnerAnno, Session
+
+*/
+
+package com.puppycrawl.tools.checkstyle.checks.sizes.parameternumber;
+
+public class InputParameterNumberIgnoreAnnotatedByFullyQualifiedClassName {
+ @java.lang.Deprecated
+ void method3(int a, int b, int c) {}
+
+ @Deprecated
+ void method4(int a, int b, int c) {} // violation, 'More than 2 parameters (found 3).'
+
+ void method5(int a, int b, int c) {} // violation, 'More than 2 parameters (found 3).'
+
+ @Session
+ void method6(int a, int b, int c) {}
+
+ @InputParameterNumberIgnoreAnnotatedByFullyQualifiedClassName.Session
+ void method7(int a, int b, int c) {} // violation, 'More than 2 parameters (found 3).'
+
+ @com.puppycrawl.tools.checkstyle.checks.sizes.parameternumber
+ .InputParameterNumberIgnoreAnnotatedByFullyQualifiedClassName.Session
+ void method8(int a, int b, int c) {} // violation, 'More than 2 parameters (found 3).'
+
+ @com.puppycrawl.tools.checkstyle.checks.sizes.parameternumber
+ .InputParameterNumberIgnoreAnnotatedByFullyQualifiedClassName.Session
+ void method8(int a, int b) {}
+
+ private static class InnerClass {
+ private @interface InnerAnno {}
+
+ @InnerClass.InnerAnno
+ void method1(int a, int b, int c) {
+ }
+
+ @InnerAnno
+ void method2(int a, int b, int c) { // violation, 'More than 2 parameters (found 3).'
+ }
+
+ @Bit
+ void method3(int a, int b, int c) { // violation, 'More than 2 parameters (found 3).'
+ }
+
+ @InnerAnno
+ void method2(int a, int b) {
+ }
+
+ @Bit
+ void method3(int a, int b) {
+ }
+
+ void method4(int a, int b) {
+ }
+ }
+
+ @interface Session {}
+ @interface Bit{}
+}
diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber/Example4.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber/Example4.txt
new file mode 100644
index 00000000000..63d693c45ef
--- /dev/null
+++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber/Example4.txt
@@ -0,0 +1,31 @@
+/*xml
+
+
+
+
+
+
+
+*/
+
+package com.puppycrawl.tools.checkstyle.checks.sizes.parameternumber;
+
+// xdoc section -- start
+class Example {
+ @JsonCreator
+ Example(int a, int b, int c, int d,
+ int e, int f, int g, int h) {}
+
+ // violation below, 'More than 7 parameters (found 8)'
+ Example(String a, String b, String c, String d,
+ String e, String f, String g, String h) {}
+
+ // annotation name must be an exact match
+ // violation below, 'More than 7 parameters (found 8)'
+ @com.puppycrawl.tools.checkstyle.checks.sizes.parameternumber.Example.JsonCreator
+ Example(float a, float b, float c, float d,
+ float e, float f, float g, float h) {}
+
+ @interface JsonCreator {}
+}
+// xdoc section -- end
diff --git a/src/xdocs/checks/sizes/parameternumber.xml b/src/xdocs/checks/sizes/parameternumber.xml
index be78ffc5201..10d18c1a124 100644
--- a/src/xdocs/checks/sizes/parameternumber.xml
+++ b/src/xdocs/checks/sizes/parameternumber.xml
@@ -24,6 +24,13 @@
default value
since
+
+
ignoreAnnotatedBy
+
Ignore methods and constructors annotated with the specified annotation(s).
+
+class Example {
+ @JsonCreator
+ Example(int a, int b, int c, int d,
+ int e, int f, int g, int h) {}
+
+ // violation below, 'More than 7 parameters (found 8)'
+ Example(String a, String b, String c, String d,
+ String e, String f, String g, String h) {}
+
+ // annotation name must be an exact match
+ // violation below, 'More than 7 parameters (found 8)'
+ @com.puppycrawl.tools.checkstyle.checks.sizes.parameternumber.Example.JsonCreator
+ Example(float a, float b, float c, float d,
+ float e, float f, float g, float h) {}
+
+ @interface JsonCreator {}
+}
+
+
+ Note: Annotation names specified in the ignoreAnnotatedBy property
+ must be an exact match for the annotation name on the method or constructor.
+