View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.ScopeUtils;
25  
26  /**
27   * <p>
28   * Checks that constant names conform to a format specified
29   * by the format property.
30   * A <em>constant</em> is a <strong>static</strong> and <strong>final</strong>
31   * field or an interface/annotation field, except
32   * <strong>serialVersionUID</strong> and <strong>serialPersistentFields
33   * </strong>.  The format is a regular expression
34   * and defaults to <strong>^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$</strong>.
35   * </p>
36   * <p>
37   * An example of how to configure the check is:
38   * </p>
39   * <pre>
40   * &lt;module name="ConstantName"/&gt;
41   * </pre>
42   *
43   * <p>
44   * An example of how to configure the check for names that are only upper case
45   * letters and digits is:
46   * </p>
47   * <pre>
48   * &lt;module name="ConstantName"&gt;
49   *    &lt;property name="format" value="^[A-Z][A-Z0-9]*$"/&gt;
50   * &lt;/module&gt;
51   * </pre>
52   *
53   *
54   * @author Rick Giles
55   */
56  public class ConstantNameCheck
57      extends AbstractAccessControlNameCheck {
58  
59      /** Creates a new {@code ConstantNameCheck} instance. */
60      public ConstantNameCheck() {
61          super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$");
62      }
63  
64      @Override
65      public int[] getDefaultTokens() {
66          return getRequiredTokens();
67      }
68  
69      @Override
70      public int[] getAcceptableTokens() {
71          return getRequiredTokens();
72      }
73  
74      @Override
75      public int[] getRequiredTokens() {
76          return new int[] {TokenTypes.VARIABLE_DEF};
77      }
78  
79      @Override
80      protected final boolean mustCheckName(DetailAST ast) {
81          boolean returnValue = false;
82  
83          final DetailAST modifiersAST =
84              ast.findFirstToken(TokenTypes.MODIFIERS);
85          final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
86          final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
87  
88          if (isStatic && isFinal && shouldCheckInScope(modifiersAST)
89                  || ScopeUtils.isInAnnotationBlock(ast)
90                  || ScopeUtils.isInInterfaceOrAnnotationBlock(ast)
91                          && !ScopeUtils.isInCodeBlock(ast)) {
92              // Handle the serialVersionUID and serialPersistentFields constants
93              // which are used for Serialization. Cannot enforce rules on it. :-)
94              final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
95              if (!"serialVersionUID".equals(nameAST.getText())
96                  && !"serialPersistentFields".equals(nameAST.getText())) {
97                  returnValue = true;
98              }
99          }
100 
101         return returnValue;
102     }
103 
104 }