001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 {@code static}, non-{@code final} variable names conform to a specified pattern. 029 * </div> 030 * <ul> 031 * <li> 032 * Property {@code applyToPackage} - Control if check should apply to package-private 033 * members. 034 * Type is {@code boolean}. 035 * Default value is {@code true}. 036 * </li> 037 * <li> 038 * Property {@code applyToPrivate} - Control if check should apply to private members. 039 * Type is {@code boolean}. 040 * Default value is {@code true}. 041 * </li> 042 * <li> 043 * Property {@code applyToProtected} - Control if check should apply to protected 044 * members. 045 * Type is {@code boolean}. 046 * Default value is {@code true}. 047 * </li> 048 * <li> 049 * Property {@code applyToPublic} - Control if check should apply to public members. 050 * Type is {@code boolean}. 051 * Default value is {@code true}. 052 * </li> 053 * <li> 054 * Property {@code format} - Sets the pattern to match valid identifiers. 055 * Type is {@code java.util.regex.Pattern}. 056 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}. 057 * </li> 058 * </ul> 059 * 060 * <p> 061 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 062 * </p> 063 * 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 StaticVariableNameCheck 076 extends AbstractAccessControlNameCheck { 077 078 /** Creates a new {@code StaticVariableNameCheck} instance. */ 079 public StaticVariableNameCheck() { 080 super("^[a-z][a-zA-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 final DetailAST modifiersAST = 101 ast.findFirstToken(TokenTypes.MODIFIERS); 102 final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null; 103 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null; 104 105 return isStatic 106 && !isFinal 107 && shouldCheckInScope(modifiersAST) 108 && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast); 109 } 110 111}