001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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, &#167;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" file.
057     */
058    public static final String MSG_KEY = "annotation.package.location";
059
060    /**
061     * Creates a new {@code PackageAnnotationCheck} instance.
062     */
063    public PackageAnnotationCheck() {
064        // no code by default
065    }
066
067    @Override
068    public int[] getDefaultTokens() {
069        return getRequiredTokens();
070    }
071
072    @Override
073    public int[] getRequiredTokens() {
074        return new int[] {
075            TokenTypes.PACKAGE_DEF,
076        };
077    }
078
079    @Override
080    public int[] getAcceptableTokens() {
081        return getRequiredTokens();
082    }
083
084    @Override
085    public void visitToken(final DetailAST ast) {
086        final boolean containsAnnotation =
087            AnnotationUtil.containsAnnotation(ast);
088
089        if (containsAnnotation && !CheckUtil.isPackageInfo(getFilePath())) {
090            log(ast, MSG_KEY);
091        }
092    }
093
094}