1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2018 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.coding; | |
21 | ||
22 | import com.puppycrawl.tools.checkstyle.FileStatefulCheck; | |
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 | ||
27 | /** | |
28 | * Check the number of nested {@code for} -statements. The maximum | |
29 | * number of nested layers can be configured. The default value is 1. | |
30 | * The code for the class is copied from the NestedIfDepthCheck-class. | |
31 | * The only difference is the intercepted token (for instead of if). | |
32 | * Example: | |
33 | * <pre> | |
34 | * <!-- Restricts nested for blocks to a specified depth (default = 1). | |
35 | * --> | |
36 | * <module name="com.puppycrawl.tools.checkstyle.checks.coding | |
37 | * .CatchWithLostStackCheck"> | |
38 | * <property name="severity" value="info"/> | |
39 | * <property name="max" value="1"/> | |
40 | * </module> | |
41 | * </pre> | |
42 | * @author Alexander Jesse | |
43 | * @see NestedIfDepthCheck | |
44 | */ | |
45 | @FileStatefulCheck | |
46 | public final class NestedForDepthCheck extends AbstractCheck { | |
47 | ||
48 | /** | |
49 | * A key is pointing to the warning message text in "messages.properties" | |
50 | * file. | |
51 | */ | |
52 | public static final String MSG_KEY = "nested.for.depth"; | |
53 | ||
54 | /** Maximum allowed nesting depth. */ | |
55 | private int max = 1; | |
56 | /** Current nesting depth. */ | |
57 | private int depth; | |
58 | ||
59 | /** | |
60 | * Setter for maximum allowed nesting depth. | |
61 | * @param max maximum allowed nesting depth. | |
62 | */ | |
63 | public void setMax(int max) { | |
64 | this.max = max; | |
65 | } | |
66 | ||
67 | @Override | |
68 | public int[] getDefaultTokens() { | |
69 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/NestedForDepthCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getRequiredTokens(); |
70 | } | |
71 | ||
72 | @Override | |
73 | public int[] getAcceptableTokens() { | |
74 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/NestedForDepthCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getRequiredTokens(); |
75 | } | |
76 | ||
77 | @Override | |
78 | public int[] getRequiredTokens() { | |
79 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/NestedForDepthCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] {TokenTypes.LITERAL_FOR}; |
80 | } | |
81 | ||
82 | @Override | |
83 | public void beginTree(DetailAST rootAST) { | |
84 | depth = 0; | |
85 | } | |
86 | ||
87 | @Override | |
88 | public void visitToken(DetailAST ast) { | |
89 |
2
1. visitToken : changed conditional boundary → KILLED 2. visitToken : negated conditional → KILLED |
if (depth > max) { |
90 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/coding/NestedForDepthCheck::log → KILLED |
log(ast, MSG_KEY, depth, max); |
91 | } | |
92 |
1
1. visitToken : Replaced integer addition with subtraction → KILLED |
++depth; |
93 | } | |
94 | ||
95 | @Override | |
96 | public void leaveToken(DetailAST ast) { | |
97 |
1
1. leaveToken : Replaced integer subtraction with addition → KILLED |
--depth; |
98 | } | |
99 | ||
100 | } | |
Mutations | ||
69 |
1.1 |
|
74 |
1.1 |
|
79 |
1.1 |
|
89 |
1.1 2.2 |
|
90 |
1.1 |
|
92 |
1.1 |
|
97 |
1.1 |