KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > naming > MemberNameCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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  * &lt;module name="MemberName"/&gt;
38  * </pre>
39  * <p>
40  * An example of how to configure the check for names that begin with
41  * &quot;m&quot;, followed by an upper case letter, and then letters and
42  * digits is:
43  * </p>
44  * <pre>
45  * &lt;module name="MemberName"&gt;
46  * &lt;property name="format" value="^m[A-Z][a-zA-Z0-9]*$"/&gt;
47  * &lt;/module&gt;
48  * </pre>
49  * @author Rick Giles
50  * @version 1.0
51  */

52 public class MemberNameCheck
53     extends AbstractNameCheck
54 {
55     /** Should we apply check to public.*/
56     private boolean mApplyToPublic = true;
57     /** Should we apply check to protected.*/
58     private boolean mApplyToProtected = true;
59     /** Should we apply check to package.*/
60     private boolean mApplyToPackage = true;
61     /** Should we apply check to private.*/
62     private boolean mApplyToPrivate = true;
63
64     /** Creates a new <code>MemberNameCheck</code> instance. */
65     public MemberNameCheck()
66     {
67         super("^[a-z][a-zA-Z0-9]*$");
68     }
69
70     /** {@inheritDoc} */
71     public int[] getDefaultTokens()
72     {
73         return new int[] {TokenTypes.VARIABLE_DEF};
74     }
75
76     /** {@inheritDoc} */
77     protected final boolean mustCheckName(DetailAST aAST)
78     {
79         final DetailAST modifiersAST =
80             aAST.findFirstToken(TokenTypes.MODIFIERS);
81         final boolean isStatic = (modifiersAST != null)
82             && modifiersAST.branchContains(TokenTypes.LITERAL_STATIC);
83
84         return (!isStatic && !ScopeUtils.inInterfaceOrAnnotationBlock(aAST)
85             && !ScopeUtils.isLocalVariableDef(aAST))
86             && (modifiersAST != null)
87             && shouldCheckInScope(modifiersAST);
88     }
89
90     /**
91      * Should we check member with given modifiers.
92      * @param aModifiers modifiers of member to check.
93      * @return true if we should check such member.
94      */

95     private boolean shouldCheckInScope(DetailAST aModifiers)
96     {
97         if (aModifiers == null) {
98             // if there are no modifaiers it is a package-private
99
return mApplyToPackage;
100         }
101
102         final boolean isPublic =
103             aModifiers.branchContains(TokenTypes.LITERAL_PUBLIC);
104         final boolean isProtected =
105             aModifiers.branchContains(TokenTypes.LITERAL_PROTECTED);
106         final boolean isPrivate =
107             aModifiers.branchContains(TokenTypes.LITERAL_PRIVATE);
108         final boolean isPackage = !(isPublic || isProtected || isPrivate);
109
110         return (mApplyToPublic && isPublic)
111             || (mApplyToProtected && isProtected)
112             || (mApplyToPackage && isPackage) || (mApplyToPrivate && isPrivate);
113     }
114
115     /**
116      * Sets whether we should apply the check to public members.
117      * @param aApplyTo new value of the property.
118      */

119     public void setApplyToPublic(boolean aApplyTo)
120     {
121         mApplyToPublic = aApplyTo;
122     }
123
124     /** @return true if the check should be applied to public members.*/
125     public boolean getApplyToPublic()
126     {
127         return mApplyToPublic;
128     }
129
130     /**
131      * Sets whether we should apply the check to protected members.
132      * @param aApplyTo new value of the property.
133      */

134     public void setApplyToProtected(boolean aApplyTo)
135     {
136         mApplyToProtected = aApplyTo;
137     }
138
139     /** @return true if the check should be applied to protected members.*/
140     public boolean getApplyToProtected()
141     {
142         return mApplyToProtected;
143     }
144
145     /**
146      * Sets whether we should apply the check to package-private members.
147      * @param aApplyTo new value of the property.
148      */

149     public void setApplyToPackage(boolean aApplyTo)
150     {
151         mApplyToPackage = aApplyTo;
152     }
153
154     /**
155      * @return true if the check should be applied to package-private members.
156      */

157     public boolean getApplyToPackage()
158     {
159         return mApplyToPackage;
160     }
161
162     /**
163      * Sets whether we should apply the check to private members.
164      * @param aApplyTo new value of the property.
165      */

166     public void setApplyToPrivate(boolean aApplyTo)
167     {
168         mApplyToPrivate = aApplyTo;
169     }
170
171     /** @return true if the check should be applied to private members.*/
172     public boolean getApplyToPrivate()
173     {
174         return mApplyToPrivate;
175     }
176 }
177
Popular Tags