AbstractNameCheck.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 java.util.regex.Pattern;
23
24
import com.puppycrawl.tools.checkstyle.StatelessCheck;
25
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26
import com.puppycrawl.tools.checkstyle.api.DetailAST;
27
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
29
30
/**
31
 * Abstract class for checking that names conform to a specified format.
32
 *
33
 * @author Rick Giles
34
 */
35
@StatelessCheck
36
public abstract class AbstractNameCheck
37
    extends AbstractCheck {
38
39
    /**
40
     * Message key for invalid pattern error.
41
     */
42
    public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
43
44
    /** The regexp to match against. */
45
    private Pattern format;
46
47
    /**
48
     * Creates a new {@code AbstractNameCheck} instance.
49
     * @param format format to check with
50
     */
51
    protected AbstractNameCheck(String format) {
52
        this.format = CommonUtils.createPattern(format);
53
    }
54
55
    /**
56
     * Decides whether the name of an AST should be checked against
57
     * the format regexp.
58
     * @param ast the AST to check.
59
     * @return true if the IDENT subnode of ast should be checked against
60
     *     the format regexp.
61
     */
62
    protected abstract boolean mustCheckName(DetailAST ast);
63
64
    /**
65
     * Set the format for the specified regular expression.
66
     * @param pattern the new pattern
67
     */
68
    public final void setFormat(Pattern pattern) {
69
        format = pattern;
70
    }
71
72
    @Override
73
    public void visitToken(DetailAST ast) {
74 1 1. visitToken : negated conditional → KILLED
        if (mustCheckName(ast)) {
75
            final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
76 1 1. visitToken : negated conditional → KILLED
            if (!format.matcher(nameAST.getText()).find()) {
77 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/naming/AbstractNameCheck::log → KILLED
                log(nameAST.getLineNo(),
78
                    nameAST.getColumnNo(),
79
                    MSG_INVALID_PATTERN,
80
                    nameAST.getText(),
81
                    format.pattern());
82
            }
83
        }
84
    }
85
86
}

Mutations

74

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ClassTypeParameterNameCheckTest.testClassFooName(com.puppycrawl.tools.checkstyle.checks.naming.ClassTypeParameterNameCheckTest)
negated conditional → KILLED

76

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ClassTypeParameterNameCheckTest.testClassFooName(com.puppycrawl.tools.checkstyle.checks.naming.ClassTypeParameterNameCheckTest)
negated conditional → KILLED

77

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.ClassTypeParameterNameCheckTest.testClassFooName(com.puppycrawl.tools.checkstyle.checks.naming.ClassTypeParameterNameCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/naming/AbstractNameCheck::log → KILLED

Active mutators

Tests examined


Report generated by PIT 1.3.1