001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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.CommonUtil; 025import com.puppycrawl.tools.checkstyle.utils.ScopeUtil; 026 027/** 028 * <p> 029 * Checks that local final variable names conform to a format specified 030 * by the format property. A catch parameter and resources in try statements 031 * are considered to be a local, final variables. 032 * </p> 033 * <ul> 034 * <li> 035 * Property {@code format} - Specifies valid identifiers. Default value is 036 * {@code "^[a-z][a-zA-Z0-9]*$"}. 037 * </li> 038 * <li> 039 * Property {@code tokens} - tokens to check Default value is: 040 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a>, 041 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">PARAMETER_DEF</a>, 042 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RESOURCE">RESOURCE</a>. 043 * </li> 044 * </ul> 045 * <p> 046 * An example of how to configure the check is: 047 * </p> 048 * <pre> 049 * <module name="LocalFinalVariableName"/> 050 * </pre> 051 * <p> 052 * An example of how to configure the check for names that are only upper case 053 * letters and digits is: 054 * </p> 055 * <pre> 056 * <module name="LocalFinalVariableName"> 057 * <property name="format" value="^[A-Z][A-Z0-9]*$"/> 058 * </module> 059 * </pre> 060 * <p>Code Example:</p> 061 * <pre> 062 * class MyClass { 063 * void MyMethod() { 064 * try { 065 * final int VAR1 = 5; // OK 066 * final int var1 = 10; // violation, name 'var1' must match pattern "^[A-Z][A-Z0-9]*$" 067 * } catch (Exception ex) { 068 * final int VAR2 = 15; // OK 069 * final int var2 = 20; // violation, name 'var2' must match pattern "^[A-Z][A-Z0-9]*$" 070 * } 071 * } 072 * } 073 * </pre> 074 * <p> 075 * An example of how to configure the check for names of local final parameters and 076 * resources in try statements (without checks on variables): 077 * </p> 078 * <pre> 079 * <module name="LocalFinalVariableName"> 080 * <property name="format" value="^[A-Z][A-Z0-9]*$"/> 081 * <property name="tokens" value="PARAMETER_DEF,RESOURCE"> 082 * </module> 083 * </pre> 084 * <p>Code Example:</p> 085 * <pre> 086 * class MyClass { 087 * void MyMethod() { 088 * try(Scanner scanner = new Scanner()) { // violation, name 'scanner' must 089 * // match pattern '^[A-Z][A-Z0-9]*$' 090 * final int VAR1 = 5; // OK 091 * final int var1 = 10; // OK 092 * } catch (final Exception ex) { // violation, name 'ex' 093 * // must match pattern '^[A-Z][A-Z0-9]*$' 094 * final int VAR2 = 15; // OK 095 * final int var2 = 20; // OK 096 * } 097 * } 098 * } 099 * </pre> 100 * @since 3.0 101 */ 102public class LocalFinalVariableNameCheck 103 extends AbstractNameCheck { 104 105 /** Creates a new {@code LocalFinalVariableNameCheck} instance. */ 106 public LocalFinalVariableNameCheck() { 107 super("^[a-z][a-zA-Z0-9]*$"); 108 } 109 110 @Override 111 public int[] getDefaultTokens() { 112 return getAcceptableTokens(); 113 } 114 115 @Override 116 public int[] getAcceptableTokens() { 117 return new int[] { 118 TokenTypes.VARIABLE_DEF, 119 TokenTypes.PARAMETER_DEF, 120 TokenTypes.RESOURCE, 121 }; 122 } 123 124 @Override 125 public int[] getRequiredTokens() { 126 return CommonUtil.EMPTY_INT_ARRAY; 127 } 128 129 @Override 130 protected final boolean mustCheckName(DetailAST ast) { 131 final DetailAST modifiersAST = 132 ast.findFirstToken(TokenTypes.MODIFIERS); 133 final boolean isFinal = ast.getType() == TokenTypes.RESOURCE 134 || modifiersAST.findFirstToken(TokenTypes.FINAL) != null; 135 return isFinal && ScopeUtil.isLocalVariableDef(ast); 136 } 137 138}