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 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 * </p> 34 * <ul> 35 * <li> 36 * Property {@code applyToPackage} - Control if check should apply to package-private members. 37 * Type is {@code boolean}. 38 * Default value is {@code true}. 39 * </li> 40 * <li> 41 * Property {@code applyToPrivate} - Control if check should apply to private members. 42 * Type is {@code boolean}. 43 * Default value is {@code true}. 44 * </li> 45 * <li> 46 * Property {@code applyToProtected} - Control if check should apply to protected members. 47 * Type is {@code boolean}. 48 * Default value is {@code true}. 49 * </li> 50 * <li> 51 * Property {@code applyToPublic} - Control if check should apply to public members. 52 * Type is {@code boolean}. 53 * Default value is {@code true}. 54 * </li> 55 * <li> 56 * Property {@code format} - Sets the pattern to match valid identifiers. 57 * Type is {@code java.util.regex.Pattern}. 58 * Default value is {@code "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"}. 59 * </li> 60 * </ul> 61 * <p> 62 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 63 * </p> 64 * <p> 65 * Violation Message Keys: 66 * </p> 67 * <ul> 68 * <li> 69 * {@code name.invalidPattern} 70 * </li> 71 * </ul> 72 * 73 * @since 3.0 74 */ 75 public class ConstantNameCheck 76 extends AbstractAccessControlNameCheck { 77 78 /** Creates a new {@code ConstantNameCheck} instance. */ 79 public ConstantNameCheck() { 80 super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"); 81 } 82 83 @Override 84 public int[] getDefaultTokens() { 85 return getRequiredTokens(); 86 } 87 88 @Override 89 public int[] getAcceptableTokens() { 90 return getRequiredTokens(); 91 } 92 93 @Override 94 public int[] getRequiredTokens() { 95 return new int[] {TokenTypes.VARIABLE_DEF}; 96 } 97 98 @Override 99 protected final boolean mustCheckName(DetailAST ast) { 100 boolean returnValue = false; 101 102 final DetailAST modifiersAST = 103 ast.findFirstToken(TokenTypes.MODIFIERS); 104 final boolean isStaticFinal = 105 modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null 106 && modifiersAST.findFirstToken(TokenTypes.FINAL) != null 107 || ScopeUtil.isInAnnotationBlock(ast) 108 || ScopeUtil.isInInterfaceBlock(ast); 109 if (isStaticFinal && shouldCheckInScope(modifiersAST) 110 && !ScopeUtil.isInCodeBlock(ast)) { 111 // Handle the serialVersionUID and serialPersistentFields constants 112 // which are used for Serialization. Cannot enforce rules on it. :-) 113 final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT); 114 if (!"serialVersionUID".equals(nameAST.getText()) 115 && !"serialPersistentFields".equals(nameAST.getText())) { 116 returnValue = true; 117 } 118 } 119 120 return returnValue; 121 } 122 123 }