001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2022 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 format} - Specifies valid identifiers. 037 * Type is {@code java.util.regex.Pattern}. 038 * Default value is {@code "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"}. 039 * </li> 040 * <li> 041 * Property {@code applyToPublic} - Controls whether to apply the check to public 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 applyToPackage} - Controls whether to apply the check to package-private member. 052 * Type is {@code boolean}. 053 * Default value is {@code true}. 054 * </li> 055 * <li> 056 * Property {@code applyToPrivate} - Controls whether to apply the check to private member. 057 * Type is {@code boolean}. 058 * Default value is {@code true}. 059 * </li> 060 * </ul> 061 * <p> 062 * To configure the check: 063 * </p> 064 * <pre> 065 * <module name="ConstantName"/> 066 * </pre> 067 * <p>Example:</p> 068 * <pre> 069 * class MyClass { 070 * public final static int FIRST_CONSTANT1 = 10; // OK 071 * protected final static int SECOND_CONSTANT2 = 100; // OK 072 * final static int third_Constant3 = 1000; // violation, name 'third_Constant3' must 073 * // match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' 074 * private final static int fourth_Const4 = 50; // violation, name 'fourth_Const4' must match 075 * // pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' 076 * } 077 * </pre> 078 * <p> 079 * The following configuration apart from names allowed by default allows {@code log} 080 * or {@code logger}: 081 * </p> 082 * <pre> 083 * <module name="ConstantName"> 084 * <property name="format" 085 * value="^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/> 086 * </module> 087 * </pre> 088 * <p>Code Example:</p> 089 * <pre> 090 * class MyClass { 091 * final static int log = 10; // OK 092 * final static int logger = 50; // OK 093 * final static int logMYSELF = 10; // violation, name 'logMYSELF' must match 094 * // pattern '^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' 095 * final static int loggerMYSELF = 5; // violation, name 'loggerMYSELF' must match 096 * // pattern '^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' 097 * final static int MYSELF = 100; // OK 098 * final static int myselfConstant = 1; // violation, name 'myselfConstant' must match pattern 099 * // '^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' 100 * } 101 * </pre> 102 * <p> 103 * The following configuration skip validation on 104 * public constant field and protected constant field. 105 * </p> 106 * <pre> 107 * <module name="ConstantName"> 108 * <property name="applyToPublic" value="false"/> 109 * <property name="applyToProtected" value="false"/> 110 * </module> 111 * </pre> 112 * <p>Code Example:</p> 113 * <pre> 114 * class MyClass { 115 * public final static int firstConstant = 10; // OK 116 * protected final static int secondConstant = 100; // OK 117 * final static int thirdConstant = 1000; // violation, name 'thirdConstant' must 118 * // match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' 119 * private final static int fourthConstant = 50; // violation, name 'fourthConstant' must match 120 * // pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' 121 * } 122 * </pre> 123 * <p> 124 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 125 * </p> 126 * <p> 127 * Violation Message Keys: 128 * </p> 129 * <ul> 130 * <li> 131 * {@code name.invalidPattern} 132 * </li> 133 * </ul> 134 * 135 * @since 3.0 136 */ 137public class ConstantNameCheck 138 extends AbstractAccessControlNameCheck { 139 140 /** Creates a new {@code ConstantNameCheck} instance. */ 141 public ConstantNameCheck() { 142 super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"); 143 } 144 145 @Override 146 public int[] getDefaultTokens() { 147 return getRequiredTokens(); 148 } 149 150 @Override 151 public int[] getAcceptableTokens() { 152 return getRequiredTokens(); 153 } 154 155 @Override 156 public int[] getRequiredTokens() { 157 return new int[] {TokenTypes.VARIABLE_DEF}; 158 } 159 160 @Override 161 protected final boolean mustCheckName(DetailAST ast) { 162 boolean returnValue = false; 163 164 final DetailAST modifiersAST = 165 ast.findFirstToken(TokenTypes.MODIFIERS); 166 final boolean isStaticFinal = 167 modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null 168 && modifiersAST.findFirstToken(TokenTypes.FINAL) != null 169 || ScopeUtil.isInAnnotationBlock(ast) 170 || ScopeUtil.isInInterfaceBlock(ast); 171 if (isStaticFinal && shouldCheckInScope(modifiersAST) 172 && !ScopeUtil.isInCodeBlock(ast)) { 173 // Handle the serialVersionUID and serialPersistentFields constants 174 // which are used for Serialization. Cannot enforce rules on it. :-) 175 final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT); 176 if (!"serialVersionUID".equals(nameAST.getText()) 177 && !"serialPersistentFields".equals(nameAST.getText())) { 178 returnValue = true; 179 } 180 } 181 182 return returnValue; 183 } 184 185}