001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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; 029import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 030 031/** 032 * <div> 033 * Checks that the outer type name and the file name match. 034 * For example, the class {@code Foo} must be in a file named {@code Foo.java}. 035 * </div> 036 * 037 * @since 5.3 038 */ 039@FileStatefulCheck 040public class OuterTypeFilenameCheck extends AbstractCheck { 041 042 /** 043 * A key is pointing to the warning message text in "messages.properties" file. 044 */ 045 public static final String MSG_KEY = "type.file.mismatch"; 046 047 /** Pattern matching any file extension with dot included. */ 048 private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("\\.[^.]*$"); 049 050 /** Indicates whether the first token has been seen in the file. */ 051 private boolean seenFirstToken; 052 053 /** Current file name. */ 054 private String fileName; 055 056 /** If file has public type. */ 057 private boolean hasPublic; 058 059 /** Outer type with mismatched file name. */ 060 private DetailAST wrongType; 061 062 /** 063 * Indicates whether the file is a compact source file. Its outer type is an 064 * implicit unnamed class that is not declared with a name in the source, so 065 * there is no type name to compare against the file name. 066 */ 067 private boolean isCompactSourceFile; 068 069 /** 070 * Creates a new {@code OuterTypeFilenameCheck} instance. 071 */ 072 public OuterTypeFilenameCheck() { 073 // no code by default 074 } 075 076 @Override 077 public int[] getDefaultTokens() { 078 return getRequiredTokens(); 079 } 080 081 @Override 082 public int[] getAcceptableTokens() { 083 return getRequiredTokens(); 084 } 085 086 @Override 087 public int[] getRequiredTokens() { 088 return new int[] { 089 TokenTypes.CLASS_DEF, 090 TokenTypes.INTERFACE_DEF, 091 TokenTypes.ENUM_DEF, 092 TokenTypes.ANNOTATION_DEF, 093 TokenTypes.RECORD_DEF, 094 }; 095 } 096 097 @Override 098 public void beginTree(DetailAST rootAST) { 099 fileName = getSourceFileName(); 100 seenFirstToken = false; 101 hasPublic = false; 102 wrongType = null; 103 isCompactSourceFile = rootAST != null 104 && rootAST.getType() == TokenTypes.COMPACT_COMPILATION_UNIT; 105 } 106 107 @Override 108 public void visitToken(DetailAST ast) { 109 if (!isCompactSourceFile) { 110 if (seenFirstToken) { 111 final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS); 112 if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null 113 && TokenUtil.isRootNode(ast.getParent())) { 114 hasPublic = true; 115 } 116 } 117 else { 118 final String outerTypeName = TokenUtil.getIdent(ast).getText(); 119 120 if (!fileName.equals(outerTypeName)) { 121 wrongType = ast; 122 } 123 } 124 seenFirstToken = true; 125 } 126 } 127 128 @Override 129 public void finishTree(DetailAST rootAST) { 130 if (!hasPublic && wrongType != null) { 131 log(wrongType, MSG_KEY); 132 } 133 } 134 135 /** 136 * Get source file name. 137 * 138 * @return source file name. 139 */ 140 private String getSourceFileName() { 141 String name = getFilePath(); 142 name = name.substring(name.lastIndexOf(File.separatorChar) + 1); 143 return FILE_EXTENSION_PATTERN.matcher(name).replaceAll(""); 144 } 145 146}