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.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
029
030/**
031 * Abstract class for checking that names conform to a specified format.
032 *
033 */
034@StatelessCheck
035public abstract class AbstractNameCheck
036    extends AbstractCheck {
037
038    /**
039     * Message key for invalid pattern violation.
040     */
041    public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
042
043    /** The regexp to match against. */
044    private Pattern format;
045
046    /**
047     * Creates a new {@code AbstractNameCheck} instance.
048     *
049     * @param format format to check with
050     */
051    protected AbstractNameCheck(String format) {
052        this.format = CommonUtil.createPattern(format);
053    }
054
055    /**
056     * Decides whether the name of an AST should be checked against
057     * the format regexp.
058     *
059     * @param ast the AST to check.
060     * @return true if the IDENT subnode of ast should be checked against
061     *     the format regexp.
062     */
063    protected abstract boolean mustCheckName(DetailAST ast);
064
065    /**
066     * Sets the pattern to match valid identifiers.
067     *
068     * @param pattern the new pattern
069     */
070    public final void setFormat(Pattern pattern) {
071        format = pattern;
072    }
073
074    @Override
075    public void visitToken(DetailAST ast) {
076        if (mustCheckName(ast)) {
077            final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
078            if (!format.matcher(nameAST.getText()).find()) {
079                log(nameAST,
080                    MSG_INVALID_PATTERN,
081                    nameAST.getText(),
082                    format.pattern());
083            }
084        }
085    }
086
087}