001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 {@code static}, non-{@code final} variable names conform to a specified pattern. 029 * </p> 030 * <ul> 031 * <li> 032 * Property {@code format} - Specifies valid identifiers. Default value is 033 * {@code "^[a-z][a-zA-Z0-9]*$"}. 034 * </li> 035 * <li> 036 * Property {@code applyToPublic} - Controls whether to apply the check to public member. 037 * Default value is {@code true}. 038 * </li> 039 * <li> 040 * Property {@code applyToProtected} - Controls whether to apply the check to protected member. 041 * Default value is {@code true}. 042 * </li> 043 * <li> 044 * Property {@code applyToPackage} - Controls whether to apply the check to package-private member. 045 * Default value is {@code true}. 046 * </li> 047 * <li> 048 * Property {@code applyToPrivate} - Controls whether to apply the check to private member. 049 * Default value is {@code true}. 050 * </li> 051 * </ul> 052 * <p> 053 * An example of how to configure the check is: 054 * </p> 055 * <pre> 056 * <module name="StaticVariableName"/> 057 * </pre> 058 * <p> 059 * An example of how to configure the check for names that begin with 060 * a lower case letter, followed by letters, digits, and underscores is: 061 * </p> 062 * <pre> 063 * <module name="StaticVariableName"> 064 * <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/> 065 * </module> 066 * </pre> 067 * 068 * @since 3.0 069 */ 070public class StaticVariableNameCheck 071 extends AbstractAccessControlNameCheck { 072 073 /** Creates a new {@code StaticVariableNameCheck} instance. */ 074 public StaticVariableNameCheck() { 075 super("^[a-z][a-zA-Z0-9]*$"); 076 } 077 078 @Override 079 public int[] getDefaultTokens() { 080 return getRequiredTokens(); 081 } 082 083 @Override 084 public int[] getAcceptableTokens() { 085 return getRequiredTokens(); 086 } 087 088 @Override 089 public int[] getRequiredTokens() { 090 return new int[] {TokenTypes.VARIABLE_DEF}; 091 } 092 093 @Override 094 protected final boolean mustCheckName(DetailAST ast) { 095 final DetailAST modifiersAST = 096 ast.findFirstToken(TokenTypes.MODIFIERS); 097 final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null; 098 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null; 099 100 return isStatic 101 && !isFinal 102 && shouldCheckInScope(modifiersAST) 103 && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast); 104 } 105 106}