001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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 java.util.regex.Pattern; 023 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.utils.ScopeUtils; 027 028/** 029 * <p> 030 * Checks that local, non-final variable names conform to a format specified 031 * by the format property. A catch parameter is considered to be 032 * a local variable. The format is a 033 * {@link Pattern regular expression} 034 * and defaults to 035 * <strong>^[a-z][a-zA-Z0-9]*$</strong>. 036 * </p> 037 * <p> 038 * An example of how to configure the check is: 039 * </p> 040 * <pre> 041 * <module name="LocalVariableName"/> 042 * </pre> 043 * <p> 044 * An example of how to configure the check for names that begin with a lower 045 * case letter, followed by letters, digits, and underscores is: 046 * </p> 047 * <pre> 048 * <module name="LocalVariableName"> 049 * <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/> 050 * </module> 051 * </pre> 052 * <p> 053 * An example of one character variable name in 054 * initialization expression(like "i") in FOR loop: 055 * </p> 056 * <pre> 057 * for(int i = 1; i < 10; i++) {} 058 * </pre> 059 * <p> 060 * An example of how to configure the check to allow one char variable name in 061 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html"> 062 * initialization expressions</a> in FOR loop: 063 * </p> 064 * <pre> 065 * <module name="LocalVariableName"> 066 * <property name="allowOneCharVarInForLoop" value="true"/> 067 * </module> 068 * </pre> 069 * 070 */ 071public class LocalVariableNameCheck 072 extends AbstractNameCheck { 073 074 /** Regexp for one-char loop variables. */ 075 private static final Pattern SINGLE_CHAR = Pattern.compile("^[a-z]$"); 076 077 /** 078 * Allow one character name for initialization expression in FOR loop. 079 */ 080 private boolean allowOneCharVarInForLoop; 081 082 /** Creates a new {@code LocalVariableNameCheck} instance. */ 083 public LocalVariableNameCheck() { 084 super("^[a-z][a-zA-Z0-9]*$"); 085 } 086 087 /** 088 * Sets whether to allow one character name in FOR loop or not. 089 * 090 * @param allow Flag for allowing or not one character name in FOR loop. 091 */ 092 public final void setAllowOneCharVarInForLoop(boolean allow) { 093 allowOneCharVarInForLoop = allow; 094 } 095 096 @Override 097 public int[] getDefaultTokens() { 098 return getRequiredTokens(); 099 } 100 101 @Override 102 public int[] getAcceptableTokens() { 103 return getRequiredTokens(); 104 } 105 106 @Override 107 public int[] getRequiredTokens() { 108 return new int[] { 109 TokenTypes.VARIABLE_DEF, 110 }; 111 } 112 113 @Override 114 protected final boolean mustCheckName(DetailAST ast) { 115 final boolean result; 116 if (allowOneCharVarInForLoop && isForLoopVariable(ast)) { 117 final String variableName = ast.findFirstToken(TokenTypes.IDENT).getText(); 118 result = !SINGLE_CHAR.matcher(variableName).find(); 119 } 120 else { 121 final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS); 122 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null; 123 result = !isFinal && ScopeUtils.isLocalVariableDef(ast); 124 } 125 return result; 126 } 127 128 /** 129 * Checks if a variable is the loop's one. 130 * @param variableDef variable definition. 131 * @return true if a variable is the loop's one. 132 */ 133 private static boolean isForLoopVariable(DetailAST variableDef) { 134 final int parentType = variableDef.getParent().getType(); 135 return parentType == TokenTypes.FOR_INIT 136 || parentType == TokenTypes.FOR_EACH_CLAUSE; 137 } 138 139}