Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MemberNameCheck |
|
| 2.3333333333333335;2.333 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 Oliver Burn | |
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 | package com.puppycrawl.tools.checkstyle.checks.naming; | |
20 | ||
21 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
22 | import com.puppycrawl.tools.checkstyle.api.ScopeUtils; | |
23 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
24 | ||
25 | /** | |
26 | * <p> | |
27 | * Checks that instance variable names conform to a format specified | |
28 | * by the format property. The format is a | |
29 | * {@link java.util.regex.Pattern regular expression} | |
30 | * and defaults to | |
31 | * <strong>^[a-z][a-zA-Z0-9]*$</strong>. | |
32 | * </p> | |
33 | * <p> | |
34 | * An example of how to configure the check is: | |
35 | * </p> | |
36 | * <pre> | |
37 | * <module name="MemberName"/> | |
38 | * </pre> | |
39 | * <p> | |
40 | * An example of how to configure the check for names that begin with | |
41 | * "m", followed by an upper case letter, and then letters and | |
42 | * digits is: | |
43 | * </p> | |
44 | * <pre> | |
45 | * <module name="MemberName"> | |
46 | * <property name="format" value="^m[A-Z][a-zA-Z0-9]*$"/> | |
47 | * </module> | |
48 | * </pre> | |
49 | * @author Rick Giles | |
50 | * @version 1.0 | |
51 | */ | |
52 | public class MemberNameCheck | |
53 | extends AbstractAccessControlNameCheck | |
54 | { | |
55 | /** Creates a new <code>MemberNameCheck</code> instance. */ | |
56 | public MemberNameCheck() | |
57 | { | |
58 | 39 | super("^[a-z][a-zA-Z0-9]*$"); |
59 | 39 | } |
60 | ||
61 | @Override | |
62 | public int[] getDefaultTokens() | |
63 | { | |
64 | 39 | return new int[] {TokenTypes.VARIABLE_DEF}; |
65 | } | |
66 | ||
67 | @Override | |
68 | protected final boolean mustCheckName(DetailAST aAST) | |
69 | { | |
70 | 509 | final DetailAST modifiersAST = |
71 | aAST.findFirstToken(TokenTypes.MODIFIERS); | |
72 | 509 | final boolean isStatic = (modifiersAST != null) |
73 | && modifiersAST.branchContains(TokenTypes.LITERAL_STATIC); | |
74 | ||
75 | 509 | return (!isStatic && !ScopeUtils.inInterfaceOrAnnotationBlock(aAST) |
76 | && !ScopeUtils.isLocalVariableDef(aAST)) | |
77 | && shouldCheckInScope(modifiersAST); | |
78 | } | |
79 | ||
80 | } |