1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.annotation;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26 import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
27 import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
28
29 /**
30 * <div>
31 * Checks that all package annotations are in the package-info.java file.
32 * </div>
33 *
34 * <p>
35 * For Java SE8 and above, placement of package annotations in the package-info.java
36 * file is enforced by the compiler and this check is not necessary.
37 * </p>
38 *
39 * <p>
40 * For Java SE7 and below, the Java Language Specification highly recommends
41 * but doesn't require that annotations are placed in the package-info.java file,
42 * and this check can help to enforce that placement.
43 * </p>
44 *
45 * <p>
46 * See <a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.4.1">
47 * Java Language Specification, §7.4.1</a> for more info.
48 * </p>
49 *
50 * @since 5.0
51 */
52 @StatelessCheck
53 public class PackageAnnotationCheck extends AbstractCheck {
54
55 /**
56 * A key is pointing to the warning message text in "messages.properties"
57 * file.
58 */
59 public static final String MSG_KEY = "annotation.package.location";
60
61 @Override
62 public int[] getDefaultTokens() {
63 return getRequiredTokens();
64 }
65
66 @Override
67 public int[] getRequiredTokens() {
68 return new int[] {
69 TokenTypes.PACKAGE_DEF,
70 };
71 }
72
73 @Override
74 public int[] getAcceptableTokens() {
75 return getRequiredTokens();
76 }
77
78 @Override
79 public void visitToken(final DetailAST ast) {
80 final boolean containsAnnotation =
81 AnnotationUtil.containsAnnotation(ast);
82
83 if (containsAnnotation && !CheckUtil.isPackageInfo(getFilePath())) {
84 log(ast, MSG_KEY);
85 }
86 }
87
88 }