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.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
30 * a local variable.
31 * </div>
32 *
33 * <p>
34 * This check does not support 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 allowOneCharVarInForLoop} - Allow one character variable name in
41 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html">
42 * initialization expressions</a>
43 * in FOR loop if one char variable name is prohibited by {@code format} regexp.
44 * Type is {@code boolean}.
45 * Default value is {@code false}.
46 * </li>
47 * <li>
48 * Property {@code format} - Sets the pattern to match valid identifiers.
49 * Type is {@code java.util.regex.Pattern}.
50 * Default value is {@code "^([a-z][a-zA-Z0-9]*|_)$"}.
51 * </li>
52 * </ul>
53 *
54 * <p>
55 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
56 * </p>
57 *
58 * <p>
59 * Violation Message Keys:
60 * </p>
61 * <ul>
62 * <li>
63 * {@code name.invalidPattern}
64 * </li>
65 * </ul>
66 *
67 * @since 3.0
68 */
69 public class LocalVariableNameCheck
70 extends AbstractNameCheck {
71
72 /**
73 * Allow one character variable name in
74 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html">
75 * initialization expressions</a>
76 * in FOR loop if one char variable name is prohibited by {@code format} regexp.
77 */
78 private boolean allowOneCharVarInForLoop;
79
80 /** Creates a new {@code LocalVariableNameCheck} instance. */
81 public LocalVariableNameCheck() {
82 super("^([a-z][a-zA-Z0-9]*|_)$");
83 }
84
85 /**
86 * Setter to allow one character variable name in
87 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html">
88 * initialization expressions</a>
89 * in FOR loop if one char variable name is prohibited by {@code format} regexp.
90 *
91 * @param allow Flag for allowing or not one character name in FOR loop.
92 * @since 5.8
93 */
94 public final void setAllowOneCharVarInForLoop(boolean allow) {
95 allowOneCharVarInForLoop = allow;
96 }
97
98 @Override
99 public int[] getDefaultTokens() {
100 return getRequiredTokens();
101 }
102
103 @Override
104 public int[] getAcceptableTokens() {
105 return getRequiredTokens();
106 }
107
108 @Override
109 public int[] getRequiredTokens() {
110 return new int[] {
111 TokenTypes.VARIABLE_DEF,
112 };
113 }
114
115 @Override
116 protected final boolean mustCheckName(DetailAST ast) {
117 final boolean result;
118 if (allowOneCharVarInForLoop && isForLoopVariable(ast)) {
119 final String variableName = ast.findFirstToken(TokenTypes.IDENT).getText();
120 result = variableName.length() != 1;
121 }
122 else {
123 final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS);
124 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
125 result = !isFinal && ScopeUtil.isLocalVariableDef(ast);
126 }
127 return result;
128 }
129
130 /**
131 * Checks if a variable is the loop's one.
132 *
133 * @param variableDef variable definition.
134 * @return true if a variable is the loop's one.
135 */
136 private static boolean isForLoopVariable(DetailAST variableDef) {
137 final int parentType = variableDef.getParent().getType();
138 return parentType == TokenTypes.FOR_INIT
139 || parentType == TokenTypes.FOR_EACH_CLAUSE;
140 }
141
142 }