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"
042     * file.
043     */
044    public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
045
046    /**
047     * Creates a new {@code IllegalIdentifierNameCheck} instance.
048     */
049    public IllegalIdentifierNameCheck() {
050        super("^(?!var$|\\S*\\$)\\S+$", MSG_INVALID_PATTERN);
051    }
052
053    @Override
054    public int[] getDefaultTokens() {
055        return getAcceptableTokens();
056    }
057
058    @Override
059    public int[] getAcceptableTokens() {
060        return new int[] {
061            TokenTypes.CLASS_DEF,
062            TokenTypes.INTERFACE_DEF,
063            TokenTypes.ENUM_DEF,
064            TokenTypes.ANNOTATION_DEF,
065            TokenTypes.ANNOTATION_FIELD_DEF,
066            TokenTypes.PARAMETER_DEF,
067            TokenTypes.VARIABLE_DEF,
068            TokenTypes.METHOD_DEF,
069            TokenTypes.ENUM_CONSTANT_DEF,
070            TokenTypes.PATTERN_VARIABLE_DEF,
071            TokenTypes.RECORD_DEF,
072            TokenTypes.RECORD_COMPONENT_DEF,
073            TokenTypes.LAMBDA,
074        };
075    }
076
077    @Override
078    public int[] getRequiredTokens() {
079        return CommonUtil.EMPTY_INT_ARRAY;
080    }
081
082    @Override
083    protected boolean mustCheckName(DetailAST ast) {
084        return ast.findFirstToken(TokenTypes.IDENT) != null;
085    }
086
087}