OuterTypeNumberCheck.java

1
///////////////////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3
// Copyright (C) 2001-2022 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.sizes;
21
22
import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
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 for the number of types declared at the <i>outer</i> (or <i>root</i>) level in a file.
30
 * </p>
31
 * <p>
32
 * Rationale: It is considered good practice to only define one outer type per file.
33
 * </p>
34
 * <ul>
35
 * <li>
36
 * Property {@code max} - Specify the maximum number of outer types allowed.
37
 * Type is {@code int}.
38
 * Default value is {@code 1}.
39
 * </li>
40
 * </ul>
41
 * <p>
42
 * To configure the check to accept 1 outer type per file:
43
 * </p>
44
 * <pre>
45
 * &lt;module name="OuterTypeNumber"/&gt;
46
 * </pre>
47
 * <p>
48
 * To configure the check to accept 2 outer types per file:
49
 * </p>
50
 * <pre>
51
 * &lt;module name="OuterTypeNumber"&gt;
52
 *   &lt;property name="max" value="2"/&gt;
53
 * &lt;/module&gt;
54
 * </pre>
55
 * <p>
56
 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
57
 * </p>
58
 * <p>
59
 * Violation Message Keys:
60
 * </p>
61
 * <ul>
62
 * <li>
63
 * {@code maxOuterTypes}
64
 * </li>
65
 * </ul>
66
 *
67
 * @since 5.0
68
 */
69
@FileStatefulCheck
70
public class OuterTypeNumberCheck extends AbstractCheck {
71
72
    /**
73
     * A key is pointing to the warning message text in "messages.properties"
74
     * file.
75
     */
76
    public static final String MSG_KEY = "maxOuterTypes";
77
78
    /** Specify the maximum number of outer types allowed. */
79
    private int max = 1;
80
    /** Tracks the current depth in types. */
81
    private int currentDepth;
82
    /** Tracks the number of outer types found. */
83
    private int outerNum;
84
85
    @Override
86
    public int[] getDefaultTokens() {
87 1 1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/OuterTypeNumberCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getRequiredTokens();
88
    }
89
90
    @Override
91
    public int[] getAcceptableTokens() {
92 1 1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/OuterTypeNumberCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getRequiredTokens();
93
    }
94
95
    @Override
96
    public int[] getRequiredTokens() {
97 1 1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/OuterTypeNumberCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return new int[] {
98
            TokenTypes.CLASS_DEF,
99
            TokenTypes.INTERFACE_DEF,
100
            TokenTypes.ENUM_DEF,
101
            TokenTypes.ANNOTATION_DEF,
102
            TokenTypes.RECORD_DEF,
103
        };
104
    }
105
106
    @Override
107
    public void beginTree(DetailAST ast) {
108
        currentDepth = 0;
109
        outerNum = 0;
110
    }
111
112
    @Override
113
    public void finishTree(DetailAST ast) {
114 4 1. finishTree : changed conditional boundary → KILLED
2. finishTree : negated conditional → KILLED
3. finishTree : removed conditional - replaced comparison check with false → KILLED
4. finishTree : removed conditional - replaced comparison check with true → KILLED
        if (max < outerNum) {
115 1 1. finishTree : removed call to com/puppycrawl/tools/checkstyle/checks/sizes/OuterTypeNumberCheck::log → KILLED
            log(ast, MSG_KEY, outerNum, max);
116
        }
117
    }
118
119
    @Override
120
    public void visitToken(DetailAST ast) {
121 3 1. visitToken : negated conditional → KILLED
2. visitToken : removed conditional - replaced equality check with false → KILLED
3. visitToken : removed conditional - replaced equality check with true → KILLED
        if (currentDepth == 0) {
122 1 1. visitToken : Replaced integer addition with subtraction → KILLED
            outerNum++;
123
        }
124 1 1. visitToken : Replaced integer addition with subtraction → KILLED
        currentDepth++;
125
    }
126
127
    @Override
128
    public void leaveToken(DetailAST ast) {
129 1 1. leaveToken : Replaced integer subtraction with addition → KILLED
        currentDepth--;
130
    }
131
132
    /**
133
     * Setter to specify the maximum number of outer types allowed.
134
     *
135
     * @param max the new number.
136
     */
137
    public void setMax(int max) {
138
        this.max = max;
139
    }
140
141
}

Mutations

87

1.1
Location : getDefaultTokens
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithInnerClass()]
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/OuterTypeNumberCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

92

1.1
Location : getAcceptableTokens
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testGetAcceptableTokens()]
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/OuterTypeNumberCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

97

1.1
Location : getRequiredTokens
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testGetRequiredTokens()]
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/sizes/OuterTypeNumberCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

114

1.1
Location : finishTree
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithInnerClass()]
changed conditional boundary → KILLED

2.2
Location : finishTree
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithInnerClass()]
negated conditional → KILLED

3.3
Location : finishTree
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithRecords()]
removed conditional - replaced comparison check with false → KILLED

4.4
Location : finishTree
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithInnerClass()]
removed conditional - replaced comparison check with true → KILLED

115

1.1
Location : finishTree
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithRecords()]
removed call to com/puppycrawl/tools/checkstyle/checks/sizes/OuterTypeNumberCheck::log → KILLED

121

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithRecords()]
negated conditional → KILLED

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithRecords()]
removed conditional - replaced equality check with false → KILLED

3.3
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithInnerClass()]
removed conditional - replaced equality check with true → KILLED

122

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithRecords()]
Replaced integer addition with subtraction → KILLED

124

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithRecords()]
Replaced integer addition with subtraction → KILLED

129

1.1
Location : leaveToken
Killed by : com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheckTest]/[method:testWithRecords()]
Replaced integer subtraction with addition → KILLED

Active mutators

Tests examined


Report generated by PIT 1.8.0