001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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 format specified
033 * by the format property. The format is a
034 * {@link Pattern regular expression}
035 * and defaults to
036 * <strong>^[a-z]+(\.[a-zA-Z_][a-zA-Z_0-9_]*)*$</strong>.
037 * </p>
038 * <p>
039 * The default format has been chosen to match the requirements in the
040 * <a
041 * href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html">
042 * Java Language specification</a> and the Sun coding conventions.
043 * However both underscores and uppercase letters are rather uncommon,
044 * so most projects should probably use
045 * <strong>^[a-z]+(\.[a-z][a-z0-9]*)*$</strong>.
046 * </p>
047 * <p>
048 * An example of how to configure the check is:
049 * </p>
050 * <pre>
051 * &lt;module name="PackageName"/&gt;
052 * </pre>
053 * <p>
054 * An example of how to configure the check for package names that begin with
055 * {@code com.puppycrawl.tools.checkstyle} is:
056 * </p>
057 * <pre>
058 * &lt;module name="PackageName"&gt;
059 *    &lt;property name="format"
060 *              value="^com\.puppycrawl\.tools\.checkstyle(\.[a-zA-Z_][a-zA-Z_0-9]*)*$"/&gt;
061 * &lt;/module&gt;
062 * </pre>
063 *
064 * @author Oliver Burn
065 */
066@StatelessCheck
067public class PackageNameCheck
068    extends AbstractCheck {
069    /**
070     * A key is pointing to the warning message text in "messages.properties"
071     * file.
072     */
073    public static final String MSG_KEY = "name.invalidPattern";
074
075    /** The regexp to match against. */
076    // Uppercase letters seem rather uncommon, but they're allowed in
077    // https://docs.oracle.com/javase/specs/
078    //  second_edition/html/packages.doc.html#40169
079    private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$");
080
081    /**
082     * Set the format for the specified regular expression.
083     * @param pattern the new pattern
084     */
085    public void setFormat(Pattern pattern) {
086        format = pattern;
087    }
088
089    @Override
090    public int[] getDefaultTokens() {
091        return getRequiredTokens();
092    }
093
094    @Override
095    public int[] getAcceptableTokens() {
096        return getRequiredTokens();
097    }
098
099    @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}