View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.naming;
21  
22  import com.puppycrawl.tools.checkstyle.api.DetailAST;
23  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
25  import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
26  
27  /**
28   * <p>
29   * Checks that local final variable names conform to a format specified
30   * by the format property. A catch parameter and resources in try statements
31   * are considered to be a local variables.The format is a
32   * {@link java.util.regex.Pattern regular expression} and defaults to
33   * <strong>^[a-z][a-zA-Z0-9]*$</strong>.
34   * </p>
35   * <p>
36   * An example of how to configure the check is:
37   * </p>
38   * <pre>
39   * &lt;module name="LocalFinalVariableName"/&gt;
40   * </pre>
41   * <p>
42   * An example of how to configure the check for names that are only upper case
43   * letters and digits is:
44   * </p>
45   * <pre>
46   * &lt;module name="LocalFinalVariableName"&gt;
47   *    &lt;property name="format" value="^[A-Z][A-Z0-9]*$"/&gt;
48   * &lt;/module&gt;
49   * </pre>
50   *
51   * @author Rick Giles
52   */
53  public class LocalFinalVariableNameCheck
54      extends AbstractNameCheck {
55      /** Creates a new {@code LocalFinalVariableNameCheck} instance. */
56      public LocalFinalVariableNameCheck() {
57          super("^[a-z][a-zA-Z0-9]*$");
58      }
59  
60      @Override
61      public int[] getDefaultTokens() {
62          return getAcceptableTokens();
63      }
64  
65      @Override
66      public int[] getAcceptableTokens() {
67          return new int[] {
68              TokenTypes.VARIABLE_DEF,
69              TokenTypes.PARAMETER_DEF,
70              TokenTypes.RESOURCE,
71          };
72      }
73  
74      @Override
75      public int[] getRequiredTokens() {
76          return CommonUtils.EMPTY_INT_ARRAY;
77      }
78  
79      @Override
80      protected final boolean mustCheckName(DetailAST ast) {
81          final DetailAST modifiersAST =
82              ast.findFirstToken(TokenTypes.MODIFIERS);
83          final boolean isFinal = ast.getType() == TokenTypes.RESOURCE
84              || modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
85          return isFinal && ScopeUtils.isLocalVariableDef(ast);
86      }
87  }