OuterTypeFilenameCheck.java

1
////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2017 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;
21
22
import java.io.File;
23
import java.util.regex.Pattern;
24
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
29
/**
30
 * Checks that the outer type name and the file name match.
31
 * @author Oliver Burn
32
 * @author maxvetrenko
33
 */
34
public class OuterTypeFilenameCheck extends AbstractCheck {
35
    /**
36
     * A key is pointing to the warning message text in "messages.properties"
37
     * file.
38
     */
39
    public static final String MSG_KEY = "type.file.mismatch";
40
41
    /** Pattern matching any file extension with dot included. */
42
    private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("\\.[^.]*$");
43
44
    /** Indicates whether the first token has been seen in the file. */
45
    private boolean seenFirstToken;
46
47
    /** Current file name. */
48
    private String fileName;
49
50
    /** If file has public type. */
51
    private boolean hasPublic;
52
53
    /** If first type has has same name as file. */
54
    private boolean validFirst;
55
56
    /** Outer type with mismatched file name. */
57
    private DetailAST wrongType;
58
59
    @Override
60
    public int[] getDefaultTokens() {
61 1 1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/OuterTypeFilenameCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getAcceptableTokens();
62
    }
63
64
    @Override
65
    public int[] getAcceptableTokens() {
66 1 1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/OuterTypeFilenameCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return new int[] {
67
            TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF,
68
            TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF,
69
        };
70
    }
71
72
    @Override
73
    public int[] getRequiredTokens() {
74 1 1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/OuterTypeFilenameCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getAcceptableTokens();
75
    }
76
77
    @Override
78
    public void beginTree(DetailAST rootAST) {
79
        fileName = getFileName();
80
        seenFirstToken = false;
81
        validFirst = false;
82
        hasPublic = false;
83
        wrongType = null;
84
    }
85
86
    @Override
87
    public void visitToken(DetailAST ast) {
88 1 1. visitToken : negated conditional → KILLED
        if (seenFirstToken) {
89
            final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
90 1 1. visitToken : negated conditional → KILLED
            if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
91 1 1. visitToken : negated conditional → KILLED
                    && ast.getParent() == null) {
92
                hasPublic = true;
93
            }
94
        }
95
        else {
96
            final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();
97
98 1 1. visitToken : negated conditional → KILLED
            if (fileName.equals(outerTypeName)) {
99
                validFirst = true;
100
            }
101
            else {
102
                wrongType = ast;
103
            }
104
        }
105
        seenFirstToken = true;
106
    }
107
108
    @Override
109
    public void finishTree(DetailAST rootAST) {
110 3 1. finishTree : negated conditional → KILLED
2. finishTree : negated conditional → KILLED
3. finishTree : negated conditional → KILLED
        if (!validFirst && !hasPublic && wrongType != null) {
111 1 1. finishTree : removed call to com/puppycrawl/tools/checkstyle/checks/OuterTypeFilenameCheck::log → KILLED
            log(wrongType.getLineNo(), MSG_KEY);
112
        }
113
    }
114
115
    /**
116
     * Get source file name.
117
     * @return source file name.
118
     */
119
    private String getFileName() {
120
        String name = getFileContents().getFileName();
121 1 1. getFileName : Replaced integer addition with subtraction → KILLED
        name = name.substring(name.lastIndexOf(File.separatorChar) + 1);
122
        name = FILE_EXTENSION_PATTERN.matcher(name).replaceAll("");
123 1 1. getFileName : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/OuterTypeFilenameCheck::getFileName to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return name;
124
    }
125
}

Mutations

61

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

66

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

74

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

88

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest.testWrongDefault(com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest)
negated conditional → KILLED

90

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest.testPublicClassIsNotFirst(com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest)
negated conditional → KILLED

91

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest.testPublicClassIsNotFirst(com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest)
negated conditional → KILLED

98

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest.testWrongDefault(com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest)
negated conditional → KILLED

110

1.1
Location : finishTree
Killed by : com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest.testWrongDefault(com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest)
negated conditional → KILLED

2.2
Location : finishTree
Killed by : com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest.testPublicClassIsNotFirst(com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest)
negated conditional → KILLED

3.3
Location : finishTree
Killed by : com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest.testWrongDefault(com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest)
negated conditional → KILLED

111

1.1
Location : finishTree
Killed by : com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest.testWrongDefault(com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/OuterTypeFilenameCheck::log → KILLED

121

1.1
Location : getFileName
Killed by : com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest.testFinePublic(com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest)
Replaced integer addition with subtraction → KILLED

123

1.1
Location : getFileName
Killed by : com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest.testPublicClassIsNotFirst(com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheckTest)
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/OuterTypeFilenameCheck::getFileName to ( if (x != null) null else throw new RuntimeException ) → KILLED

Active mutators

Tests examined


Report generated by PIT 1.2.2