001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 * @since 3.0 036 */ 037public class ConstantNameCheck extends AbstractAccessControlNameCheck { 038 039 /** 040 * A key is pointing to the warning message text in "messages.properties" file. 041 */ 042 public static final String MSG_INVALID_PATTERN = "name.invalidPattern"; 043 044 /** Creates a new {@code ConstantNameCheck} instance. */ 045 public ConstantNameCheck() { 046 super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$", MSG_INVALID_PATTERN); 047 } 048 049 @Override 050 public int[] getDefaultTokens() { 051 return getRequiredTokens(); 052 } 053 054 @Override 055 public int[] getAcceptableTokens() { 056 return getRequiredTokens(); 057 } 058 059 @Override 060 public int[] getRequiredTokens() { 061 return new int[] {TokenTypes.VARIABLE_DEF}; 062 } 063 064 /** 065 * Setter to control if check should apply to package-private members. 066 * 067 * @param applyTo new value of the property. 068 * @propertySince 5.0 069 */ 070 @Override 071 public final void setApplyToPackage(boolean applyTo) { 072 super.setApplyToPackage(applyTo); 073 } 074 075 /** 076 * Setter to control if check should apply to private members. 077 * 078 * @param applyTo new value of the property. 079 * @propertySince 5.0 080 */ 081 @Override 082 public final void setApplyToPrivate(boolean applyTo) { 083 super.setApplyToPrivate(applyTo); 084 } 085 086 /** 087 * Setter to control if check should apply to protected members. 088 * 089 * @param applyTo new value of the property. 090 * @propertySince 5.0 091 */ 092 @Override 093 public final void setApplyToProtected(boolean applyTo) { 094 super.setApplyToProtected(applyTo); 095 } 096 097 /** 098 * Setter to control if check should apply to public members. 099 * 100 * @param applyTo new value of the property. 101 * @propertySince 5.0 102 */ 103 @Override 104 public final void setApplyToPublic(boolean applyTo) { 105 super.setApplyToPublic(applyTo); 106 } 107 108 @Override 109 protected final boolean mustCheckName(DetailAST ast) { 110 boolean returnValue = false; 111 112 final DetailAST modifiersAST = 113 ast.findFirstToken(TokenTypes.MODIFIERS); 114 final boolean isStaticFinal = 115 modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null 116 && modifiersAST.findFirstToken(TokenTypes.FINAL) != null 117 || ScopeUtil.isInAnnotationBlock(ast) 118 || ScopeUtil.isInInterfaceBlock(ast); 119 if (isStaticFinal && shouldCheckInScope(modifiersAST) 120 && !ScopeUtil.isInCodeBlock(ast)) { 121 // Handle the serialVersionUID and serialPersistentFields constants 122 // which are used for Serialization. Cannot enforce rules on it. :-) 123 final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT); 124 if (!"serialVersionUID".equals(nameAST.getText()) 125 && !"serialPersistentFields".equals(nameAST.getText())) { 126 returnValue = true; 127 } 128 } 129 130 return returnValue; 131 } 132 133}