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 * @author Oliver Burn
033 * @author maxvetrenko
034 */
035@FileStatefulCheck
036public class OuterTypeFilenameCheck extends AbstractCheck {
037
038    /**
039     * A key is pointing to the warning message text in "messages.properties"
040     * file.
041     */
042    public static final String MSG_KEY = "type.file.mismatch";
043
044    /** Pattern matching any file extension with dot included. */
045    private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("\\.[^.]*$");
046
047    /** Indicates whether the first token has been seen in the file. */
048    private boolean seenFirstToken;
049
050    /** Current file name. */
051    private String fileName;
052
053    /** If file has public type. */
054    private boolean hasPublic;
055
056    /** If first type has has same name as file. */
057    private boolean validFirst;
058
059    /** Outer type with mismatched file name. */
060    private DetailAST wrongType;
061
062    @Override
063    public int[] getDefaultTokens() {
064        return getRequiredTokens();
065    }
066
067    @Override
068    public int[] getAcceptableTokens() {
069        return getRequiredTokens();
070    }
071
072    @Override
073    public int[] getRequiredTokens() {
074        return new int[] {
075            TokenTypes.CLASS_DEF,
076            TokenTypes.INTERFACE_DEF,
077            TokenTypes.ENUM_DEF,
078            TokenTypes.ANNOTATION_DEF,
079        };
080    }
081
082    @Override
083    public void beginTree(DetailAST rootAST) {
084        fileName = getFileName();
085        seenFirstToken = false;
086        validFirst = false;
087        hasPublic = false;
088        wrongType = null;
089    }
090
091    @Override
092    public void visitToken(DetailAST ast) {
093        if (seenFirstToken) {
094            final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
095            if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
096                    && ast.getParent() == null) {
097                hasPublic = true;
098            }
099        }
100        else {
101            final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();
102
103            if (fileName.equals(outerTypeName)) {
104                validFirst = true;
105            }
106            else {
107                wrongType = ast;
108            }
109        }
110        seenFirstToken = true;
111    }
112
113    @Override
114    public void finishTree(DetailAST rootAST) {
115        if (!validFirst && !hasPublic && wrongType != null) {
116            log(wrongType.getLineNo(), MSG_KEY);
117        }
118    }
119
120    /**
121     * Get source file name.
122     * @return source file name.
123     */
124    private String getFileName() {
125        String name = getFileContents().getFileName();
126        name = name.substring(name.lastIndexOf(File.separatorChar) + 1);
127        name = FILE_EXTENSION_PATTERN.matcher(name).replaceAll("");
128        return name;
129    }
130
131}