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.ScopeUtils; 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>. The format is a regular expression 034 * and defaults to <strong>^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$</strong>. 035 * </p> 036 * <p> 037 * An example of how to configure the check is: 038 * </p> 039 * <pre> 040 * <module name="ConstantName"/> 041 * </pre> 042 * 043 * <p> 044 * An example of how to configure the check for names that are only upper case 045 * letters and digits is: 046 * </p> 047 * <pre> 048 * <module name="ConstantName"> 049 * <property name="format" value="^[A-Z][A-Z0-9]*$"/> 050 * </module> 051 * </pre> 052 * 053 * 054 * @author Rick Giles 055 */ 056public class ConstantNameCheck 057 extends AbstractAccessControlNameCheck { 058 059 /** Creates a new {@code ConstantNameCheck} instance. */ 060 public ConstantNameCheck() { 061 super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"); 062 } 063 064 @Override 065 public int[] getDefaultTokens() { 066 return getRequiredTokens(); 067 } 068 069 @Override 070 public int[] getAcceptableTokens() { 071 return getRequiredTokens(); 072 } 073 074 @Override 075 public int[] getRequiredTokens() { 076 return new int[] {TokenTypes.VARIABLE_DEF}; 077 } 078 079 @Override 080 protected final boolean mustCheckName(DetailAST ast) { 081 boolean returnValue = false; 082 083 final DetailAST modifiersAST = 084 ast.findFirstToken(TokenTypes.MODIFIERS); 085 final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null; 086 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null; 087 088 if (isStatic && isFinal && shouldCheckInScope(modifiersAST) 089 || ScopeUtils.isInAnnotationBlock(ast) 090 || ScopeUtils.isInInterfaceOrAnnotationBlock(ast) 091 && !ScopeUtils.isInCodeBlock(ast)) { 092 // Handle the serialVersionUID and serialPersistentFields constants 093 // which are used for Serialization. Cannot enforce rules on it. :-) 094 final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT); 095 if (!"serialVersionUID".equals(nameAST.getText()) 096 && !"serialPersistentFields".equals(nameAST.getText())) { 097 returnValue = true; 098 } 099 } 100 101 return returnValue; 102 } 103 104}