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