001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks.naming; 020 021import com.puppycrawl.tools.checkstyle.api.DetailAST; 022import com.puppycrawl.tools.checkstyle.api.ScopeUtils; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024 025/** 026 * <p> 027 * Checks that local final variable names conform to a format specified 028 * by the format property. A catch parameter is considered to be 029 * a local variable.The format is a 030 * {@link java.util.regex.Pattern regular expression} and defaults to 031 * <strong>^[a-z][a-zA-Z0-9]*$</strong>. 032 * </p> 033 * <p> 034 * An example of how to configure the check is: 035 * </p> 036 * <pre> 037 * <module name="LocalFinalVariableName"/> 038 * </pre> 039 * <p> 040 * An example of how to configure the check for names that are only upper case 041 * letters and digits is: 042 * </p> 043 * <pre> 044 * <module name="LocalFinalVariableName"> 045 * <property name="format" value="^[A-Z][A-Z0-9]*$"/> 046 * </module> 047 * </pre> 048 * 049 * @author Rick Giles 050 * @version 1.0 051 */ 052public class LocalFinalVariableNameCheck 053 extends AbstractNameCheck 054{ 055 /** Creates a new <code>LocalFinalVariableNameCheck</code> instance. */ 056 public LocalFinalVariableNameCheck() 057 { 058 super("^[a-z][a-zA-Z0-9]*$"); 059 } 060 061 @Override 062 public int[] getDefaultTokens() 063 { 064 return new int[] { 065 TokenTypes.VARIABLE_DEF, 066 TokenTypes.PARAMETER_DEF, 067 }; 068 } 069 070 @Override 071 protected final boolean mustCheckName(DetailAST ast) 072 { 073 final DetailAST modifiersAST = 074 ast.findFirstToken(TokenTypes.MODIFIERS); 075 final boolean isFinal = (modifiersAST != null) 076 && modifiersAST.branchContains(TokenTypes.FINAL); 077 return (isFinal && ScopeUtils.isLocalVariableDef(ast)); 078 } 079}