001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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 instance variable names conform to a specified pattern.
029 * </p>
030 * <ul>
031 * <li>
032 * Property {@code format} - Specifies valid identifiers.
033 * Type is {@code java.util.regex.Pattern}.
034 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}.
035 * </li>
036 * <li>
037 * Property {@code applyToPublic} - Controls whether to apply the check to public member.
038 * Type is {@code boolean}.
039 * Default value is {@code true}.
040 * </li>
041 * <li>
042 * Property {@code applyToProtected} - Controls whether to apply the check to protected member.
043 * Type is {@code boolean}.
044 * Default value is {@code true}.
045 * </li>
046 * <li>
047 * Property {@code applyToPackage} - Controls whether to apply the check to package-private member.
048 * Type is {@code boolean}.
049 * Default value is {@code true}.
050 * </li>
051 * <li>
052 * Property {@code applyToPrivate} - Controls whether to apply the check to private member.
053 * Type is {@code boolean}.
054 * Default value is {@code true}.
055 * </li>
056 * </ul>
057 * <p>
058 * To configure the check:
059 * </p>
060 * <pre>
061 * &lt;module name=&quot;MemberName&quot;/&gt;
062 * </pre>
063 * <p>Code Example:</p>
064 * <pre>
065 * class MyClass {
066 *   public int num1; // OK
067 *   protected int num2; // OK
068 *   final int num3 = 3; // OK
069 *   private int num4; // OK
070 *
071 *   static int num5; // ignored: not an instance variable
072 *   public static final int CONSTANT = 1; // ignored: not an instance variable
073 *
074 *   public int NUM1; // violation, name 'NUM1'
075 *                    // must match pattern '^[a-z][a-zA-Z0-9]*$'
076 *   protected int NUM2; // violation, name 'NUM2'
077 *                       // must match pattern '^[a-z][a-zA-Z0-9]*$'
078 *   final int NUM3; // violation, name 'NUM3'
079 *                   // must match pattern '^[a-z][a-zA-Z0-9]*$'
080 *   private int NUM4; // violation, name 'NUM4'
081 *                     // must match pattern '^[a-z][a-zA-Z0-9]*$'
082 * }
083 * </pre>
084 * <p>
085 * An example of how to configure the check for names that begin with
086 * {@code &quot;m&quot;}, followed by an upper case letter, and then letters
087 * and digits. Also, suppress the check from being applied to protected
088 * and package-private member:
089 * </p>
090 *
091 * <pre>
092 * &lt;module name=&quot;MemberName&quot;&gt;
093 *   &lt;property name=&quot;format&quot; value=&quot;^m[A-Z][a-zA-Z0-9]*$&quot;/&gt;
094 *   &lt;property name=&quot;applyToProtected&quot; value=&quot;false&quot;/&gt;
095 *   &lt;property name=&quot;applyToPackage&quot; value=&quot;false&quot;/&gt;
096 * &lt;/module&gt;
097 * </pre>
098 * <p>Code Example:</p>
099 * <pre>
100 * class MyClass {
101 *   public int num1; // violation, name 'num1'
102 *                    // must match pattern '^m[A-Z][a-zA-Z0-9]*$'
103 *   protected int num2; // OK
104 *   int num3; // OK
105 *   private int num4; // violation, name 'num4'
106 *                     // must match pattern '^m[A-Z][a-zA-Z0-9]*$'
107 * }
108 * </pre>
109 * <p>
110 * An example of how to suppress the check which is applied to
111 * public and private member:
112 * </p>
113 * <pre>
114 * &lt;module name=&quot;MemberName&quot;&gt;
115 *   &lt;property name=&quot;applyToPublic&quot; value=&quot;false&quot;/&gt;
116 *   &lt;property name=&quot;applyToPrivate&quot; value=&quot;false&quot;/&gt;
117 * &lt;/module&gt;
118 * </pre>
119 * <p>Code Example:</p>
120 * <pre>
121 * class MyClass {
122 *   public int NUM1; // OK
123 *   protected int NUM2; // violation, name 'NUM2'
124 *                       // must match pattern '^[a-z][a-zA-Z0-9]*$'
125 *   int NUM3; // violation, name 'NUM3'
126 *             // must match pattern '^[a-z][a-zA-Z0-9]*$'
127 *   private int NUM4; // OK
128 * }
129 * </pre>
130 * <p>
131 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
132 * </p>
133 * <p>
134 * Violation Message Keys:
135 * </p>
136 * <ul>
137 * <li>
138 * {@code name.invalidPattern}
139 * </li>
140 * </ul>
141 *
142 * @since 3.0
143 */
144public class MemberNameCheck
145    extends AbstractAccessControlNameCheck {
146
147    /** Creates a new {@code MemberNameCheck} instance. */
148    public MemberNameCheck() {
149        super("^[a-z][a-zA-Z0-9]*$");
150    }
151
152    @Override
153    public int[] getDefaultTokens() {
154        return getRequiredTokens();
155    }
156
157    @Override
158    public int[] getAcceptableTokens() {
159        return getRequiredTokens();
160    }
161
162    @Override
163    public int[] getRequiredTokens() {
164        return new int[] {TokenTypes.VARIABLE_DEF};
165    }
166
167    @Override
168    protected final boolean mustCheckName(DetailAST ast) {
169        final DetailAST modifiersAST =
170            ast.findFirstToken(TokenTypes.MODIFIERS);
171        final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
172
173        return !isStatic && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast)
174            && !ScopeUtil.isLocalVariableDef(ast)
175                && shouldCheckInScope(modifiersAST);
176    }
177
178}