1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 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 * <ul>
36 * <li>
37 * Property {@code format} - Sets the pattern to match valid identifiers.
38 * Type is {@code java.util.regex.Pattern}.
39 * Default value is {@code "^(?!var$|\S*\$)\S+$"}.
40 * </li>
41 * <li>
42 * Property {@code tokens} - tokens to check
43 * Type is {@code java.lang.String[]}.
44 * Validation type is {@code tokenSet}.
45 * Default value is:
46 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
47 * CLASS_DEF</a>,
48 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
49 * INTERFACE_DEF</a>,
50 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
51 * ENUM_DEF</a>,
52 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
53 * ANNOTATION_DEF</a>,
54 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_FIELD_DEF">
55 * ANNOTATION_FIELD_DEF</a>,
56 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">
57 * PARAMETER_DEF</a>,
58 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">
59 * VARIABLE_DEF</a>,
60 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
61 * METHOD_DEF</a>,
62 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_CONSTANT_DEF">
63 * ENUM_CONSTANT_DEF</a>,
64 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PATTERN_VARIABLE_DEF">
65 * PATTERN_VARIABLE_DEF</a>,
66 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
67 * RECORD_DEF</a>,
68 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_COMPONENT_DEF">
69 * RECORD_COMPONENT_DEF</a>,
70 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAMBDA">
71 * LAMBDA</a>.
72 * </li>
73 * </ul>
74 *
75 * <p>
76 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
77 * </p>
78 *
79 * <p>
80 * Violation Message Keys:
81 * </p>
82 * <ul>
83 * <li>
84 * {@code name.invalidPattern}
85 * </li>
86 * </ul>
87 *
88 * @since 8.36
89 */
90 @StatelessCheck
91 public class IllegalIdentifierNameCheck extends AbstractNameCheck {
92
93 /**
94 * Creates a new {@code IllegalIdentifierNameCheck} instance.
95 */
96 public IllegalIdentifierNameCheck() {
97 super("^(?!var$|\\S*\\$)\\S+$");
98 }
99
100 @Override
101 public int[] getDefaultTokens() {
102 return getAcceptableTokens();
103 }
104
105 @Override
106 public int[] getAcceptableTokens() {
107 return new int[] {
108 TokenTypes.CLASS_DEF,
109 TokenTypes.INTERFACE_DEF,
110 TokenTypes.ENUM_DEF,
111 TokenTypes.ANNOTATION_DEF,
112 TokenTypes.ANNOTATION_FIELD_DEF,
113 TokenTypes.PARAMETER_DEF,
114 TokenTypes.VARIABLE_DEF,
115 TokenTypes.METHOD_DEF,
116 TokenTypes.ENUM_CONSTANT_DEF,
117 TokenTypes.PATTERN_VARIABLE_DEF,
118 TokenTypes.RECORD_DEF,
119 TokenTypes.RECORD_COMPONENT_DEF,
120 TokenTypes.LAMBDA,
121 };
122 }
123
124 @Override
125 public int[] getRequiredTokens() {
126 return CommonUtil.EMPTY_INT_ARRAY;
127 }
128
129 @Override
130 protected boolean mustCheckName(DetailAST ast) {
131 return ast.findFirstToken(TokenTypes.IDENT) != null;
132 }
133
134 }