1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2025 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 /////////////////////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.checks.coding; 21 22 import java.io.File; 23 24 import com.puppycrawl.tools.checkstyle.FileStatefulCheck; 25 import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 26 import com.puppycrawl.tools.checkstyle.api.DetailAST; 27 import com.puppycrawl.tools.checkstyle.api.FullIdent; 28 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 29 30 /** 31 * <div> 32 * Ensures that a class has a package declaration, and (optionally) whether 33 * the package name matches the directory name for the source file. 34 * </div> 35 * 36 * <p> 37 * Rationale: Classes that live in the null package cannot be imported. 38 * Many novice developers are not aware of this. 39 * </p> 40 * 41 * <p> 42 * Packages provide logical namespace to classes and should be stored in 43 * the form of directory levels to provide physical grouping to your classes. 44 * These directories are added to the classpath so that your classes 45 * are visible to JVM when it runs the code. 46 * </p> 47 * <ul> 48 * <li> 49 * Property {@code matchDirectoryStructure} - Control whether to check for 50 * directory and package name match. 51 * Type is {@code boolean}. 52 * Default value is {@code true}. 53 * </li> 54 * </ul> 55 * 56 * <p> 57 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 58 * </p> 59 * 60 * <p> 61 * Violation Message Keys: 62 * </p> 63 * <ul> 64 * <li> 65 * {@code mismatch.package.directory} 66 * </li> 67 * <li> 68 * {@code missing.package.declaration} 69 * </li> 70 * </ul> 71 * 72 * @since 3.2 73 */ 74 @FileStatefulCheck 75 public final class PackageDeclarationCheck extends AbstractCheck { 76 77 /** 78 * A key is pointing to the warning message text in "messages.properties" 79 * file. 80 */ 81 public static final String MSG_KEY_MISSING = "missing.package.declaration"; 82 83 /** 84 * A key is pointing to the warning message text in "messages.properties" 85 * file. 86 */ 87 public static final String MSG_KEY_MISMATCH = "mismatch.package.directory"; 88 89 /** Is package defined. */ 90 private boolean defined; 91 92 /** Control whether to check for directory and package name match. */ 93 private boolean matchDirectoryStructure = true; 94 95 /** 96 * Setter to control whether to check for directory and package name match. 97 * 98 * @param matchDirectoryStructure the new value. 99 * @since 7.6.1 100 */ 101 public void setMatchDirectoryStructure(boolean matchDirectoryStructure) { 102 this.matchDirectoryStructure = matchDirectoryStructure; 103 } 104 105 @Override 106 public int[] getDefaultTokens() { 107 return getRequiredTokens(); 108 } 109 110 @Override 111 public int[] getRequiredTokens() { 112 return new int[] {TokenTypes.PACKAGE_DEF}; 113 } 114 115 @Override 116 public int[] getAcceptableTokens() { 117 return getRequiredTokens(); 118 } 119 120 @Override 121 public void beginTree(DetailAST ast) { 122 defined = false; 123 } 124 125 @Override 126 public void finishTree(DetailAST ast) { 127 if (!defined && ast != null) { 128 log(ast, MSG_KEY_MISSING); 129 } 130 } 131 132 @Override 133 public void visitToken(DetailAST ast) { 134 defined = true; 135 136 if (matchDirectoryStructure) { 137 final DetailAST packageNameAst = ast.getLastChild().getPreviousSibling(); 138 final FullIdent fullIdent = FullIdent.createFullIdent(packageNameAst); 139 final String packageName = fullIdent.getText().replace('.', File.separatorChar); 140 141 final String directoryName = getDirectoryName(); 142 143 if (!directoryName.endsWith(packageName)) { 144 log(ast, MSG_KEY_MISMATCH, packageName); 145 } 146 } 147 } 148 149 /** 150 * Returns the directory name this file is in. 151 * 152 * @return Directory name. 153 */ 154 private String getDirectoryName() { 155 final String fileName = getFilePath(); 156 final int lastSeparatorPos = fileName.lastIndexOf(File.separatorChar); 157 return fileName.substring(0, lastSeparatorPos); 158 } 159 160 }