001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> 041 * @author Oliver Burn 042 * @author Vikramaditya Kukreja 043 */ 044@FileStatefulCheck 045public final class PackageDeclarationCheck extends AbstractCheck { 046 047 /** 048 * A key is pointing to the warning message text in "messages.properties" 049 * file. 050 */ 051 public static final String MSG_KEY_MISSING = "missing.package.declaration"; 052 053 /** 054 * A key is pointing to the warning message text in "messages.properties" 055 * file. 056 */ 057 public static final String MSG_KEY_MISMATCH = "mismatch.package.directory"; 058 059 /** Line number used to log violation when no AST nodes are present in file. */ 060 private static final int DEFAULT_LINE_NUMBER = 1; 061 062 /** Is package defined. */ 063 private boolean defined; 064 065 /** Whether to check for directory and package name match. */ 066 private boolean matchDirectoryStructure = true; 067 068 /** 069 * Set whether to check for directory and package name match. 070 * @param matchDirectoryStructure the new value. 071 */ 072 public void setMatchDirectoryStructure(boolean matchDirectoryStructure) { 073 this.matchDirectoryStructure = matchDirectoryStructure; 074 } 075 076 @Override 077 public int[] getDefaultTokens() { 078 return getRequiredTokens(); 079 } 080 081 @Override 082 public int[] getRequiredTokens() { 083 return new int[] {TokenTypes.PACKAGE_DEF}; 084 } 085 086 @Override 087 public int[] getAcceptableTokens() { 088 return getRequiredTokens(); 089 } 090 091 @Override 092 public void beginTree(DetailAST ast) { 093 defined = false; 094 } 095 096 @Override 097 public void finishTree(DetailAST ast) { 098 if (!defined) { 099 int lineNumber = DEFAULT_LINE_NUMBER; 100 if (ast != null) { 101 lineNumber = ast.getLineNo(); 102 } 103 log(lineNumber, MSG_KEY_MISSING); 104 } 105 } 106 107 @Override 108 public void visitToken(DetailAST ast) { 109 defined = true; 110 111 if (matchDirectoryStructure) { 112 final DetailAST packageNameAst = ast.getLastChild().getPreviousSibling(); 113 final FullIdent fullIdent = FullIdent.createFullIdent(packageNameAst); 114 final String packageName = fullIdent.getText().replace('.', File.separatorChar); 115 116 final String directoryName = getDirectoryName(); 117 118 if (!directoryName.endsWith(packageName)) { 119 log(fullIdent.getLineNo(), MSG_KEY_MISMATCH, packageName); 120 } 121 } 122 } 123 124 /** 125 * Returns the directory name this file is in. 126 * @return Directory name. 127 */ 128 private String getDirectoryName() { 129 final String fileName = getFileContents().getFileName(); 130 final int lastSeparatorPos = fileName.lastIndexOf(File.separatorChar); 131 return fileName.substring(0, lastSeparatorPos); 132 } 133 134}