001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2023 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 * <p> 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 * </p> 034 * <ul> 035 * <li> 036 * Property {@code applyToPackage} - Controls whether to apply the check to package-private member. 037 * Type is {@code boolean}. 038 * Default value is {@code true}. 039 * </li> 040 * <li> 041 * Property {@code applyToPrivate} - Controls whether to apply the check to private member. 042 * Type is {@code boolean}. 043 * Default value is {@code true}. 044 * </li> 045 * <li> 046 * Property {@code applyToProtected} - Controls whether to apply the check to protected member. 047 * Type is {@code boolean}. 048 * Default value is {@code true}. 049 * </li> 050 * <li> 051 * Property {@code applyToPublic} - Controls whether to apply the check to public member. 052 * Type is {@code boolean}. 053 * Default value is {@code true}. 054 * </li> 055 * <li> 056 * Property {@code format} - Specifies valid identifiers. 057 * Type is {@code java.util.regex.Pattern}. 058 * Default value is {@code "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"}. 059 * </li> 060 * </ul> 061 * <p> 062 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 063 * </p> 064 * <p> 065 * Violation Message Keys: 066 * </p> 067 * <ul> 068 * <li> 069 * {@code name.invalidPattern} 070 * </li> 071 * </ul> 072 * 073 * @since 3.0 074 */ 075public class ConstantNameCheck 076 extends AbstractAccessControlNameCheck { 077 078 /** Creates a new {@code ConstantNameCheck} instance. */ 079 public ConstantNameCheck() { 080 super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"); 081 } 082 083 @Override 084 public int[] getDefaultTokens() { 085 return getRequiredTokens(); 086 } 087 088 @Override 089 public int[] getAcceptableTokens() { 090 return getRequiredTokens(); 091 } 092 093 @Override 094 public int[] getRequiredTokens() { 095 return new int[] {TokenTypes.VARIABLE_DEF}; 096 } 097 098 @Override 099 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}