View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.naming;
21  
22  import com.puppycrawl.tools.checkstyle.api.DetailAST;
23  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24  import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
25  
26  /**
27   * <p>
28   * Checks that instance variable names conform to a specified pattern.
29   * </p>
30   * <ul>
31   * <li>
32   * Property {@code applyToPackage} - Control if check should apply to package-private members.
33   * Type is {@code boolean}.
34   * Default value is {@code true}.
35   * </li>
36   * <li>
37   * Property {@code applyToPrivate} - Control if check should apply to private members.
38   * Type is {@code boolean}.
39   * Default value is {@code true}.
40   * </li>
41   * <li>
42   * Property {@code applyToProtected} - Control if check should apply to protected members.
43   * Type is {@code boolean}.
44   * Default value is {@code true}.
45   * </li>
46   * <li>
47   * Property {@code applyToPublic} - Control if check should apply to public members.
48   * Type is {@code boolean}.
49   * Default value is {@code true}.
50   * </li>
51   * <li>
52   * Property {@code format} - Sets the pattern to match valid identifiers.
53   * Type is {@code java.util.regex.Pattern}.
54   * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}.
55   * </li>
56   * </ul>
57   * <p>
58   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
59   * </p>
60   * <p>
61   * Violation Message Keys:
62   * </p>
63   * <ul>
64   * <li>
65   * {@code name.invalidPattern}
66   * </li>
67   * </ul>
68   *
69   * @since 3.0
70   */
71  public class MemberNameCheck
72      extends AbstractAccessControlNameCheck {
73  
74      /** Creates a new {@code MemberNameCheck} instance. */
75      public MemberNameCheck() {
76          super("^[a-z][a-zA-Z0-9]*$");
77      }
78  
79      @Override
80      public int[] getDefaultTokens() {
81          return getRequiredTokens();
82      }
83  
84      @Override
85      public int[] getAcceptableTokens() {
86          return getRequiredTokens();
87      }
88  
89      @Override
90      public int[] getRequiredTokens() {
91          return new int[] {TokenTypes.VARIABLE_DEF};
92      }
93  
94      @Override
95      protected final boolean mustCheckName(DetailAST ast) {
96          final DetailAST modifiersAST =
97              ast.findFirstToken(TokenTypes.MODIFIERS);
98          final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
99  
100         return !isStatic && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast)
101             && !ScopeUtil.isLocalVariableDef(ast)
102                 && shouldCheckInScope(modifiersAST);
103     }
104 
105 }