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