WhitespaceAfterCheck.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 a token is followed by whitespace, with the exception that it
30
 * does not check for whitespace after the semicolon of an empty for iterator.
31
 * Use Check {@link EmptyForIteratorPadCheck EmptyForIteratorPad} to validate
32
 * empty for iterators.
33
 * </p>
34
 * <p> By default the check will check the following tokens:
35
 *  {@link TokenTypes#COMMA COMMA},
36
 *  {@link TokenTypes#SEMI SEMI},
37
 *  {@link TokenTypes#TYPECAST TYPECAST},
38
 *  {@link TokenTypes#LITERAL_IF LITERAL_IF},
39
 *  {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE},
40
 *  {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE},
41
 *  {@link TokenTypes#LITERAL_FOR LITERAL_FOR},
42
 *  {@link TokenTypes#LITERAL_DO LITERAL_DO},
43
 *  {@link TokenTypes#DO_WHILE DO_WHILE}.
44
 * </p>
45
 * <p>
46
 * An example of how to configure the check is:
47
 * </p>
48
 * <pre>
49
 * &lt;module name="WhitespaceAfter"/&gt;
50
 * </pre>
51
 * <p> An example of how to configure the check for whitespace only after
52
 * {@link TokenTypes#COMMA COMMA} and {@link TokenTypes#SEMI SEMI} tokens is:
53
 * </p>
54
 * <pre>
55
 * &lt;module name="WhitespaceAfter"&gt;
56
 *     &lt;property name="tokens" value="COMMA, SEMI"/&gt;
57
 * &lt;/module&gt;
58
 * </pre>
59
 * @author Oliver Burn
60
 * @author Rick Giles
61
 */
62
public class WhitespaceAfterCheck
63
    extends AbstractCheck {
64
65
    /**
66
     * A key is pointing to the warning message text in "messages.properties"
67
     * file.
68
     */
69
    public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
70
71
    /**
72
     * A key is pointing to the warning message text in "messages.properties"
73
     * file.
74
     */
75
    public static final String MSG_WS_TYPECAST = "ws.typeCast";
76
77
    @Override
78
    public int[] getDefaultTokens() {
79 1 1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getAcceptableTokens();
80
    }
81
82
    @Override
83
    public int[] getAcceptableTokens() {
84 1 1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return new int[] {
85
            TokenTypes.COMMA,
86
            TokenTypes.SEMI,
87
            TokenTypes.TYPECAST,
88
            TokenTypes.LITERAL_IF,
89
            TokenTypes.LITERAL_ELSE,
90
            TokenTypes.LITERAL_WHILE,
91
            TokenTypes.LITERAL_DO,
92
            TokenTypes.LITERAL_FOR,
93
            TokenTypes.DO_WHILE,
94
        };
95
    }
96
97
    @Override
98
    public int[] getRequiredTokens() {
99 1 1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return CommonUtils.EMPTY_INT_ARRAY;
100
    }
101
102
    @Override
103
    public void visitToken(DetailAST ast) {
104 1 1. visitToken : negated conditional → KILLED
        if (ast.getType() == TokenTypes.TYPECAST) {
105
            final DetailAST targetAST = ast.findFirstToken(TokenTypes.RPAREN);
106 1 1. visitToken : Replaced integer subtraction with addition → KILLED
            final String line = getLine(targetAST.getLineNo() - 1);
107 1 1. visitToken : negated conditional → KILLED
            if (!isFollowedByWhitespace(targetAST, line)) {
108 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck::log → KILLED
                log(targetAST.getLineNo(),
109 1 1. visitToken : Replaced integer addition with subtraction → KILLED
                    targetAST.getColumnNo() + targetAST.getText().length(),
110
                    MSG_WS_TYPECAST);
111
            }
112
        }
113
        else {
114 1 1. visitToken : Replaced integer subtraction with addition → KILLED
            final String line = getLine(ast.getLineNo() - 1);
115 1 1. visitToken : negated conditional → KILLED
            if (!isFollowedByWhitespace(ast, line)) {
116
                final Object[] message = {ast.getText()};
117 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck::log → KILLED
                log(ast.getLineNo(),
118 1 1. visitToken : Replaced integer addition with subtraction → KILLED
                    ast.getColumnNo() + ast.getText().length(),
119
                    MSG_WS_NOT_FOLLOWED,
120
                    message);
121
            }
122
        }
123
    }
124
125
    /**
126
     * Checks whether token is followed by a whitespace.
127
     * @param targetAST Ast token.
128
     * @param line The line associated with the ast token.
129
     * @return true if ast token is followed by a whitespace.
130
     */
131
    private static boolean isFollowedByWhitespace(DetailAST targetAST, String line) {
132
        final int after =
133 1 1. isFollowedByWhitespace : Replaced integer addition with subtraction → KILLED
            targetAST.getColumnNo() + targetAST.getText().length();
134
        boolean followedByWhitespace = true;
135
136 2 1. isFollowedByWhitespace : changed conditional boundary → KILLED
2. isFollowedByWhitespace : negated conditional → KILLED
        if (after < line.length()) {
137
            final char charAfter = line.charAt(after);
138 2 1. isFollowedByWhitespace : negated conditional → KILLED
2. isFollowedByWhitespace : negated conditional → KILLED
            followedByWhitespace = charAfter == ';'
139
                || charAfter == ')'
140 1 1. isFollowedByWhitespace : negated conditional → KILLED
                || Character.isWhitespace(charAfter);
141
        }
142 1 1. isFollowedByWhitespace : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED
        return followedByWhitespace;
143
    }
144
}

Mutations

79

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

84

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

99

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

104

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

106

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

107

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

108

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest.testMultilineCast(com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck::log → KILLED

109

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

114

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

115

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

117

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest.testLiteralDo(com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck::log → KILLED

118

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

133

1.1
Location : isFollowedByWhitespace
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest.testLiteralDo(com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest)
Replaced integer addition with subtraction → KILLED

136

1.1
Location : isFollowedByWhitespace
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest.testSemi(com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest)
changed conditional boundary → KILLED

2.2
Location : isFollowedByWhitespace
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest.testLiteralDo(com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest)
negated conditional → KILLED

138

1.1
Location : isFollowedByWhitespace
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest.testLiteralDo(com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest)
negated conditional → KILLED

2.2
Location : isFollowedByWhitespace
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest.testLiteralDo(com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest)
negated conditional → KILLED

140

1.1
Location : isFollowedByWhitespace
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest.testLiteralDo(com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheckTest)
negated conditional → KILLED

142

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

Active mutators

Tests examined


Report generated by PIT 1.2.2