1 | /////////////////////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code and other text files for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2022 the original author or authors. | |
4 | // | |
5 | // This library is free software; you can redistribute it and/or | |
6 | // modify it under the terms of the GNU Lesser General Public | |
7 | // License as published by the Free Software Foundation; either | |
8 | // version 2.1 of the License, or (at your option) any later version. | |
9 | // | |
10 | // This library is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | // Lesser General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU Lesser General Public | |
16 | // License along with this library; if not, write to the Free Software | |
17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | /////////////////////////////////////////////////////////////////////////////////////////////// | |
19 | ||
20 | package com.puppycrawl.tools.checkstyle.checks.sizes; | |
21 | ||
22 | import com.puppycrawl.tools.checkstyle.StatelessCheck; | |
23 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
24 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
25 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
26 | import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil; | |
27 | import com.puppycrawl.tools.checkstyle.utils.CommonUtil; | |
28 | ||
29 | /** | |
30 | * <p> | |
31 | * Checks the number of parameters of a method or constructor. | |
32 | * </p> | |
33 | * <ul> | |
34 | * <li> | |
35 | * Property {@code max} - Specify the maximum number of parameters allowed. | |
36 | * Type is {@code int}. | |
37 | * Default value is {@code 7}. | |
38 | * </li> | |
39 | * <li> | |
40 | * Property {@code ignoreOverriddenMethods} - Ignore number of parameters for | |
41 | * methods with {@code @Override} annotation. | |
42 | * Type is {@code boolean}. | |
43 | * Default value is {@code false}. | |
44 | * </li> | |
45 | * <li> | |
46 | * Property {@code tokens} - tokens to check | |
47 | * Type is {@code java.lang.String[]}. | |
48 | * Validation type is {@code tokenSet}. | |
49 | * Default value is: | |
50 | * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF"> | |
51 | * METHOD_DEF</a>, | |
52 | * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF"> | |
53 | * CTOR_DEF</a>. | |
54 | * </li> | |
55 | * </ul> | |
56 | * <p> | |
57 | * To configure the check: | |
58 | * </p> | |
59 | * <pre> | |
60 | * <module name="ParameterNumber"/> | |
61 | * </pre> | |
62 | * <p> | |
63 | * To configure the check to allow 10 parameters for a method: | |
64 | * </p> | |
65 | * <pre> | |
66 | * <module name="ParameterNumber"> | |
67 | * <property name="max" value="10"/> | |
68 | * <property name="tokens" value="METHOD_DEF"/> | |
69 | * </module> | |
70 | * </pre> | |
71 | * <p> | |
72 | * To configure the check to ignore number of parameters for methods with | |
73 | * {@code @Override} or {@code @java.lang.Override annotation}. | |
74 | * </p> | |
75 | * <p> | |
76 | * Rationale: developer may need to override method with many parameters from | |
77 | * 3-rd party library. In this case developer has no control over number of parameters. | |
78 | * </p> | |
79 | * <pre> | |
80 | * <module name="ParameterNumber"> | |
81 | * <property name="ignoreOverriddenMethods" value="true"/> | |
82 | * </module> | |
83 | * </pre> | |
84 | * <p> | |
85 | * Java code example: | |
86 | * </p> | |
87 | * <pre> | |
88 | * @Override | |
89 | * public void needsLotsOfParameters(int a, | |
90 | * int b, int c, int d, int e, int f, int g, int h) { | |
91 | * ... | |
92 | * } | |
93 | * </pre> | |
94 | * <p> | |
95 | * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} | |
96 | * </p> | |
97 | * <p> | |
98 | * Violation Message Keys: | |
99 | * </p> | |
100 | * <ul> | |
101 | * <li> | |
102 | * {@code maxParam} | |
103 | * </li> | |
104 | * </ul> | |
105 | * | |
106 | * @since 3.0 | |
107 | */ | |
108 | @StatelessCheck | |
109 | public class ParameterNumberCheck | |
110 | extends AbstractCheck { | |
111 | ||
112 | /** | |
113 | * A key is pointing to the warning message text in "messages.properties" | |
114 | * file. | |
115 | */ | |
116 | public static final String MSG_KEY = "maxParam"; | |
117 | ||
118 | /** Default maximum number of allowed parameters. */ | |
119 | private static final int DEFAULT_MAX_PARAMETERS = 7; | |
120 | ||
121 | /** Specify the maximum number of parameters allowed. */ | |
122 | private int max = DEFAULT_MAX_PARAMETERS; | |
123 | ||
124 | /** Ignore number of parameters for methods with {@code @Override} annotation. */ | |
125 | private boolean ignoreOverriddenMethods; | |
126 | ||
127 | /** | |
128 | * Setter to specify the maximum number of parameters allowed. | |
129 | * | |
130 | * @param max the max allowed parameters | |
131 | */ | |
132 | public void setMax(int max) { | |
133 | this.max = max; | |
134 | } | |
135 | ||
136 | /** | |
137 | * Setter to ignore number of parameters for methods with {@code @Override} annotation. | |
138 | * | |
139 | * @param ignoreOverriddenMethods set ignore overridden methods | |
140 | */ | |
141 | public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) { | |
142 | this.ignoreOverriddenMethods = ignoreOverriddenMethods; | |
143 | } | |
144 | ||
145 | @Override | |
146 | public int[] getDefaultTokens() { | |
147 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getAcceptableTokens(); |
148 | } | |
149 | ||
150 | @Override | |
151 | public int[] getAcceptableTokens() { | |
152 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF}; |
153 | } | |
154 | ||
155 | @Override | |
156 | public int[] getRequiredTokens() { | |
157 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtil.EMPTY_INT_ARRAY; |
158 | } | |
159 | ||
160 | @Override | |
161 | public void visitToken(DetailAST ast) { | |
162 | final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS); | |
163 | final int count = params.getChildCount(TokenTypes.PARAMETER_DEF); | |
164 |
7
1. visitToken : changed conditional boundary → KILLED 2. visitToken : negated conditional → KILLED 3. visitToken : negated conditional → KILLED 4. visitToken : removed conditional - replaced equality check with false → KILLED 5. visitToken : removed conditional - replaced equality check with true → KILLED 6. visitToken : removed conditional - replaced comparison check with false → KILLED 7. visitToken : removed conditional - replaced comparison check with true → KILLED |
if (count > max && !shouldIgnoreNumberOfParameters(ast)) { |
165 | final DetailAST name = ast.findFirstToken(TokenTypes.IDENT); | |
166 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheck::log → KILLED |
log(name, MSG_KEY, max, count); |
167 | } | |
168 | } | |
169 | ||
170 | /** | |
171 | * Determine whether to ignore number of parameters for the method. | |
172 | * | |
173 | * @param ast the token to process | |
174 | * @return true if this is overridden method and number of parameters should be ignored | |
175 | * false otherwise | |
176 | */ | |
177 | private boolean shouldIgnoreNumberOfParameters(DetailAST ast) { | |
178 | // if you override a method, you have no power over the number of parameters | |
179 |
5
1. shouldIgnoreNumberOfParameters : negated conditional → KILLED 2. shouldIgnoreNumberOfParameters : removed conditional - replaced equality check with false → KILLED 3. shouldIgnoreNumberOfParameters : removed conditional - replaced equality check with true → KILLED 4. shouldIgnoreNumberOfParameters : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED 5. shouldIgnoreNumberOfParameters : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheck::shouldIgnoreNumberOfParameters → KILLED |
return ignoreOverriddenMethods |
180 |
3
1. shouldIgnoreNumberOfParameters : negated conditional → KILLED 2. shouldIgnoreNumberOfParameters : removed conditional - replaced equality check with false → KILLED 3. shouldIgnoreNumberOfParameters : removed conditional - replaced equality check with true → KILLED |
&& AnnotationUtil.hasOverrideAnnotation(ast); |
181 | } | |
182 | ||
183 | } | |
Mutations | ||
147 |
1.1 |
|
152 |
1.1 |
|
157 |
1.1 |
|
164 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 |
|
166 |
1.1 |
|
179 |
1.1 2.2 3.3 4.4 5.5 |
|
180 |
1.1 2.2 3.3 |