001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 048 * </p> 049 * <p> 050 * Violation Message Keys: 051 * </p> 052 * <ul> 053 * <li> 054 * {@code annotation.package.location} 055 * </li> 056 * </ul> 057 * 058 * @since 5.0 059 */ 060@StatelessCheck 061public class PackageAnnotationCheck extends AbstractCheck { 062 063 /** 064 * A key is pointing to the warning message text in "messages.properties" 065 * file. 066 */ 067 public static final String MSG_KEY = "annotation.package.location"; 068 069 @Override 070 public int[] getDefaultTokens() { 071 return getRequiredTokens(); 072 } 073 074 @Override 075 public int[] getRequiredTokens() { 076 return new int[] { 077 TokenTypes.PACKAGE_DEF, 078 }; 079 } 080 081 @Override 082 public int[] getAcceptableTokens() { 083 return getRequiredTokens(); 084 } 085 086 @Override 087 public void visitToken(final DetailAST ast) { 088 final boolean containsAnnotation = 089 AnnotationUtil.containsAnnotation(ast); 090 091 if (containsAnnotation && !CheckUtil.isPackageInfo(getFilePath())) { 092 log(ast, MSG_KEY); 093 } 094 } 095 096}