001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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 format specified
030 * by the format property. 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="http://checkstyle.sourceforge.net/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a>,
041 * <a href="http://checkstyle.sourceforge.net/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">PARAMETER_DEF</a>,
042 * <a href="http://checkstyle.sourceforge.net/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RESOURCE">RESOURCE</a>.
043 * </li>
044 * </ul>
045 * <p>
046 * An example of how to configure the check is:
047 * </p>
048 * <pre>
049 * &lt;module name="LocalFinalVariableName"/&gt;
050 * </pre>
051 * <p>
052 * An example of how to configure the check for names that are only upper case
053 * letters and digits is:
054 * </p>
055 * <pre>
056 * &lt;module name="LocalFinalVariableName"&gt;
057 *   &lt;property name="format" value="^[A-Z][A-Z0-9]*$"/&gt;
058 * &lt;/module&gt;
059 * </pre>
060 *
061 * @since 3.0
062 */
063public class LocalFinalVariableNameCheck
064    extends AbstractNameCheck {
065
066    /** Creates a new {@code LocalFinalVariableNameCheck} instance. */
067    public LocalFinalVariableNameCheck() {
068        super("^[a-z][a-zA-Z0-9]*$");
069    }
070
071    @Override
072    public int[] getDefaultTokens() {
073        return getAcceptableTokens();
074    }
075
076    @Override
077    public int[] getAcceptableTokens() {
078        return new int[] {
079            TokenTypes.VARIABLE_DEF,
080            TokenTypes.PARAMETER_DEF,
081            TokenTypes.RESOURCE,
082        };
083    }
084
085    @Override
086    public int[] getRequiredTokens() {
087        return CommonUtil.EMPTY_INT_ARRAY;
088    }
089
090    @Override
091    protected final boolean mustCheckName(DetailAST ast) {
092        final DetailAST modifiersAST =
093            ast.findFirstToken(TokenTypes.MODIFIERS);
094        final boolean isFinal = ast.getType() == TokenTypes.RESOURCE
095            || modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
096        return isFinal && ScopeUtil.isLocalVariableDef(ast);
097    }
098
099}