001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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"> 035 * PatternVariableName</a>. 036 * </p> 037 * 038 * @since 3.0 039 */ 040public class LocalVariableNameCheck 041 extends AbstractNameCheck { 042 043 /** 044 * A key is pointing to the warning message text in "messages.properties" 045 * file. 046 */ 047 public static final String MSG_INVALID_PATTERN = "name.invalidPattern"; 048 049 /** 050 * Allow one character variable name in 051 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html"> 052 * initialization expressions</a> 053 * in FOR loop if one char variable name is prohibited by {@code format} regexp. 054 */ 055 private boolean allowOneCharVarInForLoop; 056 057 /** Creates a new {@code LocalVariableNameCheck} instance. */ 058 public LocalVariableNameCheck() { 059 super("^([a-z][a-zA-Z0-9]*|_)$", MSG_INVALID_PATTERN); 060 } 061 062 /** 063 * Setter to allow one character variable name in 064 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html"> 065 * initialization expressions</a> 066 * in FOR loop if one char variable name is prohibited by {@code format} regexp. 067 * 068 * @param allow Flag for allowing or not one character name in FOR loop. 069 * @since 5.8 070 */ 071 public final void setAllowOneCharVarInForLoop(boolean allow) { 072 allowOneCharVarInForLoop = allow; 073 } 074 075 @Override 076 public int[] getDefaultTokens() { 077 return getRequiredTokens(); 078 } 079 080 @Override 081 public int[] getAcceptableTokens() { 082 return getRequiredTokens(); 083 } 084 085 @Override 086 public int[] getRequiredTokens() { 087 return new int[] { 088 TokenTypes.VARIABLE_DEF, 089 }; 090 } 091 092 @Override 093 protected final boolean mustCheckName(DetailAST ast) { 094 final boolean result; 095 if (allowOneCharVarInForLoop && isForLoopVariable(ast)) { 096 final String variableName = ast.findFirstToken(TokenTypes.IDENT).getText(); 097 result = variableName.length() != 1; 098 } 099 else { 100 final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS); 101 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null; 102 result = !isFinal && ScopeUtil.isLocalVariableDef(ast); 103 } 104 return result; 105 } 106 107 /** 108 * Checks if a variable is the loop's one. 109 * 110 * @param variableDef variable definition. 111 * @return true if a variable is the loop's one. 112 */ 113 private static boolean isForLoopVariable(DetailAST variableDef) { 114 final int parentType = variableDef.getParent().getType(); 115 return parentType == TokenTypes.FOR_INIT 116 || parentType == TokenTypes.FOR_EACH_CLAUSE; 117 } 118 119}