Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/checks/PackageNameCheck.java


1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2002  Oliver Burn
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  package com.puppycrawl.tools.checkstyle.checks;
20  
21  import com.puppycrawl.tools.checkstyle.api.DetailAST;
22  import com.puppycrawl.tools.checkstyle.api.FullIdent;
23  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24  
25  /**
26   * <p>
27   * Checks that package names conform to a format specified
28   * by the format property. The format is a
29   * <a href="http://jakarta.apache.org/regexp/apidocs/org/apache/regexp/RE.html">
30   * regular expression</a>
31   * and defaults to
32   * <strong>^[a-z]+(\.[a-zA-Z_][a-zA-Z_0-9]*)*$</strong>.
33   * </p>
34   * The default format has been chosen to match the requirements in the
35   * <a
36   * href="http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#40169">
37   * Java Language specification</a> and the Sun coding conventions.
38   * However both underscores and uppercase letters are rather uncommon,
39   * so most projects should probably use
40   * <strong>^[a-z]+(\.[a-z][a-z0-9]*)*$</strong>.
41   * </p>
42   * <p>
43   * An example of how to configure the check is:
44   * </p>
45   * <pre>
46   * &lt;module name="PackageName"/&gt;
47   * </pre> 
48   * <p>
49   * An example of how to configure the check for package names that begin with
50   * <code>com.puppycrawl.tools.checkstyle</code> is:
51   * </p>
52   * <pre>
53   * &lt;module name="PackageName"&gt;
54   *    &lt;property name="format"
55   *              value="^com\.puppycrawl\.tools\.checkstyle(\\.[a-zA-Z_][a-zA-Z_0-9]*)*$"/&gt;
56   * &lt;/module&gt;
57   * </pre>
58   *
59   * @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
60   * @version 1.0
61   */
62  public class PackageNameCheck
63      extends AbstractFormatCheck
64  {
65      /**
66       * Creates a new <code>PackageNameCheck</code> instance.
67       */
68      public PackageNameCheck()
69      {
70          // Uppercase letters seem rather uncommon, but they're allowed in
71          // http://java.sun.com/docs/books/jls/
72          //   second_edition/html/packages.doc.html#40169
73          super("^[a-z]+(\\.[a-zA-Z_][a-zA-Z_0-9]*)*$");
74      }
75  
76      /** @see com.puppycrawl.tools.checkstyle.api.Check */
77      public int[] getDefaultTokens()
78      {
79          return new int[] {TokenTypes.PACKAGE_DEF};
80      }
81  
82      /** @see com.puppycrawl.tools.checkstyle.api.Check */
83      public void visitToken(DetailAST aAST)
84      {
85          final DetailAST nameAST = (DetailAST) aAST.getFirstChild();
86          final FullIdent full = FullIdent.createFullIdent(nameAST);
87          if (!getRegexp().match(full.getText())) {
88              log(full.getLineNo(),
89                  full.getColumnNo(),
90                  "name.invalidPattern",
91                  full.getText(),
92                  getFormat());
93          }
94      }
95  }