AbstractAccessControlNameCheck.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
25
/**
26
 * Abstract class for checking a class member (field/method)'s name conforms to
27
 * a format specified by the format property.
28
 *
29
 * <p>
30
 * This class extends {@link AbstractNameCheck} with support for access level
31
 * restrictions. This allows the check to be configured to be applied to one of
32
 * the four Java access levels: {@code public}, {@code protected},
33
 * {@code "package"}, and {@code private}.
34
 * </p>
35
 *
36
 * <p>Level is configured using the following properties:
37
 * <ol>
38
 * <li>applyToPublic, default true;</li>
39
 * <li>applyToProtected, default true;</li>
40
 * <li>applyToPackage, default true;</li>
41
 * <li>applyToPrivate, default true;</li>
42
 * </ol>
43
 *
44
 *
45
 * @author Rick Giles
46
 */
47
public abstract class AbstractAccessControlNameCheck
48
    extends AbstractNameCheck {
49
50
    /** If true, applies the check be public members. */
51
    private boolean applyToPublic = true;
52
53
    /** If true, applies the check be protected members. */
54
    private boolean applyToProtected = true;
55
56
    /** If true, applies the check be "package" members. */
57
    private boolean applyToPackage = true;
58
59
    /** If true, applies the check be private members. */
60
    private boolean applyToPrivate = true;
61
62
    /**
63
     * Creates a new {@code AbstractAccessControlNameCheck} instance.
64
     *
65
     * @param format
66
     *                format to check with
67
     */
68
    protected AbstractAccessControlNameCheck(String format) {
69
        super(format);
70
    }
71
72
    @Override
73
    protected boolean mustCheckName(DetailAST ast) {
74 1 1. mustCheckName : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED
        return shouldCheckInScope(ast.findFirstToken(TokenTypes.MODIFIERS));
75
    }
76
77
    /**
78
     * Should we check member with given modifiers.
79
     *
80
     * @param modifiers
81
     *                modifiers of member to check.
82
     * @return true if we should check such member.
83
     */
84
    protected boolean shouldCheckInScope(DetailAST modifiers) {
85
        final boolean isPublic = modifiers
86 1 1. shouldCheckInScope : negated conditional → KILLED
                .findFirstToken(TokenTypes.LITERAL_PUBLIC) != null;
87
        final boolean isProtected = modifiers
88 1 1. shouldCheckInScope : negated conditional → KILLED
                .findFirstToken(TokenTypes.LITERAL_PROTECTED) != null;
89
        final boolean isPrivate = modifiers
90 1 1. shouldCheckInScope : negated conditional → KILLED
                .findFirstToken(TokenTypes.LITERAL_PRIVATE) != null;
91 3 1. shouldCheckInScope : negated conditional → KILLED
2. shouldCheckInScope : negated conditional → KILLED
3. shouldCheckInScope : negated conditional → KILLED
        final boolean isPackage = !(isPublic || isProtected || isPrivate);
92
93 9 1. shouldCheckInScope : negated conditional → KILLED
2. shouldCheckInScope : negated conditional → KILLED
3. shouldCheckInScope : negated conditional → KILLED
4. shouldCheckInScope : negated conditional → KILLED
5. shouldCheckInScope : negated conditional → KILLED
6. shouldCheckInScope : negated conditional → KILLED
7. shouldCheckInScope : negated conditional → KILLED
8. shouldCheckInScope : negated conditional → KILLED
9. shouldCheckInScope : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED
        return applyToPublic && isPublic
94
                || applyToProtected && isProtected
95
                || applyToPackage && isPackage
96
                || applyToPrivate && isPrivate;
97
    }
98
99
    /**
100
     * Sets whether we should apply the check to public members.
101
     *
102
     * @param applyTo new value of the property.
103
     */
104
    public void setApplyToPublic(boolean applyTo) {
105
        applyToPublic = applyTo;
106
    }
107
108
    /**
109
     * Sets whether we should apply the check to protected members.
110
     *
111
     * @param applyTo new value of the property.
112
     */
113
    public void setApplyToProtected(boolean applyTo) {
114
        applyToProtected = applyTo;
115
    }
116
117
    /**
118
     * Sets whether we should apply the check to package-private members.
119
     *
120
     * @param applyTo new value of the property.
121
     */
122
    public void setApplyToPackage(boolean applyTo) {
123
        applyToPackage = applyTo;
124
    }
125
126
    /**
127
     * Sets whether we should apply the check to private members.
128
     *
129
     * @param applyTo new value of the property.
130
     */
131
    public void setApplyToPrivate(boolean applyTo) {
132
        applyToPrivate = applyTo;
133
    }
134
135
}

Mutations

74

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

86

1.1
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testNotPrivate(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

88

1.1
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testProtectedOnly(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

90

1.1
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testNotPrivate(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

91

1.1
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testDefaults(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

2.2
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testDefaults(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

3.3
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testDefaults(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

93

1.1
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheckTest.testEnumSpecific(com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheckTest)
negated conditional → KILLED

2.2
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheckTest.testEnumSpecific(com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheckTest)
negated conditional → KILLED

3.3
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testDefaults(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

4.4
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testDefaults(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

5.5
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testDefaults(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

6.6
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testDefaults(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

7.7
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testDefaults(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

8.8
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest.testDefaults(com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheckTest)
negated conditional → KILLED

9.9
Location : shouldCheckInScope
Killed by : com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheckTest.testEnumSpecific(com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheckTest)
replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED

Active mutators

Tests examined


Report generated by PIT 1.3.1