View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.CommonUtil;
25  import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
26  
27  /**
28   * <p>
29   * Checks that local final variable names conform to a specified pattern.
30   *  A catch parameter and resources in try statements
31   * are considered to be a local, final variables.
32   * </p>
33   * <ul>
34   * <li>
35   * Property {@code format} - Sets the pattern to match valid identifiers.
36   * Type is {@code java.util.regex.Pattern}.
37   * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}.
38   * </li>
39   * <li>
40   * Property {@code tokens} - tokens to check
41   * Type is {@code java.lang.String[]}.
42   * Validation type is {@code tokenSet}.
43   * Default value is:
44   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">
45   * VARIABLE_DEF</a>,
46   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">
47   * PARAMETER_DEF</a>,
48   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RESOURCE">
49   * RESOURCE</a>.
50   * </li>
51   * </ul>
52   * <p>
53   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
54   * </p>
55   * <p>
56   * Violation Message Keys:
57   * </p>
58   * <ul>
59   * <li>
60   * {@code name.invalidPattern}
61   * </li>
62   * </ul>
63   *
64   * @since 3.0
65   */
66  public class LocalFinalVariableNameCheck
67      extends AbstractNameCheck {
68  
69      /** Creates a new {@code LocalFinalVariableNameCheck} instance. */
70      public LocalFinalVariableNameCheck() {
71          super("^[a-z][a-zA-Z0-9]*$");
72      }
73  
74      @Override
75      public int[] getDefaultTokens() {
76          return getAcceptableTokens();
77      }
78  
79      @Override
80      public int[] getAcceptableTokens() {
81          return new int[] {
82              TokenTypes.VARIABLE_DEF,
83              TokenTypes.PARAMETER_DEF,
84              TokenTypes.RESOURCE,
85          };
86      }
87  
88      @Override
89      public int[] getRequiredTokens() {
90          return CommonUtil.EMPTY_INT_ARRAY;
91      }
92  
93      @Override
94      protected final boolean mustCheckName(DetailAST ast) {
95          final DetailAST modifiersAST =
96              ast.findFirstToken(TokenTypes.MODIFIERS);
97          final boolean isFinal = ast.getType() == TokenTypes.RESOURCE
98              || modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
99          return isFinal && ScopeUtil.isLocalVariableDef(ast);
100     }
101 
102 }