View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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       * A key is pointing to the warning message text in "messages.properties"
71       * file.
72       */
73      public static final String MSG_KEY = "name.invalidPattern";
74  
75      /** The regexp to match against. */
76      // Uppercase letters seem rather uncommon, but they're allowed in
77      // https://docs.oracle.com/javase/specs/
78      //  second_edition/html/packages.doc.html#40169
79      private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$");
80  
81      /**
82       * Set the format for the specified regular expression.
83       * @param pattern the new pattern
84       */
85      public void setFormat(Pattern pattern) {
86          format = pattern;
87      }
88  
89      @Override
90      public int[] getDefaultTokens() {
91          return getRequiredTokens();
92      }
93  
94      @Override
95      public int[] getAcceptableTokens() {
96          return getRequiredTokens();
97      }
98  
99      @Override
100     public int[] getRequiredTokens() {
101         return new int[] {TokenTypes.PACKAGE_DEF};
102     }
103 
104     @Override
105     public void visitToken(DetailAST ast) {
106         final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
107         final FullIdent full = FullIdent.createFullIdent(nameAST);
108         if (!format.matcher(full.getText()).find()) {
109             log(full.getLineNo(),
110                 full.getColumnNo(),
111                 MSG_KEY,
112                 full.getText(),
113                 format.pattern());
114         }
115     }
116 }