AbstractParenPadCheck.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 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.utils.CommonUtils;
27
28
/**
29
 * <p>Abstract class for checking the padding of parentheses. That is whether a
30
 * space is required after a left parenthesis and before a right parenthesis,
31
 * or such spaces are forbidden.
32
 * </p>
33
 * @author Oliver Burn
34
 */
35
public abstract class AbstractParenPadCheck
36
    extends AbstractCheck {
37
38
    /**
39
     * A key is pointing to the warning message text in "messages.properties"
40
     * file.
41
     */
42
    public static final String MSG_WS_FOLLOWED = "ws.followed";
43
44
    /**
45
     * A key is pointing to the warning message text in "messages.properties"
46
     * file.
47
     */
48
    public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
49
50
    /**
51
     * A key is pointing to the warning message text in "messages.properties"
52
     * file.
53
     */
54
    public static final String MSG_WS_PRECEDED = "ws.preceded";
55
56
    /**
57
     * A key is pointing to the warning message text in "messages.properties"
58
     * file.
59
     */
60
    public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded";
61
62
    /** Open parenthesis literal. */
63
    private static final char OPEN_PARENTHESIS = '(';
64
65
    /** Close parenthesis literal. */
66
    private static final char CLOSE_PARENTHESIS = ')';
67
68
    /** The policy to enforce. */
69
    private PadOption option = PadOption.NOSPACE;
70
71
    /**
72
     * Set the option to enforce.
73
     * @param optionStr string to decode option from
74
     * @throws IllegalArgumentException if unable to decode
75
     */
76
    public void setOption(String optionStr) {
77
        try {
78
            option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
79
        }
80
        catch (IllegalArgumentException iae) {
81
            throw new IllegalArgumentException("unable to parse " + optionStr, iae);
82
        }
83
    }
84
85
    /**
86
     * Process a token representing a left parentheses.
87
     * @param ast the token representing a left parentheses
88
     */
89
    protected void processLeft(DetailAST ast) {
90 1 1. processLeft : Replaced integer subtraction with addition → KILLED
        final String line = getLines()[ast.getLineNo() - 1];
91 1 1. processLeft : Replaced integer addition with subtraction → KILLED
        final int after = ast.getColumnNo() + 1;
92 2 1. processLeft : changed conditional boundary → KILLED
2. processLeft : negated conditional → KILLED
        if (after < line.length()) {
93 1 1. processLeft : negated conditional → KILLED
            if (option == PadOption.NOSPACE
94 1 1. processLeft : negated conditional → KILLED
                && Character.isWhitespace(line.charAt(after))) {
95 1 1. processLeft : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/AbstractParenPadCheck::log → KILLED
                log(ast.getLineNo(), after, MSG_WS_FOLLOWED, OPEN_PARENTHESIS);
96
            }
97 1 1. processLeft : negated conditional → KILLED
            else if (option == PadOption.SPACE
98 1 1. processLeft : negated conditional → KILLED
                     && !Character.isWhitespace(line.charAt(after))
99 1 1. processLeft : negated conditional → KILLED
                     && line.charAt(after) != CLOSE_PARENTHESIS) {
100 1 1. processLeft : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/AbstractParenPadCheck::log → KILLED
                log(ast.getLineNo(), after, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS);
101
            }
102
        }
103
    }
104
105
    /**
106
     * Process a token representing a right parentheses.
107
     * @param ast the token representing a right parentheses
108
     */
109
    protected void processRight(DetailAST ast) {
110 1 1. processRight : Replaced integer subtraction with addition → KILLED
        final int before = ast.getColumnNo() - 1;
111 2 1. processRight : changed conditional boundary → KILLED
2. processRight : negated conditional → KILLED
        if (before >= 0) {
112 1 1. processRight : Replaced integer subtraction with addition → KILLED
            final String line = getLines()[ast.getLineNo() - 1];
113 1 1. processRight : negated conditional → KILLED
            if (option == PadOption.NOSPACE
114 1 1. processRight : negated conditional → KILLED
                && Character.isWhitespace(line.charAt(before))
115 1 1. processRight : negated conditional → KILLED
                && !CommonUtils.hasWhitespaceBefore(before, line)) {
116 1 1. processRight : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/AbstractParenPadCheck::log → KILLED
                log(ast.getLineNo(), before, MSG_WS_PRECEDED, CLOSE_PARENTHESIS);
117
            }
118 1 1. processRight : negated conditional → KILLED
            else if (option == PadOption.SPACE
119 1 1. processRight : negated conditional → KILLED
                && !Character.isWhitespace(line.charAt(before))
120 1 1. processRight : negated conditional → KILLED
                && line.charAt(before) != OPEN_PARENTHESIS) {
121 1 1. processRight : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/AbstractParenPadCheck::log → KILLED
                log(ast.getLineNo(), ast.getColumnNo(),
122
                    MSG_WS_NOT_PRECEDED, CLOSE_PARENTHESIS);
123
            }
124
        }
125
    }
126
}

Mutations

90

1.1
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnly(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
Replaced integer subtraction with addition → KILLED

91

1.1
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
Replaced integer addition with subtraction → KILLED

92

1.1
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
changed conditional boundary → KILLED

2.2
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnly(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

93

1.1
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

94

1.1
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnly(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

95

1.1
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnly(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/AbstractParenPadCheck::log → KILLED

97

1.1
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnly(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

98

1.1
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

99

1.1
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckDisabledWithSpace(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

100

1.1
Location : processLeft
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckDisabledWithSpace(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/AbstractParenPadCheck::log → KILLED

110

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
Replaced integer subtraction with addition → KILLED

111

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
changed conditional boundary → KILLED

2.2
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

112

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
Replaced integer subtraction with addition → KILLED

113

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnly(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

114

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnly(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

115

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnly(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

116

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnly(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/AbstractParenPadCheck::log → KILLED

118

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

119

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

120

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
negated conditional → KILLED

121

1.1
Location : processRight
Killed by : com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest.testLambdaCheckOnlyWithSpace1(com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/AbstractParenPadCheck::log → KILLED

Active mutators

Tests examined


Report generated by PIT 1.2.2