001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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 * <div> 031 * Checks that all package annotations are in the package-info.java file. 032 * </div> 033 * 034 * <p> 035 * For Java SE8 and above, placement of package annotations in the package-info.java 036 * file is enforced by the compiler and this check is not necessary. 037 * </p> 038 * 039 * <p> 040 * For Java SE7 and below, the Java Language Specification highly recommends 041 * but doesn't require that annotations are placed in the package-info.java file, 042 * and this check can help to enforce that placement. 043 * </p> 044 * 045 * <p> 046 * See <a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.4.1"> 047 * Java Language Specification, §7.4.1</a> for more info. 048 * </p> 049 * 050 * @since 5.0 051 */ 052@StatelessCheck 053public class PackageAnnotationCheck extends AbstractCheck { 054 055 /** 056 * A key is pointing to the warning message text in "messages.properties" 057 * file. 058 */ 059 public static final String MSG_KEY = "annotation.package.location"; 060 061 @Override 062 public int[] getDefaultTokens() { 063 return getRequiredTokens(); 064 } 065 066 @Override 067 public int[] getRequiredTokens() { 068 return new int[] { 069 TokenTypes.PACKAGE_DEF, 070 }; 071 } 072 073 @Override 074 public int[] getAcceptableTokens() { 075 return getRequiredTokens(); 076 } 077 078 @Override 079 public void visitToken(final DetailAST ast) { 080 final boolean containsAnnotation = 081 AnnotationUtil.containsAnnotation(ast); 082 083 if (containsAnnotation && !CheckUtil.isPackageInfo(getFilePath())) { 084 log(ast, MSG_KEY); 085 } 086 } 087 088}