001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028
029/**
030 * Checks that the outer type name and the file name match.
031 * @author Oliver Burn
032 * @author maxvetrenko
033 */
034public class OuterTypeFilenameCheck extends AbstractCheck {
035    /**
036     * A key is pointing to the warning message text in "messages.properties"
037     * file.
038     */
039    public static final String MSG_KEY = "type.file.mismatch";
040
041    /** Pattern matching any file extension with dot included. */
042    private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("\\.[^.]*$");
043
044    /** Indicates whether the first token has been seen in the file. */
045    private boolean seenFirstToken;
046
047    /** Current file name. */
048    private String fileName;
049
050    /** If file has public type. */
051    private boolean hasPublic;
052
053    /** If first type has has same name as file. */
054    private boolean validFirst;
055
056    /** Outer type with mismatched file name. */
057    private DetailAST wrongType;
058
059    @Override
060    public int[] getDefaultTokens() {
061        return getAcceptableTokens();
062    }
063
064    @Override
065    public int[] getAcceptableTokens() {
066        return new int[] {
067            TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF,
068            TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF,
069        };
070    }
071
072    @Override
073    public int[] getRequiredTokens() {
074        return getAcceptableTokens();
075    }
076
077    @Override
078    public void beginTree(DetailAST rootAST) {
079        fileName = getFileName();
080        seenFirstToken = false;
081        validFirst = false;
082        hasPublic = false;
083        wrongType = null;
084    }
085
086    @Override
087    public void visitToken(DetailAST ast) {
088        if (seenFirstToken) {
089            final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
090            if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
091                    && ast.getParent() == null) {
092                hasPublic = true;
093            }
094        }
095        else {
096            final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();
097
098            if (fileName.equals(outerTypeName)) {
099                validFirst = true;
100            }
101            else {
102                wrongType = ast;
103            }
104        }
105        seenFirstToken = true;
106    }
107
108    @Override
109    public void finishTree(DetailAST rootAST) {
110        if (!validFirst && !hasPublic && wrongType != null) {
111            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        name = name.substring(name.lastIndexOf(File.separatorChar) + 1);
122        name = FILE_EXTENSION_PATTERN.matcher(name).replaceAll("");
123        return name;
124    }
125}