001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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    /**
071     * A key is pointing to the warning message text in "messages.properties"
072     * file.
073     */
074    public static final String MSG_KEY = "name.invalidPattern";
075
076    /** The regexp to match against. */
077    // Uppercase letters seem rather uncommon, but they're allowed in
078    // https://docs.oracle.com/javase/specs/
079    //  second_edition/html/packages.doc.html#40169
080    private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$");
081
082    /**
083     * Set the format for the specified regular expression.
084     * @param pattern the new pattern
085     */
086    public void setFormat(Pattern pattern) {
087        format = pattern;
088    }
089
090    @Override
091    public int[] getDefaultTokens() {
092        return getRequiredTokens();
093    }
094
095    @Override
096    public int[] getAcceptableTokens() {
097        return getRequiredTokens();
098    }
099
100    @Override
101    public int[] getRequiredTokens() {
102        return new int[] {TokenTypes.PACKAGE_DEF};
103    }
104
105    @Override
106    public void visitToken(DetailAST ast) {
107        final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
108        final FullIdent full = FullIdent.createFullIdent(nameAST);
109        if (!format.matcher(full.getText()).find()) {
110            log(full.getLineNo(),
111                full.getColumnNo(),
112                MSG_KEY,
113                full.getText(),
114                format.pattern());
115        }
116    }
117
118}