001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.CommonUtils;
025import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
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 variables.The format is a
032 * {@link java.util.regex.Pattern regular expression} and defaults to
033 * <strong>^[a-z][a-zA-Z0-9]*$</strong>.
034 * </p>
035 * <p>
036 * An example of how to configure the check is:
037 * </p>
038 * <pre>
039 * &lt;module name="LocalFinalVariableName"/&gt;
040 * </pre>
041 * <p>
042 * An example of how to configure the check for names that are only upper case
043 * letters and digits is:
044 * </p>
045 * <pre>
046 * &lt;module name="LocalFinalVariableName"&gt;
047 *    &lt;property name="format" value="^[A-Z][A-Z0-9]*$"/&gt;
048 * &lt;/module&gt;
049 * </pre>
050 *
051 * @author Rick Giles
052 */
053public class LocalFinalVariableNameCheck
054    extends AbstractNameCheck {
055    /** Creates a new {@code LocalFinalVariableNameCheck} instance. */
056    public LocalFinalVariableNameCheck() {
057        super("^[a-z][a-zA-Z0-9]*$");
058    }
059
060    @Override
061    public int[] getDefaultTokens() {
062        return getAcceptableTokens();
063    }
064
065    @Override
066    public int[] getAcceptableTokens() {
067        return new int[] {
068            TokenTypes.VARIABLE_DEF,
069            TokenTypes.PARAMETER_DEF,
070            TokenTypes.RESOURCE,
071        };
072    }
073
074    @Override
075    public int[] getRequiredTokens() {
076        return CommonUtils.EMPTY_INT_ARRAY;
077    }
078
079    @Override
080    protected final boolean mustCheckName(DetailAST ast) {
081        final DetailAST modifiersAST =
082            ast.findFirstToken(TokenTypes.MODIFIERS);
083        final boolean isFinal = ast.getType() == TokenTypes.RESOURCE
084            || modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
085        return isFinal && ScopeUtils.isLocalVariableDef(ast);
086    }
087}