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 instance 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 * &lt;module name=&quot;MemberName&quot;/&gt;
057 * </pre>
058 * <p>Code Example:</p>
059 * <pre>
060 * class MyClass {
061 *   public int num1; // OK
062 *   protected int num2; // OK
063 *   int num3; // OK
064 *   private int num4; // OK
065 *
066 *   public int NUM1; // violation, name 'NUM1'
067 *                    // must match pattern '^[a-z][a-zA-Z0-9]*$'
068 *   protected int NUM2; // violation, name 'NUM2'
069 *                       // must match pattern '^[a-z][a-zA-Z0-9]*$'
070 *   int NUM3; // violation, name 'NUM3'
071 *             // must match pattern '^[a-z][a-zA-Z0-9]*$'
072 *   private int NUM4; // violation, name 'NUM4'
073 *                     // must match pattern '^[a-z][a-zA-Z0-9]*$'
074 * }
075 * </pre>
076 * <p>
077 * An example of how to configure the check for names that begin with
078 * {@code &quot;m&quot;}, followed by an upper case letter, and then letters
079 * and digits. Also, suppress the check from being applied to protected
080 * and package-private member:
081 * </p>
082 *
083 * <pre>
084 * &lt;module name=&quot;MemberName&quot;&gt;
085 *   &lt;property name=&quot;format&quot; value=&quot;^m[A-Z][a-zA-Z0-9]*$&quot;/&gt;
086 *   &lt;property name=&quot;applyToProtected&quot; value=&quot;false&quot;/&gt;
087 *   &lt;property name=&quot;applyToPackage&quot; value=&quot;false&quot;/&gt;
088 * &lt;/module&gt;
089 * </pre>
090 * <p>Code Example:</p>
091 * <pre>
092 * class MyClass {
093 *   public int num1; // violation, name 'num1'
094 *                    // must match pattern '^m[A-Z][a-zA-Z0-9]*$'
095 *   protected int num2; // OK
096 *   int num3; // OK
097 *   private int num4; // violation, name 'num4'
098 *                     // must match pattern '^m[A-Z][a-zA-Z0-9]*$'
099 * }
100 * </pre>
101 * <p>
102 * An example of how to suppress the check which is applied to
103 * public and private member:
104 * </p>
105 * <pre>
106 * &lt;module name=&quot;MemberName&quot;&gt;
107 *   &lt;property name=&quot;applyToPublic&quot; value=&quot;false&quot;/&gt;
108 *   &lt;property name=&quot;applyToPrivate&quot; value=&quot;false&quot;/&gt;
109 * &lt;/module&gt;
110 * </pre>
111 * <p>Code Example:</p>
112 * <pre>
113 * class MyClass {
114 *   public int NUM1; // OK
115 *   protected int NUM2; // violation, name 'NUM2'
116 *                       // must match pattern '^[a-z][a-zA-Z0-9]*$'
117 *   int NUM3; // violation, name 'NUM3'
118 *             // must match pattern '^[a-z][a-zA-Z0-9]*$'
119 *   private int NUM4; // OK
120 * }
121 * </pre>
122 *
123 * @since 3.0
124 */
125public class MemberNameCheck
126    extends AbstractAccessControlNameCheck {
127
128    /** Creates a new {@code MemberNameCheck} instance. */
129    public MemberNameCheck() {
130        super("^[a-z][a-zA-Z0-9]*$");
131    }
132
133    @Override
134    public int[] getDefaultTokens() {
135        return getRequiredTokens();
136    }
137
138    @Override
139    public int[] getAcceptableTokens() {
140        return getRequiredTokens();
141    }
142
143    @Override
144    public int[] getRequiredTokens() {
145        return new int[] {TokenTypes.VARIABLE_DEF};
146    }
147
148    @Override
149    protected final boolean mustCheckName(DetailAST ast) {
150        final DetailAST modifiersAST =
151            ast.findFirstToken(TokenTypes.MODIFIERS);
152        final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
153
154        return !isStatic && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast)
155            && !ScopeUtil.isLocalVariableDef(ast)
156                && shouldCheckInScope(modifiersAST);
157    }
158
159}