001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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} - Sets the pattern to match 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 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
054 * </p>
055 * <p>
056 * Violation Message Keys:
057 * </p>
058 * <ul>
059 * <li>
060 * {@code name.invalidPattern}
061 * </li>
062 * </ul>
063 *
064 * @since 3.0
065 */
066public class LocalFinalVariableNameCheck
067    extends AbstractNameCheck {
068
069    /** Creates a new {@code LocalFinalVariableNameCheck} instance. */
070    public LocalFinalVariableNameCheck() {
071        super("^[a-z][a-zA-Z0-9]*$");
072    }
073
074    @Override
075    public int[] getDefaultTokens() {
076        return getAcceptableTokens();
077    }
078
079    @Override
080    public int[] getAcceptableTokens() {
081        return new int[] {
082            TokenTypes.VARIABLE_DEF,
083            TokenTypes.PARAMETER_DEF,
084            TokenTypes.RESOURCE,
085        };
086    }
087
088    @Override
089    public int[] getRequiredTokens() {
090        return CommonUtil.EMPTY_INT_ARRAY;
091    }
092
093    @Override
094    protected final boolean mustCheckName(DetailAST ast) {
095        final DetailAST modifiersAST =
096            ast.findFirstToken(TokenTypes.MODIFIERS);
097        final boolean isFinal = ast.getType() == TokenTypes.RESOURCE
098            || modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
099        return isFinal && ScopeUtil.isLocalVariableDef(ast);
100    }
101
102}