1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.naming;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.DetailAST;
24 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
26
27 /**
28 * <div>
29 * Checks identifiers against a regular expression pattern to detect illegal names. Since this
30 * check uses a pattern to define <i>valid</i> identifiers, users will need to use negative
31 * lookaheads to explicitly ban certain names (e.g., "var") or patterns (e.g., any identifier
32 * containing $) while still allowing all other valid identifiers.
33 * </div>
34 *
35 * @since 8.36
36 */
37 @StatelessCheck
38 public class IllegalIdentifierNameCheck extends AbstractNameCheck {
39
40 /**
41 * A key is pointing to the warning message text in "messages.properties"
42 * file.
43 */
44 public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
45
46 /**
47 * Creates a new {@code IllegalIdentifierNameCheck} instance.
48 */
49 public IllegalIdentifierNameCheck() {
50 super("^(?!var$|\\S*\\$)\\S+$", MSG_INVALID_PATTERN);
51 }
52
53 @Override
54 public int[] getDefaultTokens() {
55 return getAcceptableTokens();
56 }
57
58 @Override
59 public int[] getAcceptableTokens() {
60 return new int[] {
61 TokenTypes.CLASS_DEF,
62 TokenTypes.INTERFACE_DEF,
63 TokenTypes.ENUM_DEF,
64 TokenTypes.ANNOTATION_DEF,
65 TokenTypes.ANNOTATION_FIELD_DEF,
66 TokenTypes.PARAMETER_DEF,
67 TokenTypes.VARIABLE_DEF,
68 TokenTypes.METHOD_DEF,
69 TokenTypes.ENUM_CONSTANT_DEF,
70 TokenTypes.PATTERN_VARIABLE_DEF,
71 TokenTypes.RECORD_DEF,
72 TokenTypes.RECORD_COMPONENT_DEF,
73 TokenTypes.LAMBDA,
74 };
75 }
76
77 @Override
78 public int[] getRequiredTokens() {
79 return CommonUtil.EMPTY_INT_ARRAY;
80 }
81
82 @Override
83 protected boolean mustCheckName(DetailAST ast) {
84 return ast.findFirstToken(TokenTypes.IDENT) != null;
85 }
86
87 }