001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.naming; 021 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024import com.puppycrawl.tools.checkstyle.utils.ScopeUtil; 025 026/** 027 * <div> 028 * Checks that constant names conform to a specified pattern. 029 * A <em>constant</em> is a <strong>static</strong> and <strong>final</strong> 030 * field or an interface/annotation field, except 031 * <strong>serialVersionUID</strong> and <strong>serialPersistentFields 032 * </strong>. 033 * </div> 034 * 035 * <ul> 036 * <li> 037 * Property {@code applyToPackage} - Control if check should apply to package-private members. 038 * Type is {@code boolean}. 039 * Default value is {@code true}. 040 * Since version 5.0 041 * </li> 042 * <li> 043 * Property {@code applyToPrivate} - Control if check should apply to private members. 044 * Type is {@code boolean}. 045 * Default value is {@code true}. 046 * Since version 5.0 047 * </li> 048 * <li> 049 * Property {@code applyToProtected} - Control if check should apply to protected members. 050 * Type is {@code boolean}. 051 * Default value is {@code true}. 052 * Since version 5.0 053 * </li> 054 * <li> 055 * Property {@code applyToPublic} - Control if check should apply to public members. 056 * Type is {@code boolean}. 057 * Default value is {@code true}. 058 * Since version 5.0 059 * </li> 060 * <li> 061 * Property {@code format} - Sets the pattern to match valid identifiers. 062 * Type is {@code java.util.regex.Pattern}. 063 * Default value is {@code "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"}. 064 * </li> 065 * </ul> 066 * 067 * <p> 068 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 069 * </p> 070 * 071 * <p> 072 * Violation Message Keys: 073 * </p> 074 * <ul> 075 * <li> 076 * {@code name.invalidPattern} 077 * </li> 078 * </ul> 079 * 080 * @since 3.0 081 */ 082public class ConstantNameCheck 083 extends AbstractAccessControlNameCheck { 084 085 /** Creates a new {@code ConstantNameCheck} instance. */ 086 public ConstantNameCheck() { 087 super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"); 088 } 089 090 @Override 091 public int[] getDefaultTokens() { 092 return getRequiredTokens(); 093 } 094 095 @Override 096 public int[] getAcceptableTokens() { 097 return getRequiredTokens(); 098 } 099 100 @Override 101 public int[] getRequiredTokens() { 102 return new int[] {TokenTypes.VARIABLE_DEF}; 103 } 104 105 @Override 106 protected final boolean mustCheckName(DetailAST ast) { 107 boolean returnValue = false; 108 109 final DetailAST modifiersAST = 110 ast.findFirstToken(TokenTypes.MODIFIERS); 111 final boolean isStaticFinal = 112 modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null 113 && modifiersAST.findFirstToken(TokenTypes.FINAL) != null 114 || ScopeUtil.isInAnnotationBlock(ast) 115 || ScopeUtil.isInInterfaceBlock(ast); 116 if (isStaticFinal && shouldCheckInScope(modifiersAST) 117 && !ScopeUtil.isInCodeBlock(ast)) { 118 // Handle the serialVersionUID and serialPersistentFields constants 119 // which are used for Serialization. Cannot enforce rules on it. :-) 120 final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT); 121 if (!"serialVersionUID".equals(nameAST.getText()) 122 && !"serialPersistentFields".equals(nameAST.getText())) { 123 returnValue = true; 124 } 125 } 126 127 return returnValue; 128 } 129 130}