View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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 constant names conform to a specified pattern.
29   * A <em>constant</em> is a <strong>static</strong> and <strong>final</strong>
30   * field or an interface/annotation field, except
31   * <strong>serialVersionUID</strong> and <strong>serialPersistentFields
32   * </strong>.
33   * </div>
34   *
35   * @since 3.0
36   */
37  public class ConstantNameCheck
38      extends AbstractAccessControlNameCheck {
39  
40      /**
41       * A key is pointing to the warning message text in "messages.properties"
42       * file.
43       */
44      public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
45  
46      /** Creates a new {@code ConstantNameCheck} instance. */
47      public ConstantNameCheck() {
48          super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$", MSG_INVALID_PATTERN);
49      }
50  
51      @Override
52      public int[] getDefaultTokens() {
53          return getRequiredTokens();
54      }
55  
56      @Override
57      public int[] getAcceptableTokens() {
58          return getRequiredTokens();
59      }
60  
61      @Override
62      public int[] getRequiredTokens() {
63          return new int[] {TokenTypes.VARIABLE_DEF};
64      }
65  
66      /**
67       * Setter to control if check should apply to package-private members.
68       *
69       * @param applyTo new value of the property.
70       * @propertySince 5.0
71       */
72      @Override
73      public final void setApplyToPackage(boolean applyTo) {
74          super.setApplyToPackage(applyTo);
75      }
76  
77      /**
78       * Setter to control if check should apply to private members.
79       *
80       * @param applyTo new value of the property.
81       * @propertySince 5.0
82       */
83      @Override
84      public final void setApplyToPrivate(boolean applyTo) {
85          super.setApplyToPrivate(applyTo);
86      }
87  
88      /**
89       * Setter to control if check should apply to protected members.
90       *
91       * @param applyTo new value of the property.
92       * @propertySince 5.0
93       */
94      @Override
95      public final void setApplyToProtected(boolean applyTo) {
96          super.setApplyToProtected(applyTo);
97      }
98  
99      /**
100      * Setter to control if check should apply to public members.
101      *
102      * @param applyTo new value of the property.
103      * @propertySince 5.0
104      */
105     @Override
106     public final void setApplyToPublic(boolean applyTo) {
107         super.setApplyToPublic(applyTo);
108     }
109 
110     @Override
111     protected final boolean mustCheckName(DetailAST ast) {
112         boolean returnValue = false;
113 
114         final DetailAST modifiersAST =
115             ast.findFirstToken(TokenTypes.MODIFIERS);
116         final boolean isStaticFinal =
117             modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null
118                 && modifiersAST.findFirstToken(TokenTypes.FINAL) != null
119             || ScopeUtil.isInAnnotationBlock(ast)
120             || ScopeUtil.isInInterfaceBlock(ast);
121         if (isStaticFinal && shouldCheckInScope(modifiersAST)
122                         && !ScopeUtil.isInCodeBlock(ast)) {
123             // Handle the serialVersionUID and serialPersistentFields constants
124             // which are used for Serialization. Cannot enforce rules on it. :-)
125             final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
126             if (!"serialVersionUID".equals(nameAST.getText())
127                 && !"serialPersistentFields".equals(nameAST.getText())) {
128                 returnValue = true;
129             }
130         }
131 
132         return returnValue;
133     }
134 
135 }