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.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
25 import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
26
27 /**
28 * <p>
29 * Checks that local final variable names conform to a specified pattern.
30 * A catch parameter and resources in try statements
31 * are considered to be a local, final variables.
32 * </p>
33 * <p>
34 * This check does not support final pattern variables. Instead, use
35 * <a href="https://checkstyle.org/checks/naming/patternvariablename.html#PatternVariableName">
36 * PatternVariableName</a>.
37 * </p>
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 "^([a-z][a-zA-Z0-9]*|_)$"}.
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#VARIABLE_DEF">
50 * VARIABLE_DEF</a>,
51 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">
52 * PARAMETER_DEF</a>,
53 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RESOURCE">
54 * RESOURCE</a>.
55 * </li>
56 * </ul>
57 * <p>
58 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
59 * </p>
60 * <p>
61 * Violation Message Keys:
62 * </p>
63 * <ul>
64 * <li>
65 * {@code name.invalidPattern}
66 * </li>
67 * </ul>
68 *
69 * @since 3.0
70 */
71 public class LocalFinalVariableNameCheck
72 extends AbstractNameCheck {
73
74 /** Creates a new {@code LocalFinalVariableNameCheck} instance. */
75 public LocalFinalVariableNameCheck() {
76 super("^([a-z][a-zA-Z0-9]*|_)$");
77 }
78
79 @Override
80 public int[] getDefaultTokens() {
81 return getAcceptableTokens();
82 }
83
84 @Override
85 public int[] getAcceptableTokens() {
86 return new int[] {
87 TokenTypes.VARIABLE_DEF,
88 TokenTypes.PARAMETER_DEF,
89 TokenTypes.RESOURCE,
90 };
91 }
92
93 @Override
94 public int[] getRequiredTokens() {
95 return CommonUtil.EMPTY_INT_ARRAY;
96 }
97
98 @Override
99 protected final boolean mustCheckName(DetailAST ast) {
100 final DetailAST modifiersAST =
101 ast.findFirstToken(TokenTypes.MODIFIERS);
102 final boolean isFinal = ast.getType() == TokenTypes.RESOURCE
103 || modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
104 return isFinal && ScopeUtil.isLocalVariableDef(ast);
105 }
106
107 }