View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.naming;
21  
22  import java.util.regex.Pattern;
23  
24  import com.puppycrawl.tools.checkstyle.StatelessCheck;
25  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26  import com.puppycrawl.tools.checkstyle.api.DetailAST;
27  import com.puppycrawl.tools.checkstyle.api.FullIdent;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  
30  /**
31   * <div>
32   * Checks that package names conform to a specified pattern.
33   * </div>
34   *
35   * <p>
36   * The default value of {@code format} for module {@code PackageName} has been chosen to match
37   * the requirements in the
38   * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.5.3">
39   * Java Language specification</a>
40   * and the Sun coding conventions. However, both underscores and uppercase letters are rather
41   * uncommon, so most configurations should probably assign value
42   * {@code ^[a-z]+(\.[a-z][a-z0-9]*)*$} to {@code format} for module {@code PackageName}.
43   * </p>
44   *
45   * @since 3.0
46   */
47  @StatelessCheck
48  public class PackageNameCheck
49      extends AbstractCheck {
50  
51      /**
52       * A key is pointing to the warning message text in "messages.properties"
53       * file.
54       */
55      public static final String MSG_KEY = "name.invalidPattern";
56  
57      /** Control the pattern to match valid identifiers. */
58      // Uppercase letters seem rather uncommon, but they're allowed in
59      // https://docs.oracle.com/javase/specs/
60      //  second_edition/html/packages.doc.html#40169
61      private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_]\\w*)*$");
62  
63      /**
64       * Creates a new {@code PackageNameCheck} instance.
65       */
66      public PackageNameCheck() {
67          // no code by default
68      }
69  
70      /**
71       * Setter to control the pattern to match valid identifiers.
72       *
73       * @param pattern the new pattern
74       * @since 3.0
75       */
76      public void setFormat(Pattern pattern) {
77          format = pattern;
78      }
79  
80      @Override
81      public int[] getDefaultTokens() {
82          return getRequiredTokens();
83      }
84  
85      @Override
86      public int[] getAcceptableTokens() {
87          return getRequiredTokens();
88      }
89  
90      @Override
91      public int[] getRequiredTokens() {
92          return new int[] {TokenTypes.PACKAGE_DEF};
93      }
94  
95      @Override
96      public void visitToken(DetailAST ast) {
97          final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
98          final FullIdent full = FullIdent.createFullIdent(nameAST);
99          if (!format.matcher(full.getText()).find()) {
100             log(full.getDetailAst(),
101                 MSG_KEY,
102                 full.getText(),
103                 format.pattern());
104         }
105     }
106 
107 }