001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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 * <div>
032 * Checks that package names conform to a specified pattern.
033 * </div>
034 *
035 * <p>
036 * The default value of {@code format} for module {@code PackageName} has been chosen to match
037 * the requirements in the
038 * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.5.3">
039 * Java Language specification</a>
040 * and the Sun coding conventions. However, both underscores and uppercase letters are rather
041 * uncommon, so most configurations should probably assign value
042 * {@code ^[a-z]+(\.[a-z][a-z0-9]*)*$} to {@code format} for module {@code PackageName}.
043 * </p>
044 * <ul>
045 * <li>
046 * Property {@code format} - Control the pattern to match valid identifiers.
047 * Type is {@code java.util.regex.Pattern}.
048 * Default value is {@code "^[a-z]+(\.[a-zA-Z_]\w*)*$"}.
049 * </li>
050 * </ul>
051 *
052 * <p>
053 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
054 * </p>
055 *
056 * <p>
057 * Violation Message Keys:
058 * </p>
059 * <ul>
060 * <li>
061 * {@code name.invalidPattern}
062 * </li>
063 * </ul>
064 *
065 * @since 3.0
066 */
067@StatelessCheck
068public class PackageNameCheck
069    extends AbstractCheck {
070
071    /**
072     * A key is pointing to the warning message text in "messages.properties"
073     * file.
074     */
075    public static final String MSG_KEY = "name.invalidPattern";
076
077    /** Control the pattern to match valid identifiers. */
078    // Uppercase letters seem rather uncommon, but they're allowed in
079    // https://docs.oracle.com/javase/specs/
080    //  second_edition/html/packages.doc.html#40169
081    private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_]\\w*)*$");
082
083    /**
084     * Setter to control the pattern to match valid identifiers.
085     *
086     * @param pattern the new pattern
087     * @since 3.0
088     */
089    public void setFormat(Pattern pattern) {
090        format = pattern;
091    }
092
093    @Override
094    public int[] getDefaultTokens() {
095        return getRequiredTokens();
096    }
097
098    @Override
099    public int[] getAcceptableTokens() {
100        return getRequiredTokens();
101    }
102
103    @Override
104    public int[] getRequiredTokens() {
105        return new int[] {TokenTypes.PACKAGE_DEF};
106    }
107
108    @Override
109    public void visitToken(DetailAST ast) {
110        final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
111        final FullIdent full = FullIdent.createFullIdent(nameAST);
112        if (!format.matcher(full.getText()).find()) {
113            log(full.getDetailAst(),
114                MSG_KEY,
115                full.getText(),
116                format.pattern());
117        }
118    }
119
120}