001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks;
021
022import java.io.File;
023import java.util.regex.Pattern;
024
025import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029
030/**
031 * Checks that the outer type name and the file name match.
032 */
033@FileStatefulCheck
034public class OuterTypeFilenameCheck extends AbstractCheck {
035
036    /**
037     * A key is pointing to the warning message text in "messages.properties"
038     * file.
039     */
040    public static final String MSG_KEY = "type.file.mismatch";
041
042    /** Pattern matching any file extension with dot included. */
043    private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("\\.[^.]*$");
044
045    /** Indicates whether the first token has been seen in the file. */
046    private boolean seenFirstToken;
047
048    /** Current file name. */
049    private String fileName;
050
051    /** If file has public type. */
052    private boolean hasPublic;
053
054    /** Outer type with mismatched file name. */
055    private DetailAST wrongType;
056
057    @Override
058    public int[] getDefaultTokens() {
059        return getRequiredTokens();
060    }
061
062    @Override
063    public int[] getAcceptableTokens() {
064        return getRequiredTokens();
065    }
066
067    @Override
068    public int[] getRequiredTokens() {
069        return new int[] {
070            TokenTypes.CLASS_DEF,
071            TokenTypes.INTERFACE_DEF,
072            TokenTypes.ENUM_DEF,
073            TokenTypes.ANNOTATION_DEF,
074        };
075    }
076
077    @Override
078    public void beginTree(DetailAST rootAST) {
079        fileName = getFileName();
080        seenFirstToken = false;
081        hasPublic = false;
082        wrongType = null;
083    }
084
085    @Override
086    public void visitToken(DetailAST ast) {
087        if (seenFirstToken) {
088            final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
089            if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
090                    && ast.getParent() == null) {
091                hasPublic = true;
092            }
093        }
094        else {
095            final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();
096
097            if (!fileName.equals(outerTypeName)) {
098                wrongType = ast;
099            }
100        }
101        seenFirstToken = true;
102    }
103
104    @Override
105    public void finishTree(DetailAST rootAST) {
106        if (!hasPublic && wrongType != null) {
107            log(wrongType.getLineNo(), MSG_KEY);
108        }
109    }
110
111    /**
112     * Get source file name.
113     * @return source file name.
114     */
115    private String getFileName() {
116        String name = getFileContents().getFileName();
117        name = name.substring(name.lastIndexOf(File.separatorChar) + 1);
118        name = FILE_EXTENSION_PATTERN.matcher(name).replaceAll("");
119        return name;
120    }
121
122}