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