001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 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.Check; 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 Check { 035 /** Pattern matching any file extension with dot included. */ 036 private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("\\.[^\\.]*$"); 037 038 /** Indicates whether the first token has been seen in the file. */ 039 private boolean seenFirstToken; 040 041 /** Current file name. */ 042 private String fileName; 043 044 /** If file has public type. */ 045 private boolean hasPublic; 046 047 /** If first type has has same name as file. */ 048 private boolean validFirst; 049 050 /** Outer type with mismatched file name. */ 051 private DetailAST wrongType; 052 053 @Override 054 public int[] getDefaultTokens() { 055 return getAcceptableTokens(); 056 } 057 058 @Override 059 public int[] getAcceptableTokens() { 060 return new int[] { 061 TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, 062 TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF, 063 }; 064 } 065 066 @Override 067 public int[] getRequiredTokens() { 068 return getAcceptableTokens(); 069 } 070 071 @Override 072 public void beginTree(DetailAST rootAST) { 073 fileName = getFileName(); 074 seenFirstToken = false; 075 validFirst = false; 076 hasPublic = false; 077 wrongType = null; 078 } 079 080 @Override 081 public void visitToken(DetailAST ast) { 082 final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText(); 083 if (seenFirstToken) { 084 final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS); 085 if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null 086 && ast.getParent() == null) { 087 hasPublic = true; 088 } 089 } 090 else { 091 092 if (fileName.equals(outerTypeName)) { 093 validFirst = true; 094 } 095 else { 096 wrongType = ast; 097 } 098 } 099 seenFirstToken = true; 100 } 101 102 @Override 103 public void finishTree(DetailAST rootAST) { 104 if (!validFirst && !hasPublic && wrongType != null) { 105 log(wrongType.getLineNo(), "type.file.mismatch"); 106 } 107 } 108 109 /** 110 * Get source file name. 111 * @return source file name. 112 */ 113 private String getFileName() { 114 String name = getFileContents().getFileName(); 115 name = name.substring(name.lastIndexOf(File.separatorChar) + 1); 116 name = FILE_EXTENSION_PATTERN.matcher(name).replaceAll(""); 117 return name; 118 } 119}