001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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 format specified 029 * by the format property. 030 * A <em>constant</em> is a <strong>static</strong> and <strong>final</strong> 031 * field or an interface/annotation field, except 032 * <strong>serialVersionUID</strong> and <strong>serialPersistentFields 033 * </strong>. 034 * </p> 035 * <ul> 036 * <li> 037 * Property {@code format} - Specifies valid identifiers. Default value is 038 * {@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 * Default value is {@code true}. 043 * </li> 044 * <li> 045 * Property {@code applyToProtected} - Controls whether to apply the check to protected member. 046 * Default value is {@code true}. 047 * </li> 048 * <li> 049 * Property {@code applyToPackage} - Controls whether to apply the check to package-private member. 050 * Default value is {@code true}. 051 * </li> 052 * <li> 053 * Property {@code applyToPrivate} - Controls whether to apply the check to private member. 054 * Default value is {@code true}. 055 * </li> 056 * </ul> 057 * <p> 058 * An example of how to configure the check is: 059 * </p> 060 * <pre> 061 * <module name="ConstantName"/> 062 * </pre> 063 * 064 * <p> 065 * The following configuration apart from names allowed by default allows {@code log} 066 * or {@code logger}: 067 * </p> 068 * <pre> 069 * <module name="ConstantName"> 070 * <property name="format" 071 * value="^log(ger)?|[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/> 072 * </module> 073 * </pre> 074 * 075 * @since 3.0 076 */ 077public class ConstantNameCheck 078 extends AbstractAccessControlNameCheck { 079 080 /** Creates a new {@code ConstantNameCheck} instance. */ 081 public ConstantNameCheck() { 082 super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"); 083 } 084 085 @Override 086 public int[] getDefaultTokens() { 087 return getRequiredTokens(); 088 } 089 090 @Override 091 public int[] getAcceptableTokens() { 092 return getRequiredTokens(); 093 } 094 095 @Override 096 public int[] getRequiredTokens() { 097 return new int[] {TokenTypes.VARIABLE_DEF}; 098 } 099 100 @Override 101 protected final boolean mustCheckName(DetailAST ast) { 102 boolean returnValue = false; 103 104 final DetailAST modifiersAST = 105 ast.findFirstToken(TokenTypes.MODIFIERS); 106 final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null; 107 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null; 108 109 if (isStatic && isFinal && shouldCheckInScope(modifiersAST) 110 || ScopeUtil.isInAnnotationBlock(ast) 111 || ScopeUtil.isInInterfaceOrAnnotationBlock(ast) 112 && !ScopeUtil.isInCodeBlock(ast)) { 113 // Handle the serialVersionUID and serialPersistentFields constants 114 // which are used for Serialization. Cannot enforce rules on it. :-) 115 final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT); 116 if (!"serialVersionUID".equals(nameAST.getText()) 117 && !"serialPersistentFields".equals(nameAST.getText())) { 118 returnValue = true; 119 } 120 } 121 122 return returnValue; 123 } 124 125}