001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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 extends AbstractCheck {
036
037    /** The key for the message. */
038    private final String messageKey;
039
040    /** The regexp to match against. */
041    private Pattern format;
042
043    /**
044     * Creates a new {@code AbstractNameCheck} instance.
045     *
046     * @param format format to check with
047     * @param messageKey the key for the message
048     */
049    protected AbstractNameCheck(String format, String messageKey) {
050        this.format = CommonUtil.createPattern(format);
051        this.messageKey = messageKey;
052    }
053
054    /**
055     * Decides whether the name of an AST should be checked against the format regexp.
056     *
057     * @param ast the AST to check.
058     * @return true if the IDENT subnode of ast should be checked against the format regexp.
059     */
060    protected abstract boolean mustCheckName(DetailAST ast);
061
062    /**
063     * Sets the pattern to match valid identifiers.
064     *
065     * @param pattern the new pattern
066     */
067    public final void setFormat(Pattern pattern) {
068        format = pattern;
069    }
070
071    @Override
072    public void visitToken(DetailAST ast) {
073        if (mustCheckName(ast)) {
074            final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
075            if (!format.matcher(nameAST.getText()).find()) {
076                log(nameAST,
077                    messageKey,
078                    nameAST.getText(),
079                    format.pattern());
080            }
081        }
082    }
083
084}