1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2017 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.whitespace; | |
21 | ||
22 | import java.util.Locale; | |
23 | ||
24 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
25 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
26 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
27 | import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | |
28 | ||
29 | /** | |
30 | * <p>Checks the padding of an empty for initializer; that is whether a | |
31 | * space is required at an empty for initializer, or such spaces are | |
32 | * forbidden. No check occurs if there is a line wrap at the initializer, as in | |
33 | * </p> | |
34 | * <pre class="body"> | |
35 | for ( | |
36 | ; i < j; i++, j--) | |
37 | </pre> | |
38 | * <p> | |
39 | * The policy to verify is specified using the {@link PadOption} class and | |
40 | * defaults to {@link PadOption#NOSPACE}. | |
41 | * </p> | |
42 | * <p> | |
43 | * An example of how to configure the check is: | |
44 | * </p> | |
45 | * <pre> | |
46 | * <module name="EmptyForInitializerPad"/> | |
47 | * </pre> | |
48 | * | |
49 | * @author lkuehne | |
50 | */ | |
51 | public class EmptyForInitializerPadCheck | |
52 | extends AbstractCheck { | |
53 | ||
54 | /** | |
55 | * A key is pointing to the warning message text in "messages.properties" | |
56 | * file. | |
57 | */ | |
58 | public static final String MSG_PRECEDED = "ws.preceded"; | |
59 | ||
60 | /** | |
61 | * A key is pointing to the warning message text in "messages.properties" | |
62 | * file. | |
63 | */ | |
64 | public static final String MSG_NOT_PRECEDED = "ws.notPreceded"; | |
65 | ||
66 | /** Semicolon literal. */ | |
67 | private static final String SEMICOLON = ";"; | |
68 | ||
69 | /** The policy to enforce. */ | |
70 | private PadOption option = PadOption.NOSPACE; | |
71 | ||
72 | /** | |
73 | * Set the option to enforce. | |
74 | * @param optionStr string to decode option from | |
75 | * @throws IllegalArgumentException if unable to decode | |
76 | */ | |
77 | public void setOption(String optionStr) { | |
78 | try { | |
79 | option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); | |
80 | } | |
81 | catch (IllegalArgumentException iae) { | |
82 | throw new IllegalArgumentException("unable to parse " + optionStr, iae); | |
83 | } | |
84 | } | |
85 | ||
86 | @Override | |
87 | public int[] getDefaultTokens() { | |
88 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getAcceptableTokens(); |
89 | } | |
90 | ||
91 | @Override | |
92 | public int[] getAcceptableTokens() { | |
93 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] {TokenTypes.FOR_INIT}; |
94 | } | |
95 | ||
96 | @Override | |
97 | public int[] getRequiredTokens() { | |
98 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getAcceptableTokens(); |
99 | } | |
100 | ||
101 | @Override | |
102 | public void visitToken(DetailAST ast) { | |
103 |
1
1. visitToken : negated conditional → KILLED |
if (ast.getChildCount() == 0) { |
104 | //empty for initializer. test pad before semi. | |
105 | final DetailAST semi = ast.getNextSibling(); | |
106 |
1
1. visitToken : Replaced integer subtraction with addition → KILLED |
final int semiLineIdx = semi.getLineNo() - 1; |
107 | final String line = getLines()[semiLineIdx]; | |
108 |
1
1. visitToken : Replaced integer subtraction with addition → KILLED |
final int before = semi.getColumnNo() - 1; |
109 | //don't check if semi at beginning of line | |
110 |
1
1. visitToken : negated conditional → KILLED |
if (!CommonUtils.hasWhitespaceBefore(before, line)) { |
111 |
1
1. visitToken : negated conditional → KILLED |
if (option == PadOption.NOSPACE |
112 |
1
1. visitToken : negated conditional → KILLED |
&& Character.isWhitespace(line.charAt(before))) { |
113 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck::log → KILLED |
log(semi.getLineNo(), before, MSG_PRECEDED, SEMICOLON); |
114 | } | |
115 |
1
1. visitToken : negated conditional → KILLED |
else if (option == PadOption.SPACE |
116 |
1
1. visitToken : negated conditional → KILLED |
&& !Character.isWhitespace(line.charAt(before))) { |
117 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck::log → KILLED |
log(semi.getLineNo(), before, MSG_NOT_PRECEDED, SEMICOLON); |
118 | } | |
119 | } | |
120 | } | |
121 | } | |
122 | } | |
Mutations | ||
88 |
1.1 |
|
93 |
1.1 |
|
98 |
1.1 |
|
103 |
1.1 |
|
106 |
1.1 |
|
108 |
1.1 |
|
110 |
1.1 |
|
111 |
1.1 |
|
112 |
1.1 |
|
113 |
1.1 |
|
115 |
1.1 |
|
116 |
1.1 |
|
117 |
1.1 |