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" 044 * file. 045 */ 046 public static final String MSG_KEY = "type.file.mismatch"; 047 048 /** Pattern matching any file extension with dot included. */ 049 private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("\\.[^.]*$"); 050 051 /** Indicates whether the first token has been seen in the file. */ 052 private boolean seenFirstToken; 053 054 /** Current file name. */ 055 private String fileName; 056 057 /** If file has public type. */ 058 private boolean hasPublic; 059 060 /** Outer type with mismatched file name. */ 061 private DetailAST wrongType; 062 063 /** 064 * Indicates whether the file is a compact source file. Its outer type is an 065 * implicit unnamed class that is not declared with a name in the source, so 066 * there is no type name to compare against the file name. 067 */ 068 private boolean isCompactSourceFile; 069 070 /** 071 * Creates a new {@code OuterTypeFilenameCheck} instance. 072 */ 073 public OuterTypeFilenameCheck() { 074 // no code by default 075 } 076 077 @Override 078 public int[] getDefaultTokens() { 079 return getRequiredTokens(); 080 } 081 082 @Override 083 public int[] getAcceptableTokens() { 084 return getRequiredTokens(); 085 } 086 087 @Override 088 public int[] getRequiredTokens() { 089 return new int[] { 090 TokenTypes.CLASS_DEF, 091 TokenTypes.INTERFACE_DEF, 092 TokenTypes.ENUM_DEF, 093 TokenTypes.ANNOTATION_DEF, 094 TokenTypes.RECORD_DEF, 095 }; 096 } 097 098 @Override 099 public void beginTree(DetailAST rootAST) { 100 fileName = getSourceFileName(); 101 seenFirstToken = false; 102 hasPublic = false; 103 wrongType = null; 104 isCompactSourceFile = rootAST != null 105 && rootAST.getType() == TokenTypes.COMPACT_COMPILATION_UNIT; 106 } 107 108 @Override 109 public void visitToken(DetailAST ast) { 110 if (!isCompactSourceFile) { 111 if (seenFirstToken) { 112 final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS); 113 if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null 114 && TokenUtil.isRootNode(ast.getParent())) { 115 hasPublic = true; 116 } 117 } 118 else { 119 final String outerTypeName = TokenUtil.getIdent(ast).getText(); 120 121 if (!fileName.equals(outerTypeName)) { 122 wrongType = ast; 123 } 124 } 125 seenFirstToken = true; 126 } 127 } 128 129 @Override 130 public void finishTree(DetailAST rootAST) { 131 if (!hasPublic && wrongType != null) { 132 log(wrongType, MSG_KEY); 133 } 134 } 135 136 /** 137 * Get source file name. 138 * 139 * @return source file name. 140 */ 141 private String getSourceFileName() { 142 String name = getFilePath(); 143 name = name.substring(name.lastIndexOf(File.separatorChar) + 1); 144 return FILE_EXTENSION_PATTERN.matcher(name).replaceAll(""); 145 } 146 147}