001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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.coding; 021 022import java.io.File; 023 024import com.puppycrawl.tools.checkstyle.FileStatefulCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.FullIdent; 028import com.puppycrawl.tools.checkstyle.api.TokenTypes; 029 030/** 031 * Ensures there is a package declaration. 032 * Optionally checks if directory structure matches package name. 033 * Rationale: Classes that live in the null package cannot be 034 * imported. Many novice developers are not aware of this. 035 * Packages provide logical namespace to classes and should be stored in 036 * the form of directory levels to provide physical grouping to your classes. 037 * These directories are added to the classpath so that your classes 038 * are visible to JVM when it runs the code. 039 * 040 */ 041@FileStatefulCheck 042public final class PackageDeclarationCheck extends AbstractCheck { 043 044 /** 045 * A key is pointing to the warning message text in "messages.properties" 046 * file. 047 */ 048 public static final String MSG_KEY_MISSING = "missing.package.declaration"; 049 050 /** 051 * A key is pointing to the warning message text in "messages.properties" 052 * file. 053 */ 054 public static final String MSG_KEY_MISMATCH = "mismatch.package.directory"; 055 056 /** Line number used to log violation when no AST nodes are present in file. */ 057 private static final int DEFAULT_LINE_NUMBER = 1; 058 059 /** Is package defined. */ 060 private boolean defined; 061 062 /** Whether to check for directory and package name match. */ 063 private boolean matchDirectoryStructure = true; 064 065 /** 066 * Set whether to check for directory and package name match. 067 * @param matchDirectoryStructure the new value. 068 */ 069 public void setMatchDirectoryStructure(boolean matchDirectoryStructure) { 070 this.matchDirectoryStructure = matchDirectoryStructure; 071 } 072 073 @Override 074 public int[] getDefaultTokens() { 075 return getRequiredTokens(); 076 } 077 078 @Override 079 public int[] getRequiredTokens() { 080 return new int[] {TokenTypes.PACKAGE_DEF}; 081 } 082 083 @Override 084 public int[] getAcceptableTokens() { 085 return getRequiredTokens(); 086 } 087 088 @Override 089 public void beginTree(DetailAST ast) { 090 defined = false; 091 } 092 093 @Override 094 public void finishTree(DetailAST ast) { 095 if (!defined) { 096 int lineNumber = DEFAULT_LINE_NUMBER; 097 if (ast != null) { 098 lineNumber = ast.getLineNo(); 099 } 100 log(lineNumber, MSG_KEY_MISSING); 101 } 102 } 103 104 @Override 105 public void visitToken(DetailAST ast) { 106 defined = true; 107 108 if (matchDirectoryStructure) { 109 final DetailAST packageNameAst = ast.getLastChild().getPreviousSibling(); 110 final FullIdent fullIdent = FullIdent.createFullIdent(packageNameAst); 111 final String packageName = fullIdent.getText().replace('.', File.separatorChar); 112 113 final String directoryName = getDirectoryName(); 114 115 if (!directoryName.endsWith(packageName)) { 116 log(fullIdent.getLineNo(), MSG_KEY_MISMATCH, packageName); 117 } 118 } 119 } 120 121 /** 122 * Returns the directory name this file is in. 123 * @return Directory name. 124 */ 125 private String getDirectoryName() { 126 final String fileName = getFileContents().getFileName(); 127 final int lastSeparatorPos = fileName.lastIndexOf(File.separatorChar); 128 return fileName.substring(0, lastSeparatorPos); 129 } 130 131}