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.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24 import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
25
26 /**
27 * <div>
28 * Checks that local, non-{@code final} variable names conform to a specified pattern.
29 * A catch parameter is considered to be a local variable.
30 * </div>
31 *
32 * <p>
33 * This check does not support pattern variables. Instead, use
34 * <a href="https://checkstyle.org/checks/naming/patternvariablename.html">
35 * PatternVariableName</a>.
36 * </p>
37 *
38 * @since 3.0
39 */
40 public class LocalVariableNameCheck
41 extends AbstractNameCheck {
42
43 /**
44 * Allow one character variable name in
45 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html">
46 * initialization expressions</a>
47 * in FOR loop if one char variable name is prohibited by {@code format} regexp.
48 */
49 private boolean allowOneCharVarInForLoop;
50
51 /** Creates a new {@code LocalVariableNameCheck} instance. */
52 public LocalVariableNameCheck() {
53 super("^([a-z][a-zA-Z0-9]*|_)$");
54 }
55
56 /**
57 * Setter to allow one character variable name in
58 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html">
59 * initialization expressions</a>
60 * in FOR loop if one char variable name is prohibited by {@code format} regexp.
61 *
62 * @param allow Flag for allowing or not one character name in FOR loop.
63 * @since 5.8
64 */
65 public final void setAllowOneCharVarInForLoop(boolean allow) {
66 allowOneCharVarInForLoop = allow;
67 }
68
69 @Override
70 public int[] getDefaultTokens() {
71 return getRequiredTokens();
72 }
73
74 @Override
75 public int[] getAcceptableTokens() {
76 return getRequiredTokens();
77 }
78
79 @Override
80 public int[] getRequiredTokens() {
81 return new int[] {
82 TokenTypes.VARIABLE_DEF,
83 };
84 }
85
86 @Override
87 protected final boolean mustCheckName(DetailAST ast) {
88 final boolean result;
89 if (allowOneCharVarInForLoop && isForLoopVariable(ast)) {
90 final String variableName = ast.findFirstToken(TokenTypes.IDENT).getText();
91 result = variableName.length() != 1;
92 }
93 else {
94 final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS);
95 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
96 result = !isFinal && ScopeUtil.isLocalVariableDef(ast);
97 }
98 return result;
99 }
100
101 /**
102 * Checks if a variable is the loop's one.
103 *
104 * @param variableDef variable definition.
105 * @return true if a variable is the loop's one.
106 */
107 private static boolean isForLoopVariable(DetailAST variableDef) {
108 final int parentType = variableDef.getParent().getType();
109 return parentType == TokenTypes.FOR_INIT
110 || parentType == TokenTypes.FOR_EACH_CLAUSE;
111 }
112
113 }