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