1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 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 * <p>
51 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
52 * </p>
53 *
54 * <p>
55 * Violation Message Keys:
56 * </p>
57 * <ul>
58 * <li>
59 * {@code annotation.package.location}
60 * </li>
61 * </ul>
62 *
63 * @since 5.0
64 */
65 @StatelessCheck
66 public class PackageAnnotationCheck extends AbstractCheck {
67
68 /**
69 * A key is pointing to the warning message text in "messages.properties"
70 * file.
71 */
72 public static final String MSG_KEY = "annotation.package.location";
73
74 @Override
75 public int[] getDefaultTokens() {
76 return getRequiredTokens();
77 }
78
79 @Override
80 public int[] getRequiredTokens() {
81 return new int[] {
82 TokenTypes.PACKAGE_DEF,
83 };
84 }
85
86 @Override
87 public int[] getAcceptableTokens() {
88 return getRequiredTokens();
89 }
90
91 @Override
92 public void visitToken(final DetailAST ast) {
93 final boolean containsAnnotation =
94 AnnotationUtil.containsAnnotation(ast);
95
96 if (containsAnnotation && !CheckUtil.isPackageInfo(getFilePath())) {
97 log(ast, MSG_KEY);
98 }
99 }
100
101 }