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.annotation; 020 021import com.puppycrawl.tools.checkstyle.api.AnnotationUtility; 022import com.puppycrawl.tools.checkstyle.api.Check; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025 026/** 027 * This check makes sure that all package annotations are in the 028 * package-info.java file. 029 * 030 * <p> 031 * According to the Java JLS 3rd ed. 032 * </p> 033 * 034 * <p> 035 * The JLS does not enforce the placement of package annotations. 036 * This placement may vary based on implementation. The JLS 037 * does highly recommend that all package annotations are 038 * placed in the package-info.java file. 039 * 040 * See <a 041 * href="http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html"> 042 * Java Language specification, sections 7.4.1.1</a>. 043 * </p> 044 * @author Travis Schneeberger 045 */ 046public class PackageAnnotationCheck extends Check 047{ 048 049 /** {@inheritDoc} */ 050 @Override 051 public int[] getDefaultTokens() 052 { 053 return this.getRequiredTokens(); 054 } 055 056 /** {@inheritDoc} */ 057 @Override 058 public int[] getRequiredTokens() 059 { 060 return new int[] { 061 TokenTypes.PACKAGE_DEF, 062 }; 063 } 064 065 /** {@inheritDoc} */ 066 @Override 067 public int[] getAcceptableTokens() 068 { 069 return this.getRequiredTokens(); 070 } 071 072 /** {@inheritDoc} */ 073 @Override 074 public void visitToken(final DetailAST ast) 075 { 076 final boolean containsAnnotation = 077 AnnotationUtility.containsAnnotation(ast); 078 final boolean inPackageInfo = 079 this.getFileContents().inPackageInfo(); 080 081 if (containsAnnotation && !inPackageInfo) { 082 this.log(ast.getLine(), "annotation.package.location"); 083 } 084 } 085}