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