NoWhitespaceBeforeCheck.java

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 com.puppycrawl.tools.checkstyle.api.AbstractCheck;
23
import com.puppycrawl.tools.checkstyle.api.DetailAST;
24
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
26
27
/**
28
 * <p>
29
 * Checks that there is no whitespace before a token.
30
 * More specifically, it checks that it is not preceded with whitespace,
31
 * or (if line breaks are allowed) all characters on the line before are
32
 * whitespace. To allow line breaks before a token, set property
33
 * allowLineBreaks to true.
34
 * </p>
35
 * <p> By default the check will check the following operators:
36
 *  {@link TokenTypes#COMMA COMMA},
37
 *  {@link TokenTypes#SEMI SEMI},
38
 *  {@link TokenTypes#POST_DEC POST_DEC},
39
 *  {@link TokenTypes#POST_INC POST_INC},
40
 *  {@link TokenTypes#ELLIPSIS ELLIPSIS}.
41
 * {@link TokenTypes#DOT DOT} is also an acceptable token in a configuration
42
 * of this check.
43
 * </p>
44
 *
45
 * <p>
46
 * An example of how to configure the check is:
47
 * </p>
48
 * <pre>
49
 * &lt;module name="NoWhitespaceBefore"/&gt;
50
 * </pre>
51
 * <p> An example of how to configure the check to allow line breaks before
52
 * a {@link TokenTypes#DOT DOT} token is:
53
 * </p>
54
 * <pre>
55
 * &lt;module name="NoWhitespaceBefore"&gt;
56
 *     &lt;property name="tokens" value="DOT"/&gt;
57
 *     &lt;property name="allowLineBreaks" value="true"/&gt;
58
 * &lt;/module&gt;
59
 * </pre>
60
 * @author Rick Giles
61
 * @author lkuehne
62
 */
63
public class NoWhitespaceBeforeCheck
64
    extends AbstractCheck {
65
66
    /**
67
     * A key is pointing to the warning message text in "messages.properties"
68
     * file.
69
     */
70
    public static final String MSG_KEY = "ws.preceded";
71
72
    /** Whether whitespace is allowed if the AST is at a linebreak. */
73
    private boolean allowLineBreaks;
74
75
    @Override
76
    public int[] getDefaultTokens() {
77 1 1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return new int[] {
78
            TokenTypes.COMMA,
79
            TokenTypes.SEMI,
80
            TokenTypes.POST_INC,
81
            TokenTypes.POST_DEC,
82
            TokenTypes.ELLIPSIS,
83
        };
84
    }
85
86
    @Override
87
    public int[] getAcceptableTokens() {
88 1 1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return new int[] {
89
            TokenTypes.COMMA,
90
            TokenTypes.SEMI,
91
            TokenTypes.POST_INC,
92
            TokenTypes.POST_DEC,
93
            TokenTypes.DOT,
94
            TokenTypes.GENERIC_START,
95
            TokenTypes.GENERIC_END,
96
            TokenTypes.ELLIPSIS,
97
            TokenTypes.METHOD_REF,
98
        };
99
    }
100
101
    @Override
102
    public int[] getRequiredTokens() {
103 1 1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return CommonUtils.EMPTY_INT_ARRAY;
104
    }
105
106
    @Override
107
    public void visitToken(DetailAST ast) {
108 1 1. visitToken : Replaced integer subtraction with addition → KILLED
        final String line = getLine(ast.getLineNo() - 1);
109 1 1. visitToken : Replaced integer subtraction with addition → KILLED
        final int before = ast.getColumnNo() - 1;
110
111 2 1. visitToken : negated conditional → KILLED
2. visitToken : negated conditional → KILLED
        if ((before == -1 || Character.isWhitespace(line.charAt(before)))
112 1 1. visitToken : negated conditional → KILLED
                && !isInEmptyForInitializer(ast)) {
113
114 1 1. visitToken : negated conditional → KILLED
            boolean flag = !allowLineBreaks;
115
            // verify all characters before '.' are whitespace
116 5 1. visitToken : changed conditional boundary → KILLED
2. visitToken : Changed increment from 1 to -1 → KILLED
3. visitToken : Replaced integer subtraction with addition → KILLED
4. visitToken : negated conditional → KILLED
5. visitToken : negated conditional → KILLED
            for (int i = 0; !flag && i <= before - 1; i++) {
117 1 1. visitToken : negated conditional → KILLED
                if (!Character.isWhitespace(line.charAt(i))) {
118
                    flag = true;
119
                    break;
120
                }
121
            }
122 1 1. visitToken : negated conditional → KILLED
            if (flag) {
123 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::log → KILLED
                log(ast.getLineNo(), before, MSG_KEY, ast.getText());
124
            }
125
        }
126
    }
127
128
    /**
129
     * Checks that semicolon is in empty for initializer.
130
     * @param semicolonAst DetailAST of semicolon.
131
     * @return true if semicolon is in empty for initializer.
132
     */
133
    private static boolean isInEmptyForInitializer(DetailAST semicolonAst) {
134
        boolean result = false;
135 1 1. isInEmptyForInitializer : negated conditional → KILLED
        if (semicolonAst.getType() == TokenTypes.SEMI) {
136
            final DetailAST sibling = semicolonAst.getPreviousSibling();
137 1 1. isInEmptyForInitializer : negated conditional → KILLED
            if (sibling != null
138 1 1. isInEmptyForInitializer : negated conditional → KILLED
                    && sibling.getType() == TokenTypes.FOR_INIT
139 1 1. isInEmptyForInitializer : negated conditional → KILLED
                    && sibling.getChildCount() == 0) {
140
                result = true;
141
            }
142
        }
143 1 1. isInEmptyForInitializer : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED
        return result;
144
    }
145
146
    /**
147
     * Control whether whitespace is flagged at line breaks.
148
     * @param allowLineBreaks whether whitespace should be
149
     *     flagged at line breaks.
150
     */
151
    public void setAllowLineBreaks(boolean allowLineBreaks) {
152
        this.allowLineBreaks = allowLineBreaks;
153
    }
154
}

Mutations

77

1.1
Location : getDefaultTokens
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

88

1.1
Location : getAcceptableTokens
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

103

1.1
Location : getRequiredTokens
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

108

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
Replaced integer subtraction with addition → KILLED

109

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
Replaced integer subtraction with addition → KILLED

111

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

112

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

114

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

116

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testMethodRefAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
changed conditional boundary → KILLED

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAllowLineBreaks(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
Changed increment from 1 to -1 → KILLED

3.3
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAllowLineBreaks(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
Replaced integer subtraction with addition → KILLED

4.4
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testMethodRefAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

5.5
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testMethodRefAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

117

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testMethodRefAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

122

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

123

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::log → KILLED

135

1.1
Location : isInEmptyForInitializer
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

137

1.1
Location : isInEmptyForInitializer
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

138

1.1
Location : isInEmptyForInitializer
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

139

1.1
Location : isInEmptyForInitializer
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
negated conditional → KILLED

143

1.1
Location : isInEmptyForInitializer
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest.testDotAtTheStartOfTheLine(com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheckTest)
replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED

Active mutators

Tests examined


Report generated by PIT 1.2.2