View Javadoc
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.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   * <ul>
45   * <li>
46   * Property {@code format} - Control the pattern to match valid identifiers.
47   * Type is {@code java.util.regex.Pattern}.
48   * Default value is {@code "^[a-z]+(\.[a-zA-Z_]\w*)*$"}.
49   * </li>
50   * </ul>
51   *
52   * <p>
53   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
54   * </p>
55   *
56   * <p>
57   * Violation Message Keys:
58   * </p>
59   * <ul>
60   * <li>
61   * {@code name.invalidPattern}
62   * </li>
63   * </ul>
64   *
65   * @since 3.0
66   */
67  @StatelessCheck
68  public class PackageNameCheck
69      extends AbstractCheck {
70  
71      /**
72       * A key is pointing to the warning message text in "messages.properties"
73       * file.
74       */
75      public static final String MSG_KEY = "name.invalidPattern";
76  
77      /** Control the pattern to match valid identifiers. */
78      // Uppercase letters seem rather uncommon, but they're allowed in
79      // https://docs.oracle.com/javase/specs/
80      //  second_edition/html/packages.doc.html#40169
81      private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_]\\w*)*$");
82  
83      /**
84       * Setter to control the pattern to match valid identifiers.
85       *
86       * @param pattern the new pattern
87       * @since 3.0
88       */
89      public void setFormat(Pattern pattern) {
90          format = pattern;
91      }
92  
93      @Override
94      public int[] getDefaultTokens() {
95          return getRequiredTokens();
96      }
97  
98      @Override
99      public int[] getAcceptableTokens() {
100         return getRequiredTokens();
101     }
102 
103     @Override
104     public int[] getRequiredTokens() {
105         return new int[] {TokenTypes.PACKAGE_DEF};
106     }
107 
108     @Override
109     public void visitToken(DetailAST ast) {
110         final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
111         final FullIdent full = FullIdent.createFullIdent(nameAST);
112         if (!format.matcher(full.getText()).find()) {
113             log(full.getDetailAst(),
114                 MSG_KEY,
115                 full.getText(),
116                 format.pattern());
117         }
118     }
119 
120 }