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.ScopeUtil;
25  
26  /**
27   * <p>
28   * Checks that {@code static}, non-{@code final} variable names conform to a specified pattern.
29   * </p>
30   * <ul>
31   * <li>
32   * Property {@code applyToPackage} - Control if check should apply to package-private
33   *   members.
34   * Type is {@code boolean}.
35   * Default value is {@code true}.
36   * </li>
37   * <li>
38   * Property {@code applyToPrivate} - Control if check should apply to private members.
39   * Type is {@code boolean}.
40   * Default value is {@code true}.
41   * </li>
42   * <li>
43   * Property {@code applyToProtected} - Control if check should apply to protected
44   *   members.
45   * Type is {@code boolean}.
46   * Default value is {@code true}.
47   * </li>
48   * <li>
49   * Property {@code applyToPublic} - Control if check should apply to public members.
50   * Type is {@code boolean}.
51   * Default value is {@code true}.
52   * </li>
53   * <li>
54   * Property {@code format} - Sets the pattern to match valid identifiers.
55   * Type is {@code java.util.regex.Pattern}.
56   * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}.
57   * </li>
58   * </ul>
59   * <p>
60   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
61   * </p>
62   * <p>
63   * Violation Message Keys:
64   * </p>
65   * <ul>
66   * <li>
67   * {@code name.invalidPattern}
68   * </li>
69   * </ul>
70   *
71   * @since 3.0
72   */
73  public class StaticVariableNameCheck
74      extends AbstractAccessControlNameCheck {
75  
76      /** Creates a new {@code StaticVariableNameCheck} instance. */
77      public StaticVariableNameCheck() {
78          super("^[a-z][a-zA-Z0-9]*$");
79      }
80  
81      @Override
82      public int[] getDefaultTokens() {
83          return getRequiredTokens();
84      }
85  
86      @Override
87      public int[] getAcceptableTokens() {
88          return getRequiredTokens();
89      }
90  
91      @Override
92      public int[] getRequiredTokens() {
93          return new int[] {TokenTypes.VARIABLE_DEF};
94      }
95  
96      @Override
97      protected final boolean mustCheckName(DetailAST ast) {
98          final DetailAST modifiersAST =
99              ast.findFirstToken(TokenTypes.MODIFIERS);
100         final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
101         final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
102 
103         return isStatic
104                 && !isFinal
105                 && shouldCheckInScope(modifiersAST)
106                 && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast);
107     }
108 
109 }