Skip to content

Commit

Permalink
Issue #11340: Add ignoreAnnotatedBy property to ParameterNumberCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
sktpy committed Feb 28, 2024
1 parent bb97694 commit 4cc9297
Show file tree
Hide file tree
Showing 10 changed files with 404 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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 {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package com.puppycrawl.tools.checkstyle.checks.sizes;

import java.util.Set;

import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
Expand All @@ -32,6 +34,12 @@
* </p>
* <ul>
* <li>
* Property {@code ignoreAnnotatedBy} - Ignore methods and constructors
* annotated with the specified annotation(s).
* Type is {@code java.lang.String[]}.
* Default value is {@code ""}.
* </li>
* <li>
* Property {@code ignoreOverriddenMethods} - Ignore number of parameters for
* methods with {@code @Override} annotation.
* Type is {@code boolean}.
Expand Down Expand Up @@ -86,6 +94,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<String> ignoreAnnotatedBy = Set.of();

/**
* Setter to specify the maximum number of parameters allowed.
*
Expand All @@ -106,6 +119,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.14.0
*/
public void setIgnoreAnnotatedBy(String... annotationNames) {
ignoreAnnotatedBy = Set.of(annotationNames);
}

@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
Expand All @@ -132,16 +155,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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
Checks the number of parameters of a method or constructor.
&lt;/p&gt;</description>
<properties>
<property default-value="" name="ignoreAnnotatedBy" type="java.lang.String[]">
<description>Ignore methods and constructors
annotated with the specified annotation(s).</description>
</property>
<property default-value="false" name="ignoreOverriddenMethods" type="boolean">
<description>Ignore number of parameters for
methods with {@code @Override} annotation.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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{}
}

0 comments on commit 4cc9297

Please sign in to comment.