View Javadoc
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   * <p>
31   * Checks that all package annotations are in the package-info.java file.
32   * </p>
33   * <p>
34   * For Java SE8 and above, placement of package annotations in the package-info.java
35   * file is enforced by the compiler and this check is not necessary.
36   * </p>
37   * <p>
38   * For Java SE7 and below, the Java Language Specification highly recommends
39   * but doesn't require that annotations are placed in the package-info.java file,
40   * and this check can help to enforce that placement.
41   * </p>
42   * <p>
43   * See <a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.4.1">
44   * Java Language Specification, &#167;7.4.1</a> for more info.
45   * </p>
46   * <p>
47   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
48   * </p>
49   * <p>
50   * Violation Message Keys:
51   * </p>
52   * <ul>
53   * <li>
54   * {@code annotation.package.location}
55   * </li>
56   * </ul>
57   *
58   * @since 5.0
59   */
60  @StatelessCheck
61  public class PackageAnnotationCheck extends AbstractCheck {
62  
63      /**
64       * A key is pointing to the warning message text in "messages.properties"
65       * file.
66       */
67      public static final String MSG_KEY = "annotation.package.location";
68  
69      @Override
70      public int[] getDefaultTokens() {
71          return getRequiredTokens();
72      }
73  
74      @Override
75      public int[] getRequiredTokens() {
76          return new int[] {
77              TokenTypes.PACKAGE_DEF,
78          };
79      }
80  
81      @Override
82      public int[] getAcceptableTokens() {
83          return getRequiredTokens();
84      }
85  
86      @Override
87      public void visitToken(final DetailAST ast) {
88          final boolean containsAnnotation =
89              AnnotationUtil.containsAnnotation(ast);
90  
91          if (containsAnnotation && !CheckUtil.isPackageInfo(getFilePath())) {
92              log(ast, MSG_KEY);
93          }
94      }
95  
96  }