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