View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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. The format is a
34   * {@link Pattern regular expression}
35   * and defaults to
36   * <strong>^[a-z]+(\.[a-zA-Z_][a-zA-Z_0-9_]*)*$</strong>.
37   * </p>
38   * <p>
39   * The default format has been chosen to match the requirements in the
40   * <a
41   * href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html">
42   * Java Language specification</a> and the Sun coding conventions.
43   * However both underscores and uppercase letters are rather uncommon,
44   * so most projects should probably use
45   * <strong>^[a-z]+(\.[a-z][a-z0-9]*)*$</strong>.
46   * </p>
47   * <p>
48   * An example of how to configure the check is:
49   * </p>
50   * <pre>
51   * &lt;module name="PackageName"/&gt;
52   * </pre>
53   * <p>
54   * An example of how to configure the check for package names that begin with
55   * {@code com.puppycrawl.tools.checkstyle} is:
56   * </p>
57   * <pre>
58   * &lt;module name="PackageName"&gt;
59   *    &lt;property name="format"
60   *              value="^com\.puppycrawl\.tools\.checkstyle(\.[a-zA-Z_][a-zA-Z_0-9]*)*$"/&gt;
61   * &lt;/module&gt;
62   * </pre>
63   *
64   * @author Oliver Burn
65   */
66  @StatelessCheck
67  public class PackageNameCheck
68      extends AbstractCheck {
69  
70      /**
71       * A key is pointing to the warning message text in "messages.properties"
72       * file.
73       */
74      public static final String MSG_KEY = "name.invalidPattern";
75  
76      /** The regexp to match against. */
77      // Uppercase letters seem rather uncommon, but they're allowed in
78      // https://docs.oracle.com/javase/specs/
79      //  second_edition/html/packages.doc.html#40169
80      private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$");
81  
82      /**
83       * Set the format for the specified regular expression.
84       * @param pattern the new pattern
85       */
86      public void setFormat(Pattern pattern) {
87          format = pattern;
88      }
89  
90      @Override
91      public int[] getDefaultTokens() {
92          return getRequiredTokens();
93      }
94  
95      @Override
96      public int[] getAcceptableTokens() {
97          return getRequiredTokens();
98      }
99  
100     @Override
101     public int[] getRequiredTokens() {
102         return new int[] {TokenTypes.PACKAGE_DEF};
103     }
104 
105     @Override
106     public void visitToken(DetailAST ast) {
107         final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
108         final FullIdent full = FullIdent.createFullIdent(nameAST);
109         if (!format.matcher(full.getText()).find()) {
110             log(full.getLineNo(),
111                 full.getColumnNo(),
112                 MSG_KEY,
113                 full.getText(),
114                 format.pattern());
115         }
116     }
117 
118 }