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