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 java.util.ArrayDeque; | |
23 | import java.util.BitSet; | |
24 | import java.util.Deque; | |
25 | import java.util.Objects; | |
26 | import java.util.stream.Stream; | |
27 | ||
28 | import com.puppycrawl.tools.checkstyle.StatelessCheck; | |
29 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
30 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
31 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
32 | import com.puppycrawl.tools.checkstyle.utils.CommonUtil; | |
33 | ||
34 | /** | |
35 | * <p> | |
36 | * Checks for long methods and constructors. | |
37 | * </p> | |
38 | * <p> | |
39 | * Rationale: If a method becomes very long it is hard to understand. | |
40 | * Therefore, long methods should usually be refactored into several | |
41 | * individual methods that focus on a specific task. | |
42 | * </p> | |
43 | * <ul> | |
44 | * <li> | |
45 | * Property {@code max} - Specify the maximum number of lines allowed. | |
46 | * Type is {@code int}. | |
47 | * Default value is {@code 150}. | |
48 | * </li> | |
49 | * <li> | |
50 | * Property {@code countEmpty} - Control whether to count empty lines and comments. | |
51 | * Type is {@code boolean}. | |
52 | * Default value is {@code true}. | |
53 | * </li> | |
54 | * <li> | |
55 | * Property {@code tokens} - tokens to check | |
56 | * Type is {@code java.lang.String[]}. | |
57 | * Validation type is {@code tokenSet}. | |
58 | * Default value is: | |
59 | * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF"> | |
60 | * METHOD_DEF</a>, | |
61 | * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF"> | |
62 | * CTOR_DEF</a>, | |
63 | * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#COMPACT_CTOR_DEF"> | |
64 | * COMPACT_CTOR_DEF</a>. | |
65 | * </li> | |
66 | * </ul> | |
67 | * <p> | |
68 | * To configure the check: | |
69 | * </p> | |
70 | * <pre> | |
71 | * <module name="MethodLength"/> | |
72 | * </pre> | |
73 | * <p> | |
74 | * Example: | |
75 | * </p> | |
76 | * <pre> | |
77 | * public class MyClass { | |
78 | * public MyClass() { // constructor (line 1) | |
79 | * /* line 2 | |
80 | * ... | |
81 | * line 150 */ | |
82 | * } // line 151, violation, as it is over 150 | |
83 | * | |
84 | * public void firstExample() { // line 1 | |
85 | * | |
86 | * // line 3 | |
87 | * System.out.println("line 4"); | |
88 | * /* line 5 | |
89 | * line 6 */ | |
90 | * } // line 7, OK, as it is less than 150 | |
91 | * | |
92 | * public void secondExample() { // line 1 | |
93 | * // line 2 | |
94 | * System.out.println("line 3"); | |
95 | * | |
96 | * /* line 5 | |
97 | * ... | |
98 | * line 150 */ | |
99 | * } // line 151, violation, as it is over 150 | |
100 | * } | |
101 | * </pre> | |
102 | * <p> | |
103 | * To configure the check so that it accepts methods with at most 4 lines: | |
104 | * </p> | |
105 | * <pre> | |
106 | * <module name="MethodLength"> | |
107 | * <property name="tokens" value="METHOD_DEF"/> | |
108 | * <property name="max" value="4"/> | |
109 | * </module> | |
110 | * </pre> | |
111 | * <p> | |
112 | * Example: | |
113 | * </p> | |
114 | * <pre> | |
115 | * public class MyTest { | |
116 | * public MyTest() { // constructor (line 1) | |
117 | * int var1 = 2; // line 2 | |
118 | * int var2 = 4; // line 3 | |
119 | * int sum = var1 + var2; // line 4 | |
120 | * } // line 5, OK, constructor is not mentioned in the tokens | |
121 | * | |
122 | * public void firstMethod() { // line 1 | |
123 | * // comment (line 2) | |
124 | * System.out.println("line 3"); | |
125 | * } // line 4, OK, as it allows at most 4 lines | |
126 | * | |
127 | * public void secondMethod() { // line 1 | |
128 | * int index = 0; // line 2 | |
129 | * if (index < 5) { // line 3 | |
130 | * index++; // line 4 | |
131 | * } // line 5 | |
132 | * } // line 6, violation, as it is over 4 lines | |
133 | * | |
134 | * public void thirdMethod() { // line 1 | |
135 | * | |
136 | * // comment (line 3) | |
137 | * System.out.println("line 4"); | |
138 | * } // line 5, violation, as it is over 4 lines | |
139 | * } | |
140 | * </pre> | |
141 | * <p> | |
142 | * To configure the check so that it accepts methods with at most 4 lines, | |
143 | * not counting empty lines and comments: | |
144 | * </p> | |
145 | * <pre> | |
146 | * <module name="MethodLength"> | |
147 | * <property name="tokens" value="METHOD_DEF"/> | |
148 | * <property name="max" value="4"/> | |
149 | * <property name="countEmpty" value="false"/> | |
150 | * </module> | |
151 | * </pre> | |
152 | * <p> | |
153 | * Example: | |
154 | * </p> | |
155 | * <pre> | |
156 | * public class MyTest { | |
157 | * public MyTest() { // constructor (line 1) | |
158 | * int var1 = 2; // line 2 | |
159 | * int var2 = 4; // line 3 | |
160 | * int sum = var1 + var2; // line 4 | |
161 | * } // line 5, OK, constructor is not mentioned in the tokens | |
162 | * | |
163 | * public void firstMethod() { // line 1 | |
164 | * // comment - not counted as line | |
165 | * System.out.println("line 2"); | |
166 | * } // line 3, OK, as it allows at most 4 lines | |
167 | * | |
168 | * public void secondMethod() { // line 1 | |
169 | * int index = 0; // line 2 | |
170 | * if (index < 5) { // line 3 | |
171 | * index++; // line 4 | |
172 | * } // line 5 | |
173 | * } // line 6, violation, as it is over 4 lines | |
174 | * | |
175 | * public void thirdMethod() { // line 1 | |
176 | * | |
177 | * // comment - not counted as line | |
178 | * System.out.println("line 2"); | |
179 | * } // line 3, OK, as it allows at most 4 lines | |
180 | * } | |
181 | * </pre> | |
182 | * <p> | |
183 | * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} | |
184 | * </p> | |
185 | * <p> | |
186 | * Violation Message Keys: | |
187 | * </p> | |
188 | * <ul> | |
189 | * <li> | |
190 | * {@code maxLen.method} | |
191 | * </li> | |
192 | * </ul> | |
193 | * | |
194 | * @since 3.0 | |
195 | */ | |
196 | @StatelessCheck | |
197 | public class MethodLengthCheck extends AbstractCheck { | |
198 | ||
199 | /** | |
200 | * A key is pointing to the warning message text in "messages.properties" | |
201 | * file. | |
202 | */ | |
203 | public static final String MSG_KEY = "maxLen.method"; | |
204 | ||
205 | /** Default maximum number of lines. */ | |
206 | private static final int DEFAULT_MAX_LINES = 150; | |
207 | ||
208 | /** Control whether to count empty lines and comments. */ | |
209 | private boolean countEmpty = true; | |
210 | ||
211 | /** Specify the maximum number of lines allowed. */ | |
212 | private int max = DEFAULT_MAX_LINES; | |
213 | ||
214 | @Override | |
215 | public int[] getDefaultTokens() { | |
216 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/MethodLengthCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getAcceptableTokens(); |
217 | } | |
218 | ||
219 | @Override | |
220 | public int[] getAcceptableTokens() { | |
221 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/MethodLengthCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
222 | TokenTypes.METHOD_DEF, | |
223 | TokenTypes.CTOR_DEF, | |
224 | TokenTypes.COMPACT_CTOR_DEF, | |
225 | }; | |
226 | } | |
227 | ||
228 | @Override | |
229 | public int[] getRequiredTokens() { | |
230 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/MethodLengthCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtil.EMPTY_INT_ARRAY; |
231 | } | |
232 | ||
233 | @Override | |
234 | public void visitToken(DetailAST ast) { | |
235 | final DetailAST openingBrace = ast.findFirstToken(TokenTypes.SLIST); | |
236 |
3
1. visitToken : removed conditional - replaced equality check with true → SURVIVED 2. visitToken : negated conditional → KILLED 3. visitToken : removed conditional - replaced equality check with false → KILLED |
if (openingBrace != null) { |
237 | final int length; | |
238 |
3
1. visitToken : negated conditional → KILLED 2. visitToken : removed conditional - replaced equality check with false → KILLED 3. visitToken : removed conditional - replaced equality check with true → KILLED |
if (countEmpty) { |
239 | final DetailAST closingBrace = openingBrace.findFirstToken(TokenTypes.RCURLY); | |
240 | length = getLengthOfBlock(openingBrace, closingBrace); | |
241 | } | |
242 | else { | |
243 | length = countUsedLines(openingBrace); | |
244 | } | |
245 |
4
1. visitToken : changed conditional boundary → KILLED 2. visitToken : negated conditional → KILLED 3. visitToken : removed conditional - replaced comparison check with false → KILLED 4. visitToken : removed conditional - replaced comparison check with true → KILLED |
if (length > max) { |
246 | final String methodName = ast.findFirstToken(TokenTypes.IDENT).getText(); | |
247 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/sizes/MethodLengthCheck::log → KILLED |
log(ast, MSG_KEY, length, max, methodName); |
248 | } | |
249 | } | |
250 | } | |
251 | ||
252 | /** | |
253 | * Returns length of code. | |
254 | * | |
255 | * @param openingBrace block opening brace | |
256 | * @param closingBrace block closing brace | |
257 | * @return number of lines with code for current block | |
258 | */ | |
259 | private static int getLengthOfBlock(DetailAST openingBrace, DetailAST closingBrace) { | |
260 | final int startLineNo = openingBrace.getLineNo(); | |
261 | final int endLineNo = closingBrace.getLineNo(); | |
262 |
3
1. getLengthOfBlock : Replaced integer subtraction with addition → KILLED 2. getLengthOfBlock : Replaced integer addition with subtraction → KILLED 3. getLengthOfBlock : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return endLineNo - startLineNo + 1; |
263 | } | |
264 | ||
265 | /** | |
266 | * Count number of used code lines without comments. | |
267 | * | |
268 | * @param ast start ast | |
269 | * @return number of used lines of code | |
270 | */ | |
271 | private static int countUsedLines(DetailAST ast) { | |
272 |
1
1. countUsedLines : removed call to java/util/ArrayDeque::<init> → KILLED |
final Deque<DetailAST> nodes = new ArrayDeque<>(); |
273 | nodes.add(ast); | |
274 |
1
1. countUsedLines : removed call to java/util/BitSet::<init> → KILLED |
final BitSet usedLines = new BitSet(); |
275 | final int startLineNo = ast.getLineNo(); | |
276 |
3
1. countUsedLines : negated conditional → KILLED 2. countUsedLines : removed conditional - replaced equality check with false → KILLED 3. countUsedLines : removed conditional - replaced equality check with true → KILLED |
while (!nodes.isEmpty()) { |
277 | final DetailAST node = nodes.removeFirst(); | |
278 |
1
1. countUsedLines : Replaced integer subtraction with addition → SURVIVED |
final int lineIndex = node.getLineNo() - startLineNo; |
279 | // text block requires special treatment, | |
280 | // since it is the only non-comment token that can span more than one line | |
281 |
3
1. countUsedLines : removed conditional - replaced equality check with false → SURVIVED 2. countUsedLines : negated conditional → KILLED 3. countUsedLines : removed conditional - replaced equality check with true → KILLED |
if (node.getType() == TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) { |
282 |
1
1. countUsedLines : Replaced integer subtraction with addition → NO_COVERAGE |
final int endLineIndex = node.getLastChild().getLineNo() - startLineNo; |
283 |
2
1. countUsedLines : Replaced integer addition with subtraction → NO_COVERAGE 2. countUsedLines : removed call to java/util/BitSet::set → NO_COVERAGE |
usedLines.set(lineIndex, endLineIndex + 1); |
284 | } | |
285 | else { | |
286 |
1
1. countUsedLines : removed call to java/util/BitSet::set → KILLED |
usedLines.set(lineIndex); |
287 | Stream.iterate( | |
288 | node.getLastChild(), Objects::nonNull, DetailAST::getPreviousSibling | |
289 |
1
1. countUsedLines : removed call to java/util/stream/Stream::forEach → KILLED |
).forEach(nodes::addFirst); |
290 | } | |
291 | } | |
292 |
1
1. countUsedLines : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return usedLines.cardinality(); |
293 | } | |
294 | ||
295 | /** | |
296 | * Setter to specify the maximum number of lines allowed. | |
297 | * | |
298 | * @param length the maximum length of a method. | |
299 | */ | |
300 | public void setMax(int length) { | |
301 | max = length; | |
302 | } | |
303 | ||
304 | /** | |
305 | * Setter to control whether to count empty lines and comments. | |
306 | * | |
307 | * @param countEmpty whether to count empty and comments. | |
308 | */ | |
309 | public void setCountEmpty(boolean countEmpty) { | |
310 | this.countEmpty = countEmpty; | |
311 | } | |
312 | ||
313 | } | |
Mutations | ||
216 |
1.1 |
|
221 |
1.1 |
|
230 |
1.1 |
|
236 |
1.1 2.2 3.3 |
|
238 |
1.1 2.2 3.3 |
|
245 |
1.1 2.2 3.3 4.4 |
|
247 |
1.1 |
|
262 |
1.1 2.2 3.3 |
|
272 |
1.1 |
|
274 |
1.1 |
|
276 |
1.1 2.2 3.3 |
|
278 |
1.1 |
|
281 |
1.1 2.2 3.3 |
|
282 |
1.1 |
|
283 |
1.1 2.2 |
|
286 |
1.1 |
|
289 |
1.1 |
|
292 |
1.1 |