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 com.puppycrawl.tools.checkstyle.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
026
027/**
028 * <div>
029 * Checks identifiers against a regular expression pattern to detect illegal names. Since this
030 * check uses a pattern to define <i>valid</i> identifiers, users will need to use negative
031 * lookaheads to explicitly ban certain names (e.g., "var") or patterns (e.g., any identifier
032 * containing $) while still allowing all other valid identifiers.
033 * </div>
034 *
035 * @since 8.36
036 */
037@StatelessCheck
038public class IllegalIdentifierNameCheck extends AbstractNameCheck {
039
040    /**
041     * A key is pointing to the warning message text in "messages.properties" file.
042     */
043    public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
044
045    /**
046     * Creates a new {@code IllegalIdentifierNameCheck} instance.
047     */
048    public IllegalIdentifierNameCheck() {
049        super("^(?!var$|\\S*\\$)\\S+$", MSG_INVALID_PATTERN);
050    }
051
052    @Override
053    public int[] getDefaultTokens() {
054        return getAcceptableTokens();
055    }
056
057    @Override
058    public int[] getAcceptableTokens() {
059        return new int[] {
060            TokenTypes.CLASS_DEF,
061            TokenTypes.INTERFACE_DEF,
062            TokenTypes.ENUM_DEF,
063            TokenTypes.ANNOTATION_DEF,
064            TokenTypes.ANNOTATION_FIELD_DEF,
065            TokenTypes.PARAMETER_DEF,
066            TokenTypes.VARIABLE_DEF,
067            TokenTypes.METHOD_DEF,
068            TokenTypes.ENUM_CONSTANT_DEF,
069            TokenTypes.PATTERN_VARIABLE_DEF,
070            TokenTypes.RECORD_DEF,
071            TokenTypes.RECORD_COMPONENT_DEF,
072            TokenTypes.LAMBDA,
073        };
074    }
075
076    @Override
077    public int[] getRequiredTokens() {
078        return CommonUtil.EMPTY_INT_ARRAY;
079    }
080
081    @Override
082    protected boolean mustCheckName(DetailAST ast) {
083        return ast.findFirstToken(TokenTypes.IDENT) != null;
084    }
085
086}