001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks.coding; 020 021import com.puppycrawl.tools.checkstyle.api.Check; 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024 025/** 026 * Ensures there is a package declaration. 027 * Rationale: Classes that live in the null package cannot be 028 * imported. Many novice developers are not aware of this. 029 * 030 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> 031 * @author Oliver Burn 032 */ 033public final class PackageDeclarationCheck extends Check 034{ 035 /** is package defined. */ 036 private boolean defined; 037 038 @Override 039 public int[] getDefaultTokens() 040 { 041 return new int[] {TokenTypes.PACKAGE_DEF}; 042 } 043 044 @Override 045 public int[] getRequiredTokens() 046 { 047 return getDefaultTokens(); 048 } 049 050 @Override 051 public void beginTree(DetailAST ast) 052 { 053 defined = false; 054 } 055 056 @Override 057 public void finishTree(DetailAST ast) 058 { 059 if (!defined) { 060 log(ast.getLineNo(), "missing.package.declaration"); 061 } 062 } 063 064 @Override 065 public void visitToken(DetailAST ast) 066 { 067 defined = true; 068 } 069}