1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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   * <p>
32   * Checks that package names conform to a format specified
33   * by the format property.
34   * </p>
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">Java Language specification</a>
39   * and the Sun coding conventions. However both underscores and uppercase letters are rather
40   * uncommon, so most configurations should probably assign value
41   * {@code ^[a-z]+(\.[a-z][a-z0-9]*)*$} to {@code format} for module {@code PackageName}.
42   * </p>
43   * <ul>
44   * <li>
45   * Property {@code format} - Specifies valid identifiers. Default value is
46   * {@code "^[a-z]+(\.[a-zA-Z_][a-zA-Z0-9_]*)*$"}.
47   * </li>
48   * </ul>
49   * <p>
50   * An example of how to configure the check is:
51   * </p>
52   * <pre>
53   * &lt;module name="PackageName"/&gt;
54   * </pre>
55   * <p>
56   * An example of how to configure the check to ensure with packages start with a lowercase letter
57   * and only contains lowercase letters or numbers is:
58   * </p>
59   * <pre>
60   * &lt;module name=&quot;PackageName&quot;&gt;
61   *   &lt;property name=&quot;format&quot;
62   *     value=&quot;^[a-z]+(\.[a-z][a-z0-9]*)*$&quot;/&gt;
63   * &lt;/module&gt;
64   * </pre>
65   *
66   * @since 3.0
67   */
68  @StatelessCheck
69  public class PackageNameCheck
70      extends AbstractCheck {
71  
72      /**
73       * A key is pointing to the warning message text in "messages.properties"
74       * file.
75       */
76      public static final String MSG_KEY = "name.invalidPattern";
77  
78      /** Specifies valid identifiers. */
79      // Uppercase letters seem rather uncommon, but they're allowed in
80      // https://docs.oracle.com/javase/specs/
81      //  second_edition/html/packages.doc.html#40169
82      private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$");
83  
84      /**
85       * Setter to specifies valid identifiers.
86       * @param pattern the new pattern
87       */
88      public void setFormat(Pattern pattern) {
89          format = pattern;
90      }
91  
92      @Override
93      public int[] getDefaultTokens() {
94          return getRequiredTokens();
95      }
96  
97      @Override
98      public int[] getAcceptableTokens() {
99          return getRequiredTokens();
100     }
101 
102     @Override
103     public int[] getRequiredTokens() {
104         return new int[] {TokenTypes.PACKAGE_DEF};
105     }
106 
107     @Override
108     public void visitToken(DetailAST ast) {
109         final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
110         final FullIdent full = FullIdent.createFullIdent(nameAST);
111         if (!format.matcher(full.getText()).find()) {
112             log(full.getDetailAst(),
113                 MSG_KEY,
114                 full.getText(),
115                 format.pattern());
116         }
117     }
118 
119 }