001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 /** Is package defined. */ 104 private boolean defined; 105 106 /** Control whether to check for directory and package name match. */ 107 private boolean matchDirectoryStructure = true; 108 109 /** 110 * Setter to control whether to check for directory and package name match. 111 * 112 * @param matchDirectoryStructure the new value. 113 */ 114 public void setMatchDirectoryStructure(boolean matchDirectoryStructure) { 115 this.matchDirectoryStructure = matchDirectoryStructure; 116 } 117 118 @Override 119 public int[] getDefaultTokens() { 120 return getRequiredTokens(); 121 } 122 123 @Override 124 public int[] getRequiredTokens() { 125 return new int[] {TokenTypes.PACKAGE_DEF}; 126 } 127 128 @Override 129 public int[] getAcceptableTokens() { 130 return getRequiredTokens(); 131 } 132 133 @Override 134 public void beginTree(DetailAST ast) { 135 defined = false; 136 } 137 138 @Override 139 public void finishTree(DetailAST ast) { 140 if (!defined && ast != null) { 141 log(ast, MSG_KEY_MISSING); 142 } 143 } 144 145 @Override 146 public void visitToken(DetailAST ast) { 147 defined = true; 148 149 if (matchDirectoryStructure) { 150 final DetailAST packageNameAst = ast.getLastChild().getPreviousSibling(); 151 final FullIdent fullIdent = FullIdent.createFullIdent(packageNameAst); 152 final String packageName = fullIdent.getText().replace('.', File.separatorChar); 153 154 final String directoryName = getDirectoryName(); 155 156 if (!directoryName.endsWith(packageName)) { 157 log(ast, MSG_KEY_MISMATCH, packageName); 158 } 159 } 160 } 161 162 /** 163 * Returns the directory name this file is in. 164 * 165 * @return Directory name. 166 */ 167 private String getDirectoryName() { 168 final String fileName = getFileContents().getFileName(); 169 final int lastSeparatorPos = fileName.lastIndexOf(File.separatorChar); 170 return fileName.substring(0, lastSeparatorPos); 171 } 172 173}