OverloadMethodsDeclarationOrderCheck.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 java.util.HashMap;
23
import java.util.Map;
24
25
import com.puppycrawl.tools.checkstyle.StatelessCheck;
26
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
27
import com.puppycrawl.tools.checkstyle.api.DetailAST;
28
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29
30
/**
31
 * <p>
32
 * Checks that overload methods are grouped together. Example:
33
 * </p>
34
 * <pre>
35
 * {@code
36
 * public void foo(int i) {}
37
 * public void foo(String s) {}
38
 * public void notFoo() {} // Have to be after foo(int i, String s)
39
 * public void foo(int i, String s) {}
40
 * }
41
 * </pre>
42
 * <p>
43
 * An example of how to configure the check is:
44
 * </p>
45
 *
46
 * <pre>
47
 * &lt;module name="OverloadMethodsDeclarationOrder"/&gt;
48
 * </pre>
49
 * @author maxvetrenko
50
 */
51
@StatelessCheck
52
public class OverloadMethodsDeclarationOrderCheck extends AbstractCheck {
53
54
    /**
55
     * A key is pointing to the warning message text in "messages.properties"
56
     * file.
57
     */
58
    public static final String MSG_KEY = "overload.methods.declaration";
59
60
    @Override
61
    public int[] getDefaultTokens() {
62 1 1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/OverloadMethodsDeclarationOrderCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getRequiredTokens();
63
    }
64
65
    @Override
66
    public int[] getAcceptableTokens() {
67 1 1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/OverloadMethodsDeclarationOrderCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getRequiredTokens();
68
    }
69
70
    @Override
71
    public int[] getRequiredTokens() {
72 1 1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/OverloadMethodsDeclarationOrderCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return new int[] {
73
            TokenTypes.OBJBLOCK,
74
        };
75
    }
76
77
    @Override
78
    public void visitToken(DetailAST ast) {
79
        final int parentType = ast.getParent().getType();
80 4 1. visitToken : negated conditional → KILLED
2. visitToken : negated conditional → KILLED
3. visitToken : negated conditional → KILLED
4. visitToken : negated conditional → KILLED
        if (parentType == TokenTypes.CLASS_DEF
81
                || parentType == TokenTypes.ENUM_DEF
82
                || parentType == TokenTypes.INTERFACE_DEF
83
                || parentType == TokenTypes.LITERAL_NEW) {
84 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/coding/OverloadMethodsDeclarationOrderCheck::checkOverloadMethodsGrouping → KILLED
            checkOverloadMethodsGrouping(ast);
85
        }
86
    }
87
88
    /**
89
     * Checks that if overload methods are grouped together they should not be
90
     * separated from each other.
91
     * @param objectBlock
92
     *        is a class, interface or enum object block.
93
     */
94
    private void checkOverloadMethodsGrouping(DetailAST objectBlock) {
95
        final int allowedDistance = 1;
96
        DetailAST currentToken = objectBlock.getFirstChild();
97
        final Map<String, Integer> methodIndexMap = new HashMap<>();
98
        final Map<String, Integer> methodLineNumberMap = new HashMap<>();
99
        int currentIndex = 0;
100 1 1. checkOverloadMethodsGrouping : negated conditional → KILLED
        while (currentToken != null) {
101 1 1. checkOverloadMethodsGrouping : negated conditional → KILLED
            if (currentToken.getType() == TokenTypes.METHOD_DEF) {
102 1 1. checkOverloadMethodsGrouping : Changed increment from 1 to -1 → KILLED
                currentIndex++;
103
                final String methodName =
104
                        currentToken.findFirstToken(TokenTypes.IDENT).getText();
105 1 1. checkOverloadMethodsGrouping : negated conditional → KILLED
                if (methodIndexMap.containsKey(methodName)) {
106
                    final int previousIndex = methodIndexMap.get(methodName);
107 3 1. checkOverloadMethodsGrouping : changed conditional boundary → KILLED
2. checkOverloadMethodsGrouping : Replaced integer subtraction with addition → KILLED
3. checkOverloadMethodsGrouping : negated conditional → KILLED
                    if (currentIndex - previousIndex > allowedDistance) {
108
                        final int previousLineWithOverloadMethod =
109
                                methodLineNumberMap.get(methodName);
110 1 1. checkOverloadMethodsGrouping : removed call to com/puppycrawl/tools/checkstyle/checks/coding/OverloadMethodsDeclarationOrderCheck::log → KILLED
                        log(currentToken.getLineNo(), MSG_KEY,
111
                                previousLineWithOverloadMethod);
112
                    }
113
                }
114
                methodIndexMap.put(methodName, currentIndex);
115
                methodLineNumberMap.put(methodName, currentToken.getLineNo());
116
            }
117
            currentToken = currentToken.getNextSibling();
118
        }
119
    }
120
121
}

Mutations

62

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

67

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

72

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

80

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

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
negated conditional → KILLED

3.3
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
negated conditional → KILLED

4.4
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
negated conditional → KILLED

84

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/coding/OverloadMethodsDeclarationOrderCheck::checkOverloadMethodsGrouping → KILLED

100

1.1
Location : checkOverloadMethodsGrouping
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
negated conditional → KILLED

101

1.1
Location : checkOverloadMethodsGrouping
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
negated conditional → KILLED

102

1.1
Location : checkOverloadMethodsGrouping
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
Changed increment from 1 to -1 → KILLED

105

1.1
Location : checkOverloadMethodsGrouping
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
negated conditional → KILLED

107

1.1
Location : checkOverloadMethodsGrouping
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
changed conditional boundary → KILLED

2.2
Location : checkOverloadMethodsGrouping
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
Replaced integer subtraction with addition → KILLED

3.3
Location : checkOverloadMethodsGrouping
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
negated conditional → KILLED

110

1.1
Location : checkOverloadMethodsGrouping
Killed by : com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest.testDefault(com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/coding/OverloadMethodsDeclarationOrderCheck::log → KILLED

Active mutators

Tests examined


Report generated by PIT 1.3.1