ConstantNameCheck.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.naming;
21
22
import com.puppycrawl.tools.checkstyle.api.DetailAST;
23
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24
import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
25
26
/**
27
 * <p>
28
 * Checks that constant names conform to a format specified
29
 * by the format property.
30
 * A <em>constant</em> is a <strong>static</strong> and <strong>final</strong>
31
 * field or an interface/annotation field, except
32
 * <strong>serialVersionUID</strong> and <strong>serialPersistentFields
33
 * </strong>.  The format is a regular expression
34
 * and defaults to <strong>^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$</strong>.
35
 * </p>
36
 * <p>
37
 * An example of how to configure the check is:
38
 * </p>
39
 * <pre>
40
 * &lt;module name="ConstantName"/&gt;
41
 * </pre>
42
 *
43
 * <p>
44
 * An example of how to configure the check for names that are only upper case
45
 * letters and digits is:
46
 * </p>
47
 * <pre>
48
 * &lt;module name="ConstantName"&gt;
49
 *    &lt;property name="format" value="^[A-Z][A-Z0-9]*$"/&gt;
50
 * &lt;/module&gt;
51
 * </pre>
52
 *
53
 *
54
 * @author Rick Giles
55
 */
56
public class ConstantNameCheck
57
    extends AbstractAccessControlNameCheck {
58
59
    /** Creates a new {@code ConstantNameCheck} instance. */
60
    public ConstantNameCheck() {
61
        super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$");
62
    }
63
64
    @Override
65
    public int[] getDefaultTokens() {
66 1 1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/naming/ConstantNameCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getRequiredTokens();
67
    }
68
69
    @Override
70
    public int[] getAcceptableTokens() {
71 1 1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/naming/ConstantNameCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getRequiredTokens();
72
    }
73
74
    @Override
75
    public int[] getRequiredTokens() {
76 1 1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/naming/ConstantNameCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return new int[] {TokenTypes.VARIABLE_DEF};
77
    }
78
79
    @Override
80
    protected final boolean mustCheckName(DetailAST ast) {
81
        boolean returnValue = false;
82
83
        final DetailAST modifiersAST =
84
            ast.findFirstToken(TokenTypes.MODIFIERS);
85 1 1. mustCheckName : negated conditional → KILLED
        final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
86 1 1. mustCheckName : negated conditional → KILLED
        final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
87
88 3 1. mustCheckName : negated conditional → KILLED
2. mustCheckName : negated conditional → KILLED
3. mustCheckName : negated conditional → KILLED
        if (isStatic && isFinal && shouldCheckInScope(modifiersAST)
89 1 1. mustCheckName : negated conditional → KILLED
                || ScopeUtils.isInAnnotationBlock(ast)
90 1 1. mustCheckName : negated conditional → KILLED
                || ScopeUtils.isInInterfaceOrAnnotationBlock(ast)
91 1 1. mustCheckName : negated conditional → KILLED
                        && !ScopeUtils.isInCodeBlock(ast)) {
92
            // Handle the serialVersionUID and serialPersistentFields constants
93
            // which are used for Serialization. Cannot enforce rules on it. :-)
94
            final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
95 1 1. mustCheckName : negated conditional → KILLED
            if (!"serialVersionUID".equals(nameAST.getText())
96 1 1. mustCheckName : negated conditional → KILLED
                && !"serialPersistentFields".equals(nameAST.getText())) {
97
                returnValue = true;
98
            }
99
        }
100
101 1 1. mustCheckName : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED
        return returnValue;
102
    }
103
104
}

Mutations

66

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

71

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

76

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

85

1.1
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
negated conditional → KILLED

86

1.1
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
negated conditional → KILLED

88

1.1
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
negated conditional → KILLED

2.2
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
negated conditional → KILLED

3.3
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
negated conditional → KILLED

89

1.1
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testStaticMethodInInterface(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
negated conditional → KILLED

90

1.1
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
negated conditional → KILLED

91

1.1
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testStaticMethodInInterface(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
negated conditional → KILLED

95

1.1
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testDefault1(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
negated conditional → KILLED

96

1.1
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testDefault1(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
negated conditional → KILLED

101

1.1
Location : mustCheckName
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest.testStaticMethodInInterface(com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheckTest)
replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED

Active mutators

Tests examined


Report generated by PIT 1.3.1