001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 * </li> 041 * <li> 042 * Property {@code applyToPrivate} - Control if check should apply to private members. 043 * Type is {@code boolean}. 044 * Default value is {@code true}. 045 * </li> 046 * <li> 047 * Property {@code applyToProtected} - Control if check should apply to protected members. 048 * Type is {@code boolean}. 049 * Default value is {@code true}. 050 * </li> 051 * <li> 052 * Property {@code applyToPublic} - Control if check should apply to public members. 053 * Type is {@code boolean}. 054 * Default value is {@code true}. 055 * </li> 056 * <li> 057 * Property {@code format} - Sets the pattern to match valid identifiers. 058 * Type is {@code java.util.regex.Pattern}. 059 * Default value is {@code "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"}. 060 * </li> 061 * </ul> 062 * 063 * <p> 064 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 065 * </p> 066 * 067 * <p> 068 * Violation Message Keys: 069 * </p> 070 * <ul> 071 * <li> 072 * {@code name.invalidPattern} 073 * </li> 074 * </ul> 075 * 076 * @since 3.0 077 */ 078public class ConstantNameCheck 079 extends AbstractAccessControlNameCheck { 080 081 /** Creates a new {@code ConstantNameCheck} instance. */ 082 public ConstantNameCheck() { 083 super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"); 084 } 085 086 @Override 087 public int[] getDefaultTokens() { 088 return getRequiredTokens(); 089 } 090 091 @Override 092 public int[] getAcceptableTokens() { 093 return getRequiredTokens(); 094 } 095 096 @Override 097 public int[] getRequiredTokens() { 098 return new int[] {TokenTypes.VARIABLE_DEF}; 099 } 100 101 @Override 102 protected final boolean mustCheckName(DetailAST ast) { 103 boolean returnValue = false; 104 105 final DetailAST modifiersAST = 106 ast.findFirstToken(TokenTypes.MODIFIERS); 107 final boolean isStaticFinal = 108 modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null 109 && modifiersAST.findFirstToken(TokenTypes.FINAL) != null 110 || ScopeUtil.isInAnnotationBlock(ast) 111 || ScopeUtil.isInInterfaceBlock(ast); 112 if (isStaticFinal && shouldCheckInScope(modifiersAST) 113 && !ScopeUtil.isInCodeBlock(ast)) { 114 // Handle the serialVersionUID and serialPersistentFields constants 115 // which are used for Serialization. Cannot enforce rules on it. :-) 116 final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT); 117 if (!"serialVersionUID".equals(nameAST.getText()) 118 && !"serialPersistentFields".equals(nameAST.getText())) { 119 returnValue = true; 120 } 121 } 122 123 return returnValue; 124 } 125 126}