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   *
31   * @since 3.0
32   */
33  public class StaticVariableNameCheck
34      extends AbstractAccessControlNameCheck {
35  
36      /** Creates a new {@code StaticVariableNameCheck} instance. */
37      public StaticVariableNameCheck() {
38          super("^[a-z][a-zA-Z0-9]*$");
39      }
40  
41      @Override
42      public int[] getDefaultTokens() {
43          return getRequiredTokens();
44      }
45  
46      @Override
47      public int[] getAcceptableTokens() {
48          return getRequiredTokens();
49      }
50  
51      @Override
52      public int[] getRequiredTokens() {
53          return new int[] {TokenTypes.VARIABLE_DEF};
54      }
55  
56      /**
57       * Setter to control if check should apply to package-private members.
58       *
59       * @param applyTo new value of the property.
60       * @propertySince 5.0
61       */
62      @Override
63      public final void setApplyToPackage(boolean applyTo) {
64          super.setApplyToPackage(applyTo);
65      }
66  
67      /**
68       * Setter to control if check should apply to private members.
69       *
70       * @param applyTo new value of the property.
71       * @propertySince 5.0
72       */
73      @Override
74      public final void setApplyToPrivate(boolean applyTo) {
75          super.setApplyToPrivate(applyTo);
76      }
77  
78      /**
79       * Setter to control if check should apply to protected members.
80       *
81       * @param applyTo new value of the property.
82       * @propertySince 5.0
83       */
84      @Override
85      public final void setApplyToProtected(boolean applyTo) {
86          super.setApplyToProtected(applyTo);
87      }
88  
89      /**
90       * Setter to control if check should apply to public members.
91       *
92       * @param applyTo new value of the property.
93       * @propertySince 5.0
94       */
95      @Override
96      public final void setApplyToPublic(boolean applyTo) {
97          super.setApplyToPublic(applyTo);
98      }
99  
100     @Override
101     protected final boolean mustCheckName(DetailAST ast) {
102         final DetailAST modifiersAST =
103             ast.findFirstToken(TokenTypes.MODIFIERS);
104         final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
105         final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
106 
107         return isStatic
108                 && !isFinal
109                 && shouldCheckInScope(modifiersAST)
110                 && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast);
111     }
112 
113 }