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