001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.naming; 021 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024import com.puppycrawl.tools.checkstyle.utils.ScopeUtil; 025 026/** 027 * <div> 028 * Checks that local, non-{@code final} variable names conform to a specified pattern. 029 * A catch parameter is considered to be a local variable. 030 * </div> 031 * 032 * <p> 033 * This check does not support pattern variables. Instead, use 034 * <a href="https://checkstyle.org/checks/naming/patternvariablename.html#PatternVariableName"> 035 * PatternVariableName</a>. 036 * </p> 037 * <ul> 038 * <li> 039 * Property {@code allowOneCharVarInForLoop} - Allow one character variable name in 040 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html"> 041 * initialization expressions</a> 042 * in FOR loop if one char variable name is prohibited by {@code format} regexp. 043 * Type is {@code boolean}. 044 * Default value is {@code false}. 045 * </li> 046 * <li> 047 * Property {@code format} - Sets the pattern to match valid identifiers. 048 * Type is {@code java.util.regex.Pattern}. 049 * Default value is {@code "^([a-z][a-zA-Z0-9]*|_)$"}. 050 * </li> 051 * </ul> 052 * 053 * <p> 054 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 055 * </p> 056 * 057 * <p> 058 * Violation Message Keys: 059 * </p> 060 * <ul> 061 * <li> 062 * {@code name.invalidPattern} 063 * </li> 064 * </ul> 065 * 066 * @since 3.0 067 */ 068public class LocalVariableNameCheck 069 extends AbstractNameCheck { 070 071 /** 072 * Allow one character variable name in 073 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html"> 074 * initialization expressions</a> 075 * in FOR loop if one char variable name is prohibited by {@code format} regexp. 076 */ 077 private boolean allowOneCharVarInForLoop; 078 079 /** Creates a new {@code LocalVariableNameCheck} instance. */ 080 public LocalVariableNameCheck() { 081 super("^([a-z][a-zA-Z0-9]*|_)$"); 082 } 083 084 /** 085 * Setter to allow one character variable name in 086 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html"> 087 * initialization expressions</a> 088 * in FOR loop if one char variable name is prohibited by {@code format} regexp. 089 * 090 * @param allow Flag for allowing or not one character name in FOR loop. 091 * @since 5.8 092 */ 093 public final void setAllowOneCharVarInForLoop(boolean allow) { 094 allowOneCharVarInForLoop = allow; 095 } 096 097 @Override 098 public int[] getDefaultTokens() { 099 return getRequiredTokens(); 100 } 101 102 @Override 103 public int[] getAcceptableTokens() { 104 return getRequiredTokens(); 105 } 106 107 @Override 108 public int[] getRequiredTokens() { 109 return new int[] { 110 TokenTypes.VARIABLE_DEF, 111 }; 112 } 113 114 @Override 115 protected final boolean mustCheckName(DetailAST ast) { 116 final boolean result; 117 if (allowOneCharVarInForLoop && isForLoopVariable(ast)) { 118 final String variableName = ast.findFirstToken(TokenTypes.IDENT).getText(); 119 result = variableName.length() != 1; 120 } 121 else { 122 final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS); 123 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null; 124 result = !isFinal && ScopeUtil.isLocalVariableDef(ast); 125 } 126 return result; 127 } 128 129 /** 130 * Checks if a variable is the loop's one. 131 * 132 * @param variableDef variable definition. 133 * @return true if a variable is the loop's one. 134 */ 135 private static boolean isForLoopVariable(DetailAST variableDef) { 136 final int parentType = variableDef.getParent().getType(); 137 return parentType == TokenTypes.FOR_INIT 138 || parentType == TokenTypes.FOR_EACH_CLAUSE; 139 } 140 141}