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 * <p> 032 * Ensures that a class has a package declaration, and (optionally) whether 033 * the package name matches the directory name for the source file. 034 * </p> 035 * <p> 036 * Rationale: Classes that live in the null package cannot be imported. 037 * Many novice developers are not aware of this. 038 * </p> 039 * <p> 040 * Packages provide logical namespace to classes and should be stored in 041 * the form of directory levels to provide physical grouping to your classes. 042 * These directories are added to the classpath so that your classes 043 * are visible to JVM when it runs the code. 044 * </p> 045 * <ul> 046 * <li> 047 * Property {@code matchDirectoryStructure} - Control whether to check for 048 * directory and package name match. 049 * Default value is {@code true}. 050 * </li> 051 * </ul> 052 * <p> 053 * To configure the check: 054 * </p> 055 * <pre> 056 * <module name="PackageDeclaration"/> 057 * </pre> 058 * <p> 059 * Let us consider the class AnnotationLocationCheck which is in the directory 060 * /com/puppycrawl/tools/checkstyle/checks/annotations/ 061 * </p> 062 * <pre> 063 * package com.puppycrawl.tools.checkstyle.checks; //Violation 064 * public class AnnotationLocationCheck extends AbstractCheck { 065 * //... 066 * } 067 * </pre> 068 * <p> 069 * Example of how the check works when matchDirectoryStructure option is set to false. 070 * Let us again consider the AnnotationLocationCheck class located at directory 071 * /com/puppycrawl/tools/checkstyle/checks/annotations/ along with the following setup, 072 * </p> 073 * <pre> 074 * <module name="PackageDeclaration"> 075 * <property name="matchDirectoryStructure" value="false"/> 076 * </module> 077 * </pre> 078 * <pre> 079 * package com.puppycrawl.tools.checkstyle.checks; //No Violation 080 * 081 * public class AnnotationLocationCheck extends AbstractCheck { 082 * //... 083 * } 084 * </pre> 085 * 086 * @since 3.2 087 */ 088@FileStatefulCheck 089public final class PackageDeclarationCheck extends AbstractCheck { 090 091 /** 092 * A key is pointing to the warning message text in "messages.properties" 093 * file. 094 */ 095 public static final String MSG_KEY_MISSING = "missing.package.declaration"; 096 097 /** 098 * A key is pointing to the warning message text in "messages.properties" 099 * file. 100 */ 101 public static final String MSG_KEY_MISMATCH = "mismatch.package.directory"; 102 103 /** Line number used to log violation when no AST nodes are present in file. */ 104 private static final int DEFAULT_LINE_NUMBER = 1; 105 106 /** Is package defined. */ 107 private boolean defined; 108 109 /** Control whether to check for directory and package name match. */ 110 private boolean matchDirectoryStructure = true; 111 112 /** 113 * Setter to control whether to check for directory and package name match. 114 * @param matchDirectoryStructure the new value. 115 */ 116 public void setMatchDirectoryStructure(boolean matchDirectoryStructure) { 117 this.matchDirectoryStructure = matchDirectoryStructure; 118 } 119 120 @Override 121 public int[] getDefaultTokens() { 122 return getRequiredTokens(); 123 } 124 125 @Override 126 public int[] getRequiredTokens() { 127 return new int[] {TokenTypes.PACKAGE_DEF}; 128 } 129 130 @Override 131 public int[] getAcceptableTokens() { 132 return getRequiredTokens(); 133 } 134 135 @Override 136 public void beginTree(DetailAST ast) { 137 defined = false; 138 } 139 140 @Override 141 public void finishTree(DetailAST ast) { 142 if (!defined) { 143 int lineNumber = DEFAULT_LINE_NUMBER; 144 if (ast != null) { 145 lineNumber = ast.getLineNo(); 146 } 147 log(lineNumber, MSG_KEY_MISSING); 148 } 149 } 150 151 @Override 152 public void visitToken(DetailAST ast) { 153 defined = true; 154 155 if (matchDirectoryStructure) { 156 final DetailAST packageNameAst = ast.getLastChild().getPreviousSibling(); 157 final FullIdent fullIdent = FullIdent.createFullIdent(packageNameAst); 158 final String packageName = fullIdent.getText().replace('.', File.separatorChar); 159 160 final String directoryName = getDirectoryName(); 161 162 if (!directoryName.endsWith(packageName)) { 163 log(fullIdent.getLineNo(), MSG_KEY_MISMATCH, packageName); 164 } 165 } 166 } 167 168 /** 169 * Returns the directory name this file is in. 170 * @return Directory name. 171 */ 172 private String getDirectoryName() { 173 final String fileName = getFileContents().getFileName(); 174 final int lastSeparatorPos = fileName.lastIndexOf(File.separatorChar); 175 return fileName.substring(0, lastSeparatorPos); 176 } 177 178}