Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractAccessControlNameCheck |
|
| 2.0;2 |
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.TokenTypes; | |
23 | ||
24 | /** | |
25 | * Abstract class for checking a class member (field/method)'s name conforms to | |
26 | * a format specified by the format property. | |
27 | * | |
28 | * <p>This class extends {@link AbstractNameCheck} with support for access level | |
29 | * restrictions. This allows the check to be configured to be applied to one of | |
30 | * the four Java access levels: {@code public}, {@code protected}, | |
31 | * {@code "package"}, and {@code private}. | |
32 | * | |
33 | * <p>Level is configured using the following properties: | |
34 | * <ol> | |
35 | * <li>applyToPublic, default true; | |
36 | * <li>applyToProtected, default true; | |
37 | * <li>applyToPackage, default true; | |
38 | * <li>applyToPrivate, default true; | |
39 | * </ol> | |
40 | * </p> | |
41 | * | |
42 | * @author Rick Giles | |
43 | * @version 1.0 | |
44 | */ | |
45 | public abstract class AbstractAccessControlNameCheck | |
46 | extends AbstractNameCheck | |
47 | { | |
48 | /** If true, applies the check be public members. */ | |
49 | 73 | private boolean mApplyToPublic = true; |
50 | ||
51 | /** If true, applies the check be protected members. */ | |
52 | 73 | private boolean mApplyToProtected = true; |
53 | ||
54 | /** If true, applies the check be "package" members. */ | |
55 | 73 | private boolean mApplyToPackage = true; |
56 | ||
57 | /** If true, applies the check be private members. */ | |
58 | 73 | private boolean mApplyToPrivate = true; |
59 | ||
60 | /** | |
61 | * Creates a new {@code AbstractAccessControlNameCheck} instance. | |
62 | * | |
63 | * @param aFormat | |
64 | * format to check with | |
65 | */ | |
66 | public AbstractAccessControlNameCheck(String aFormat) | |
67 | { | |
68 | 73 | super(aFormat); |
69 | 73 | } |
70 | ||
71 | @Override | |
72 | protected boolean mustCheckName(DetailAST aAST) | |
73 | { | |
74 | 36 | return shouldCheckInScope(aAST); |
75 | } | |
76 | ||
77 | /** | |
78 | * Should we check member with given modifiers. | |
79 | * | |
80 | * @param aModifiers | |
81 | * modifiers of member to check. | |
82 | * @return true if we should check such member. | |
83 | */ | |
84 | protected boolean shouldCheckInScope(DetailAST aModifiers) | |
85 | { | |
86 | 512 | if (aModifiers == null) { |
87 | // if there are no modifiers it is a package-private | |
88 | 0 | return mApplyToPackage; |
89 | } | |
90 | ||
91 | 512 | final boolean isPublic = aModifiers |
92 | .branchContains(TokenTypes.LITERAL_PUBLIC); | |
93 | 512 | final boolean isProtected = aModifiers |
94 | .branchContains(TokenTypes.LITERAL_PROTECTED); | |
95 | 512 | final boolean isPrivate = aModifiers |
96 | .branchContains(TokenTypes.LITERAL_PRIVATE); | |
97 | 512 | final boolean isPackage = !(isPublic || isProtected || isPrivate); |
98 | ||
99 | 512 | return (mApplyToPublic && isPublic) |
100 | || (mApplyToProtected && isProtected) | |
101 | || (mApplyToPackage && isPackage) | |
102 | || (mApplyToPrivate && isPrivate); | |
103 | } | |
104 | ||
105 | /** | |
106 | * Sets whether we should apply the check to public members. | |
107 | * | |
108 | * @param aApplyTo new value of the property. | |
109 | */ | |
110 | public void setApplyToPublic(boolean aApplyTo) | |
111 | { | |
112 | 4 | mApplyToPublic = aApplyTo; |
113 | 4 | } |
114 | ||
115 | /** @return true if the check should be applied to public members. */ | |
116 | public boolean getApplyToPublic() | |
117 | { | |
118 | 0 | return mApplyToPublic; |
119 | } | |
120 | ||
121 | /** | |
122 | * Sets whether we should apply the check to protected members. | |
123 | * | |
124 | * @param aApplyTo new value of the property. | |
125 | */ | |
126 | public void setApplyToProtected(boolean aApplyTo) | |
127 | { | |
128 | 4 | mApplyToProtected = aApplyTo; |
129 | 4 | } |
130 | ||
131 | /** @return true if the check should be applied to protected members. */ | |
132 | public boolean getApplyToProtected() | |
133 | { | |
134 | 0 | return mApplyToProtected; |
135 | } | |
136 | ||
137 | /** | |
138 | * Sets whether we should apply the check to package-private members. | |
139 | * | |
140 | * @param aApplyTo new value of the property. | |
141 | */ | |
142 | public void setApplyToPackage(boolean aApplyTo) | |
143 | { | |
144 | 4 | mApplyToPackage = aApplyTo; |
145 | 4 | } |
146 | ||
147 | /** | |
148 | * @return true if the check should be applied to package-private members. | |
149 | */ | |
150 | public boolean getApplyToPackage() | |
151 | { | |
152 | 0 | return mApplyToPackage; |
153 | } | |
154 | ||
155 | /** | |
156 | * Sets whether we should apply the check to private members. | |
157 | * | |
158 | * @param aApplyTo new value of the property. | |
159 | */ | |
160 | public void setApplyToPrivate(boolean aApplyTo) | |
161 | { | |
162 | 6 | mApplyToPrivate = aApplyTo; |
163 | 6 | } |
164 | ||
165 | /** @return true if the check should be applied to private members. */ | |
166 | public boolean getApplyToPrivate() | |
167 | { | |
168 | 0 | return mApplyToPrivate; |
169 | } | |
170 | } |