001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.naming;
021
022import java.util.regex.Pattern;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.FullIdent;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029
030/**
031 * <p>
032 * Checks that package names conform to a specified pattern.
033 * </p>
034 * <p>
035 * The default value of {@code format} for module {@code PackageName} has been chosen to match
036 * the requirements in the
037 * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.5.3">
038 * Java Language specification</a>
039 * and the Sun coding conventions. However both underscores and uppercase letters are rather
040 * uncommon, so most configurations should probably assign value
041 * {@code ^[a-z]+(\.[a-z][a-z0-9]*)*$} to {@code format} for module {@code PackageName}.
042 * </p>
043 * <ul>
044 * <li>
045 * Property {@code format} - Specifies valid identifiers. Default value is
046 * {@code "^[a-z]+(\.[a-zA-Z_][a-zA-Z0-9_]*)*$"}.
047 * </li>
048 * </ul>
049 * <p>
050 * An example of how to configure the check is:
051 * </p>
052 * <pre>
053 * &lt;module name="PackageName"/&gt;
054 * </pre>
055 * <p>Code Example:</p>
056 * <pre>
057 * package com; // OK
058 * package COM; // violation, name 'COM' must match pattern '^[a-z]+(\.[a-zA-Z_][a-zA-Z0-9_]*)*$'
059 * package com.checkstyle.checks; // OK
060 * package com.A.checkstyle.checks; // OK
061 * package com.checkstyle1.checks; // OK
062 * package com.checkSTYLE.checks; // OK
063 * package com._checkstyle.checks_; // OK
064 * </pre>
065 * <p>
066 * An example of how to configure the check to ensure with packages start with a lowercase letter
067 * and only contains lowercase letters or numbers is:
068 * </p>
069 * <pre>
070 * &lt;module name=&quot;PackageName&quot;&gt;
071 *   &lt;property name=&quot;format&quot;
072 *     value=&quot;^[a-z]+(\.[a-z][a-z0-9]*)*$&quot;/&gt;
073 * &lt;/module&gt;
074 * </pre>
075 * <p>Code Example:</p>
076 * <pre>
077 * package com; // OK
078 * package COM; // violation, name 'COM' must match pattern '^[a-z]+(\.[a-z][a-z0-9]*)*$'
079 * package com.checkstyle.checks; // OK
080 * package com.A.checkstyle.checks; // violation, name 'com.A.checkstyle' must match
081 *                                  // pattern '^[a-z]+(\.[a-z][a-z0-9]*)*$'
082 * package com.checkstyle1.checks; // OK
083 * package com.checkSTYLE.checks; // violation, name 'com.checkSTYLE.checks' must
084 *                                // match pattern '^[a-z]+(\.[a-z][a-z0-9]*)*$'
085 * package com._checkstyle.checks_; // violation, name 'com._checkstyle.checks_' must match
086 *                                  // pattern '^[a-z]+(\.[a-z][a-z0-9]*)*$'
087 * </pre>
088 *
089 * @since 3.0
090 */
091@StatelessCheck
092public class PackageNameCheck
093    extends AbstractCheck {
094
095    /**
096     * A key is pointing to the warning message text in "messages.properties"
097     * file.
098     */
099    public static final String MSG_KEY = "name.invalidPattern";
100
101    /** Specifies valid identifiers. */
102    // Uppercase letters seem rather uncommon, but they're allowed in
103    // https://docs.oracle.com/javase/specs/
104    //  second_edition/html/packages.doc.html#40169
105    private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$");
106
107    /**
108     * Setter to specifies valid identifiers.
109     * @param pattern the new pattern
110     */
111    public void setFormat(Pattern pattern) {
112        format = pattern;
113    }
114
115    @Override
116    public int[] getDefaultTokens() {
117        return getRequiredTokens();
118    }
119
120    @Override
121    public int[] getAcceptableTokens() {
122        return getRequiredTokens();
123    }
124
125    @Override
126    public int[] getRequiredTokens() {
127        return new int[] {TokenTypes.PACKAGE_DEF};
128    }
129
130    @Override
131    public void visitToken(DetailAST ast) {
132        final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
133        final FullIdent full = FullIdent.createFullIdent(nameAST);
134        if (!format.matcher(full.getText()).find()) {
135            log(full.getDetailAst(),
136                MSG_KEY,
137                full.getText(),
138                format.pattern());
139        }
140    }
141
142}