1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 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 with a pattern for a set of illegal names, such as those
30 * that are restricted or contextual keywords. Examples include "yield", "record", and
31 * "var". Please read more at
32 * <a href="https://docs.oracle.com/javase/specs/jls/se22/html/jls-3.html#jls-3.9">
33 * Java Language Specification</a> to get to know more about restricted keywords. Since this
34 * check uses a pattern to specify valid identifiers, users can also prohibit the usage
35 * of certain symbols, such as "$", or any non-ascii character.
36 * </div>
37 *
38 * <ul>
39 * <li>
40 * Property {@code format} - Sets the pattern to match valid identifiers.
41 * Type is {@code java.util.regex.Pattern}.
42 * Default value is {@code "(?i)^(?!(record|yield|var|permits|sealed)$).+$"}.
43 * </li>
44 * <li>
45 * Property {@code tokens} - tokens to check
46 * Type is {@code java.lang.String[]}.
47 * Validation type is {@code tokenSet}.
48 * Default value is:
49 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
50 * CLASS_DEF</a>,
51 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
52 * INTERFACE_DEF</a>,
53 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
54 * ENUM_DEF</a>,
55 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
56 * ANNOTATION_DEF</a>,
57 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_FIELD_DEF">
58 * ANNOTATION_FIELD_DEF</a>,
59 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">
60 * PARAMETER_DEF</a>,
61 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">
62 * VARIABLE_DEF</a>,
63 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
64 * METHOD_DEF</a>,
65 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_CONSTANT_DEF">
66 * ENUM_CONSTANT_DEF</a>,
67 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PATTERN_VARIABLE_DEF">
68 * PATTERN_VARIABLE_DEF</a>,
69 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
70 * RECORD_DEF</a>,
71 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_COMPONENT_DEF">
72 * RECORD_COMPONENT_DEF</a>,
73 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAMBDA">
74 * LAMBDA</a>.
75 * </li>
76 * </ul>
77 *
78 * <p>
79 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
80 * </p>
81 *
82 * <p>
83 * Violation Message Keys:
84 * </p>
85 * <ul>
86 * <li>
87 * {@code name.invalidPattern}
88 * </li>
89 * </ul>
90 *
91 * @since 8.36
92 */
93 @StatelessCheck
94 public class IllegalIdentifierNameCheck extends AbstractNameCheck {
95
96 /**
97 * Creates a new {@code IllegalIdentifierNameCheck} instance.
98 */
99 public IllegalIdentifierNameCheck() {
100 super("(?i)^(?!(record|yield|var|permits|sealed)$).+$");
101 }
102
103 @Override
104 public int[] getDefaultTokens() {
105 return getAcceptableTokens();
106 }
107
108 @Override
109 public int[] getAcceptableTokens() {
110 return new int[] {
111 TokenTypes.CLASS_DEF,
112 TokenTypes.INTERFACE_DEF,
113 TokenTypes.ENUM_DEF,
114 TokenTypes.ANNOTATION_DEF,
115 TokenTypes.ANNOTATION_FIELD_DEF,
116 TokenTypes.PARAMETER_DEF,
117 TokenTypes.VARIABLE_DEF,
118 TokenTypes.METHOD_DEF,
119 TokenTypes.ENUM_CONSTANT_DEF,
120 TokenTypes.PATTERN_VARIABLE_DEF,
121 TokenTypes.RECORD_DEF,
122 TokenTypes.RECORD_COMPONENT_DEF,
123 TokenTypes.LAMBDA,
124 };
125 }
126
127 @Override
128 public int[] getRequiredTokens() {
129 return CommonUtil.EMPTY_INT_ARRAY;
130 }
131
132 @Override
133 protected boolean mustCheckName(DetailAST ast) {
134 return ast.findFirstToken(TokenTypes.IDENT) != null;
135 }
136
137 }