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 | ||
27 | /** | |
28 | * <p> | |
29 | * Checks for long anonymous inner classes. | |
30 | * </p> | |
31 | * <p> | |
32 | * Rationale: If an anonymous inner class becomes very long | |
33 | * it is hard to understand and to see the flow of the method | |
34 | * where the class is defined. Therefore, long anonymous inner | |
35 | * classes should usually be refactored into a named inner class. | |
36 | * See also Bloch, Effective Java, p. 93. | |
37 | * </p> | |
38 | * <ul> | |
39 | * <li> | |
40 | * Property {@code max} - Specify the maximum number of lines allowed. | |
41 | * Type is {@code int}. | |
42 | * Default value is {@code 20}. | |
43 | * </li> | |
44 | * </ul> | |
45 | * <p> | |
46 | * To configure the check to accept anonymous classes with up to 20 lines: | |
47 | * </p> | |
48 | * <pre> | |
49 | * <module name="AnonInnerLength"/> | |
50 | * </pre> | |
51 | * <p> | |
52 | * To configure the check to accept anonymous classes with up to 60 lines: | |
53 | * </p> | |
54 | * <pre> | |
55 | * <module name="AnonInnerLength"> | |
56 | * <property name="max" value="60"/> | |
57 | * </module> | |
58 | * </pre> | |
59 | * <p> | |
60 | * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} | |
61 | * </p> | |
62 | * <p> | |
63 | * Violation Message Keys: | |
64 | * </p> | |
65 | * <ul> | |
66 | * <li> | |
67 | * {@code maxLen.anonInner} | |
68 | * </li> | |
69 | * </ul> | |
70 | * | |
71 | * @since 3.2 | |
72 | */ | |
73 | @StatelessCheck | |
74 | public class AnonInnerLengthCheck extends AbstractCheck { | |
75 | ||
76 | /** | |
77 | * A key is pointing to the warning message text in "messages.properties" | |
78 | * file. | |
79 | */ | |
80 | public static final String MSG_KEY = "maxLen.anonInner"; | |
81 | ||
82 | /** Default maximum number of lines. */ | |
83 | private static final int DEFAULT_MAX = 20; | |
84 | ||
85 | /** Specify the maximum number of lines allowed. */ | |
86 | private int max = DEFAULT_MAX; | |
87 | ||
88 | @Override | |
89 | public int[] getDefaultTokens() { | |
90 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/AnonInnerLengthCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getRequiredTokens(); |
91 | } | |
92 | ||
93 | @Override | |
94 | public int[] getAcceptableTokens() { | |
95 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/AnonInnerLengthCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getRequiredTokens(); |
96 | } | |
97 | ||
98 | @Override | |
99 | public int[] getRequiredTokens() { | |
100 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/AnonInnerLengthCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] {TokenTypes.LITERAL_NEW}; |
101 | } | |
102 | ||
103 | @Override | |
104 | public void visitToken(DetailAST ast) { | |
105 | final DetailAST openingBrace = ast.findFirstToken(TokenTypes.OBJBLOCK); | |
106 |
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 (openingBrace != null) { |
107 | final DetailAST closingBrace = | |
108 | openingBrace.findFirstToken(TokenTypes.RCURLY); | |
109 | final int length = | |
110 |
2
1. visitToken : Replaced integer subtraction with addition → KILLED 2. visitToken : Replaced integer addition with subtraction → KILLED |
closingBrace.getLineNo() - openingBrace.getLineNo() + 1; |
111 |
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) { |
112 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/sizes/AnonInnerLengthCheck::log → KILLED |
log(ast, MSG_KEY, length, max); |
113 | } | |
114 | } | |
115 | } | |
116 | ||
117 | /** | |
118 | * Setter to specify the maximum number of lines allowed. | |
119 | * | |
120 | * @param length the maximum length of an anonymous inner class. | |
121 | */ | |
122 | public void setMax(int length) { | |
123 | max = length; | |
124 | } | |
125 | ||
126 | } | |
Mutations | ||
90 |
1.1 |
|
95 |
1.1 |
|
100 |
1.1 |
|
106 |
1.1 2.2 3.3 |
|
110 |
1.1 2.2 |
|
111 |
1.1 2.2 3.3 4.4 |
|
112 |
1.1 |