001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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     * Creates a new {@code IllegalIdentifierNameCheck} instance.
042     */
043    public IllegalIdentifierNameCheck() {
044        super("^(?!var$|\\S*\\$)\\S+$");
045    }
046
047    @Override
048    public int[] getDefaultTokens() {
049        return getAcceptableTokens();
050    }
051
052    @Override
053    public int[] getAcceptableTokens() {
054        return new int[] {
055            TokenTypes.CLASS_DEF,
056            TokenTypes.INTERFACE_DEF,
057            TokenTypes.ENUM_DEF,
058            TokenTypes.ANNOTATION_DEF,
059            TokenTypes.ANNOTATION_FIELD_DEF,
060            TokenTypes.PARAMETER_DEF,
061            TokenTypes.VARIABLE_DEF,
062            TokenTypes.METHOD_DEF,
063            TokenTypes.ENUM_CONSTANT_DEF,
064            TokenTypes.PATTERN_VARIABLE_DEF,
065            TokenTypes.RECORD_DEF,
066            TokenTypes.RECORD_COMPONENT_DEF,
067            TokenTypes.LAMBDA,
068        };
069    }
070
071    @Override
072    public int[] getRequiredTokens() {
073        return CommonUtil.EMPTY_INT_ARRAY;
074    }
075
076    @Override
077    protected boolean mustCheckName(DetailAST ast) {
078        return ast.findFirstToken(TokenTypes.IDENT) != null;
079    }
080
081}