001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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    /** If first type has has same name as file. */
055    private boolean validFirst;
056
057    /** Outer type with mismatched file name. */
058    private DetailAST wrongType;
059
060    @Override
061    public int[] getDefaultTokens() {
062        return getRequiredTokens();
063    }
064
065    @Override
066    public int[] getAcceptableTokens() {
067        return getRequiredTokens();
068    }
069
070    @Override
071    public int[] getRequiredTokens() {
072        return new int[] {
073            TokenTypes.CLASS_DEF,
074            TokenTypes.INTERFACE_DEF,
075            TokenTypes.ENUM_DEF,
076            TokenTypes.ANNOTATION_DEF,
077        };
078    }
079
080    @Override
081    public void beginTree(DetailAST rootAST) {
082        fileName = getFileName();
083        seenFirstToken = false;
084        validFirst = false;
085        hasPublic = false;
086        wrongType = null;
087    }
088
089    @Override
090    public void visitToken(DetailAST ast) {
091        if (seenFirstToken) {
092            final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
093            if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
094                    && ast.getParent() == null) {
095                hasPublic = true;
096            }
097        }
098        else {
099            final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();
100
101            if (fileName.equals(outerTypeName)) {
102                validFirst = true;
103            }
104            else {
105                wrongType = ast;
106            }
107        }
108        seenFirstToken = true;
109    }
110
111    @Override
112    public void finishTree(DetailAST rootAST) {
113        if (!validFirst && !hasPublic && wrongType != null) {
114            log(wrongType.getLineNo(), MSG_KEY);
115        }
116    }
117
118    /**
119     * Get source file name.
120     * @return source file name.
121     */
122    private String getFileName() {
123        String name = getFileContents().getFileName();
124        name = name.substring(name.lastIndexOf(File.separatorChar) + 1);
125        name = FILE_EXTENSION_PATTERN.matcher(name).replaceAll("");
126        return name;
127    }
128
129}