1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package com.puppycrawl.tools.checkstyle.checks.design; |
20 | |
|
21 | |
import antlr.collections.AST; |
22 | |
import com.google.common.collect.Sets; |
23 | |
import com.puppycrawl.tools.checkstyle.api.Check; |
24 | |
import com.puppycrawl.tools.checkstyle.api.DetailAST; |
25 | |
import com.puppycrawl.tools.checkstyle.api.ScopeUtils; |
26 | |
import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
27 | |
import com.puppycrawl.tools.checkstyle.api.Utils; |
28 | |
import java.util.Set; |
29 | |
import java.util.regex.Pattern; |
30 | |
import java.util.regex.PatternSyntaxException; |
31 | |
import org.apache.commons.beanutils.ConversionException; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class VisibilityModifierCheck |
46 | |
extends Check |
47 | |
{ |
48 | |
|
49 | |
private boolean mProtectedAllowed; |
50 | |
|
51 | |
|
52 | |
private boolean mPackageAllowed; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 4 | private String mPublicMemberFormat = "^serialVersionUID$"; |
62 | |
|
63 | |
|
64 | |
private Pattern mPublicMemberPattern; |
65 | |
|
66 | |
|
67 | |
public VisibilityModifierCheck() |
68 | 4 | { |
69 | 4 | setPublicMemberPattern(mPublicMemberFormat); |
70 | 4 | } |
71 | |
|
72 | |
|
73 | |
public boolean isProtectedAllowed() |
74 | |
{ |
75 | 7 | return mProtectedAllowed; |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void setProtectedAllowed(boolean aProtectedAllowed) |
83 | |
{ |
84 | 1 | mProtectedAllowed = aProtectedAllowed; |
85 | 1 | } |
86 | |
|
87 | |
|
88 | |
public boolean isPackageAllowed() |
89 | |
{ |
90 | 7 | return mPackageAllowed; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public void setPackageAllowed(boolean aPackageAllowed) |
98 | |
{ |
99 | 1 | mPackageAllowed = aPackageAllowed; |
100 | 1 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void setPublicMemberPattern(String aPattern) |
107 | |
{ |
108 | |
try { |
109 | 8 | mPublicMemberPattern = Utils.getPattern(aPattern); |
110 | 8 | mPublicMemberFormat = aPattern; |
111 | |
} |
112 | 0 | catch (final PatternSyntaxException e) { |
113 | 0 | throw new ConversionException("unable to parse " + aPattern, e); |
114 | 8 | } |
115 | 8 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
private Pattern getPublicMemberRegexp() |
121 | |
{ |
122 | 9 | return mPublicMemberPattern; |
123 | |
} |
124 | |
|
125 | |
@Override |
126 | |
public int[] getDefaultTokens() |
127 | |
{ |
128 | 4 | return new int[] {TokenTypes.VARIABLE_DEF}; |
129 | |
} |
130 | |
|
131 | |
@Override |
132 | |
public void visitToken(DetailAST aAST) |
133 | |
{ |
134 | 63 | if ((aAST.getType() != TokenTypes.VARIABLE_DEF) |
135 | |
|| (aAST.getParent().getType() != TokenTypes.OBJBLOCK)) |
136 | |
{ |
137 | 19 | return; |
138 | |
} |
139 | |
|
140 | 44 | final DetailAST varNameAST = getVarNameAST(aAST); |
141 | 44 | final String varName = varNameAST.getText(); |
142 | 44 | final boolean inInterfaceOrAnnotationBlock = |
143 | |
ScopeUtils.inInterfaceOrAnnotationBlock(aAST); |
144 | 44 | final Set<String> mods = getModifiers(aAST); |
145 | 44 | final String declaredScope = getVisibilityScope(mods); |
146 | 44 | final String variableScope = |
147 | |
inInterfaceOrAnnotationBlock ? "public" : declaredScope; |
148 | |
|
149 | 44 | if (!("private".equals(variableScope) |
150 | |
|| inInterfaceOrAnnotationBlock |
151 | |
|| (mods.contains("static") && mods.contains("final")) |
152 | |
|| ("package".equals(variableScope) && isPackageAllowed()) |
153 | |
|| ("protected".equals(variableScope) && isProtectedAllowed()) |
154 | |
|| ("public".equals(variableScope) |
155 | |
&& getPublicMemberRegexp().matcher(varName).find()))) |
156 | |
{ |
157 | 17 | log(varNameAST.getLineNo(), varNameAST.getColumnNo(), |
158 | |
"variable.notPrivate", varName); |
159 | |
} |
160 | 44 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
private DetailAST getVarNameAST(DetailAST aVariableDefAST) |
168 | |
{ |
169 | 44 | DetailAST ast = aVariableDefAST.getFirstChild(); |
170 | 88 | while (ast != null) { |
171 | 88 | final DetailAST nextSibling = ast.getNextSibling(); |
172 | 88 | if (ast.getType() == TokenTypes.TYPE) { |
173 | 44 | return nextSibling; |
174 | |
} |
175 | 44 | ast = nextSibling; |
176 | 44 | } |
177 | 0 | return null; |
178 | |
} |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
private Set<String> getModifiers(DetailAST aVariableDefAST) |
186 | |
{ |
187 | 44 | final AST modifiersAST = aVariableDefAST.getFirstChild(); |
188 | 44 | if (modifiersAST.getType() != TokenTypes.MODIFIERS) { |
189 | 0 | throw new IllegalStateException("Strange parse tree"); |
190 | |
} |
191 | 44 | final Set<String> retVal = Sets.newHashSet(); |
192 | 44 | AST modifier = modifiersAST.getFirstChild(); |
193 | 95 | while (modifier != null) { |
194 | 51 | retVal.add(modifier.getText()); |
195 | 51 | modifier = modifier.getNextSibling(); |
196 | |
} |
197 | 44 | return retVal; |
198 | |
|
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
private String getVisibilityScope(Set<String> aModifiers) |
207 | |
{ |
208 | 44 | final String[] explicitModifiers = {"public", "private", "protected"}; |
209 | 108 | for (final String candidate : explicitModifiers) { |
210 | 96 | if (aModifiers.contains(candidate)) { |
211 | 32 | return candidate; |
212 | |
} |
213 | |
} |
214 | 12 | return "package"; |
215 | |
} |
216 | |
} |