MissingSwitchDefaultCheck.java

1
////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2018 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.coding;
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 that switch statement has &quot;default&quot; clause.
30
 * </p>
31
 * <p>
32
 * Rationale: It's usually a good idea to introduce a
33
 * default case in every switch statement. Even if
34
 * the developer is sure that all currently possible
35
 * cases are covered, this should be expressed in the
36
 * default branch, e.g. by using an assertion. This way
37
 * the code is protected against later changes, e.g.
38
 * introduction of new types in an enumeration type.
39
 * </p>
40
 * <p>
41
 * An example of how to configure the check is:
42
 * </p>
43
 * <pre>
44
 * &lt;module name="MissingSwitchDefault"/&gt;
45
 * </pre>
46
 * @author o_sukhodolsky
47
 */
48
@StatelessCheck
49
public class MissingSwitchDefaultCheck extends AbstractCheck {
50
51
    /**
52
     * A key is pointing to the warning message text in "messages.properties"
53
     * file.
54
     */
55
    public static final String MSG_KEY = "missing.switch.default";
56
57
    @Override
58
    public int[] getDefaultTokens() {
59 1 1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/MissingSwitchDefaultCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getRequiredTokens();
60
    }
61
62
    @Override
63
    public int[] getAcceptableTokens() {
64 1 1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/MissingSwitchDefaultCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getRequiredTokens();
65
    }
66
67
    @Override
68
    public int[] getRequiredTokens() {
69 1 1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/MissingSwitchDefaultCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return new int[] {TokenTypes.LITERAL_SWITCH};
70
    }
71
72
    @Override
73
    public void visitToken(DetailAST ast) {
74
        final DetailAST firstCaseGroupAst = ast.findFirstToken(TokenTypes.CASE_GROUP);
75
76 1 1. visitToken : negated conditional → KILLED
        if (!containsDefaultSwitch(firstCaseGroupAst)) {
77 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/coding/MissingSwitchDefaultCheck::log → KILLED
            log(ast.getLineNo(), MSG_KEY);
78
        }
79
    }
80
81
    /**
82
     * Checks if the case group or its sibling contain the 'default' switch.
83
     * @param caseGroupAst first case group to check.
84
     * @return true if 'default' switch found.
85
     */
86
    private static boolean containsDefaultSwitch(DetailAST caseGroupAst) {
87
        DetailAST nextAst = caseGroupAst;
88
        boolean found = false;
89
90 1 1. containsDefaultSwitch : negated conditional → KILLED
        while (nextAst != null) {
91 1 1. containsDefaultSwitch : negated conditional → KILLED
            if (nextAst.findFirstToken(TokenTypes.LITERAL_DEFAULT) != null) {
92
                found = true;
93
                break;
94
            }
95
96
            nextAst = nextAst.getNextSibling();
97
        }
98
99 1 1. containsDefaultSwitch : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED
        return found;
100
    }
101
102
}

Mutations

59

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

64

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

69

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

76

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest.testMissingSwitchDefault(com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest)
negated conditional → KILLED

77

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest.testMissingSwitchDefault(com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/coding/MissingSwitchDefaultCheck::log → KILLED

90

1.1
Location : containsDefaultSwitch
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest.testMissingSwitchDefault(com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest)
negated conditional → KILLED

91

1.1
Location : containsDefaultSwitch
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest.testMissingSwitchDefault(com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest)
negated conditional → KILLED

99

1.1
Location : containsDefaultSwitch
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest.testMissingSwitchDefault(com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest)
replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED

Active mutators

Tests examined


Report generated by PIT 1.3.1