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.annotation; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil; 027 028/** 029 * <p> 030 * Checks that all package annotations are in the package-info.java file. 031 * </p> 032 * <p> 033 * For Java SE8 and above, placement of package annotations in the package-info.java 034 * file is enforced by the compiler and this check is not necessary. 035 * </p> 036 * <p> 037 * For Java SE7 and below, the Java Language Specification highly recommends 038 * but doesn't require that annotations are placed in the package-info.java file, 039 * and this check can help to enforce that placement. 040 * </p> 041 * <p> 042 * See <a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.4.1"> 043 * Java Language Specification, §7.4.1</a> for more info. 044 * </p> 045 * <p> 046 * To configure the check: 047 * </p> 048 * <pre> 049 * <module name="PackageAnnotation"/> 050 * </pre> 051 * <p>Example of validating MyClass.java:</p> 052 * <pre> 053 * @Deprecated 054 * package com.example.annotations.packageannotation; //violation 055 * </pre> 056 * <p>Example of fixing violation in MyClass.java:</p> 057 * <pre> 058 * package com.example.annotations.packageannotation; //ok 059 * </pre> 060 * <p>Example of validating package-info.java:</p> 061 * <pre> 062 * @Deprecated 063 * package com.example.annotations.packageannotation; //ok 064 * </pre> 065 * 066 * @since 5.0 067 */ 068@StatelessCheck 069public class PackageAnnotationCheck extends AbstractCheck { 070 071 /** 072 * A key is pointing to the warning message text in "messages.properties" 073 * file. 074 */ 075 public static final String MSG_KEY = "annotation.package.location"; 076 077 @Override 078 public int[] getDefaultTokens() { 079 return getRequiredTokens(); 080 } 081 082 @Override 083 public int[] getRequiredTokens() { 084 return new int[] { 085 TokenTypes.PACKAGE_DEF, 086 }; 087 } 088 089 @Override 090 public int[] getAcceptableTokens() { 091 return getRequiredTokens(); 092 } 093 094 @Override 095 public void visitToken(final DetailAST ast) { 096 final boolean containsAnnotation = 097 AnnotationUtil.containsAnnotation(ast); 098 final boolean inPackageInfo = 099 getFileContents().inPackageInfo(); 100 101 if (containsAnnotation && !inPackageInfo) { 102 log(ast, MSG_KEY); 103 } 104 } 105 106}