1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.utils;
21
22 import java.util.Optional;
23
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.Scope;
26 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
27
28
29
30
31
32 public final class ScopeUtil {
33
34
35 private ScopeUtil() {
36 }
37
38
39
40
41
42
43
44
45 public static Scope getDeclaredScopeFromMods(DetailAST aMods) {
46 Scope result = null;
47 for (DetailAST token = aMods.getFirstChild(); token != null;
48 token = token.getNextSibling()) {
49 switch (token.getType()) {
50 case TokenTypes.LITERAL_PUBLIC:
51 result = Scope.PUBLIC;
52 break;
53 case TokenTypes.LITERAL_PROTECTED:
54 result = Scope.PROTECTED;
55 break;
56 case TokenTypes.LITERAL_PRIVATE:
57 result = Scope.PRIVATE;
58 break;
59 default:
60 break;
61 }
62 }
63 return result;
64 }
65
66
67
68
69
70
71
72 public static Scope getScope(DetailAST ast) {
73 return Optional.ofNullable(ast.findFirstToken(TokenTypes.MODIFIERS))
74 .map(ScopeUtil::getDeclaredScopeFromMods)
75 .orElseGet(() -> getDefaultScope(ast));
76 }
77
78
79
80
81
82
83
84
85
86 public static Scope getScopeFromMods(DetailAST aMods) {
87 return Optional.ofNullable(getDeclaredScopeFromMods(aMods))
88 .orElseGet(() -> getDefaultScope(aMods));
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 private static Scope getDefaultScope(DetailAST ast) {
106 final Scope result;
107 if (isInEnumBlock(ast)) {
108 if (ast.getType() == TokenTypes.ENUM_CONSTANT_DEF) {
109 result = Scope.PUBLIC;
110 }
111 else if (ast.getType() == TokenTypes.CTOR_DEF) {
112 result = Scope.PRIVATE;
113 }
114 else {
115 result = Scope.PACKAGE;
116 }
117 }
118 else if (isInInterfaceOrAnnotationBlock(ast)) {
119 result = Scope.PUBLIC;
120 }
121 else {
122 result = Scope.PACKAGE;
123 }
124 return result;
125 }
126
127
128
129
130
131
132
133 public static Scope getSurroundingScope(DetailAST node) {
134 Scope returnValue = null;
135 for (DetailAST token = node;
136 token != null;
137 token = token.getParent()) {
138 final int type = token.getType();
139 if (TokenUtil.isTypeDeclaration(type)) {
140 final Scope tokenScope = getScope(token);
141 if (returnValue == null || returnValue.isIn(tokenScope)) {
142 returnValue = tokenScope;
143 }
144 }
145 else if (type == TokenTypes.LITERAL_NEW) {
146 returnValue = Scope.ANONINNER;
147
148 break;
149 }
150 }
151
152 return returnValue;
153 }
154
155
156
157
158
159
160
161 public static boolean isInClassBlock(DetailAST node) {
162 return isInBlockOf(node, TokenTypes.CLASS_DEF);
163 }
164
165
166
167
168
169
170
171 public static boolean isInRecordBlock(DetailAST node) {
172 return isInBlockOf(node, TokenTypes.RECORD_DEF);
173 }
174
175
176
177
178
179
180
181 public static boolean isInInterfaceBlock(DetailAST node) {
182 return isInBlockOf(node, TokenTypes.INTERFACE_DEF);
183 }
184
185
186
187
188
189
190
191 public static boolean isInAnnotationBlock(DetailAST node) {
192 return isInBlockOf(node, TokenTypes.ANNOTATION_DEF);
193 }
194
195
196
197
198
199
200
201
202 private static boolean isInBlockOf(DetailAST node, int tokenType) {
203 boolean returnValue = false;
204
205
206 for (DetailAST token = node.getParent();
207 token != null && !returnValue;
208 token = token.getParent()) {
209 if (token.getType() == tokenType) {
210 returnValue = true;
211 }
212 else if (token.getType() == TokenTypes.LITERAL_NEW
213 || TokenUtil.isTypeDeclaration(token.getType())) {
214 break;
215 }
216 }
217
218 return returnValue;
219 }
220
221
222
223
224
225
226
227
228
229 public static boolean isInInterfaceOrAnnotationBlock(DetailAST node) {
230 return isInInterfaceBlock(node) || isInAnnotationBlock(node);
231 }
232
233
234
235
236
237
238
239 public static boolean isInEnumBlock(DetailAST node) {
240 boolean returnValue = false;
241
242
243 for (DetailAST token = node.getParent();
244 token != null; token = token.getParent()) {
245 if (TokenUtil.isOfType(token, TokenTypes.INTERFACE_DEF,
246 TokenTypes.ANNOTATION_DEF, TokenTypes.CLASS_DEF,
247 TokenTypes.LITERAL_NEW, TokenTypes.ENUM_DEF)) {
248 returnValue = token.getType() == TokenTypes.ENUM_DEF;
249 break;
250 }
251 }
252
253 return returnValue;
254 }
255
256
257
258
259
260
261
262
263 public static boolean isInCodeBlock(DetailAST node) {
264 boolean returnValue = false;
265 final int[] tokenTypes = {
266 TokenTypes.METHOD_DEF,
267 TokenTypes.CTOR_DEF,
268 TokenTypes.INSTANCE_INIT,
269 TokenTypes.STATIC_INIT,
270 TokenTypes.LAMBDA,
271 TokenTypes.COMPACT_CTOR_DEF,
272 };
273
274
275 for (DetailAST token = node.getParent();
276 token != null;
277 token = token.getParent()) {
278 if (TokenUtil.isOfType(token, tokenTypes)) {
279 returnValue = true;
280 break;
281 }
282 }
283
284 return returnValue;
285 }
286
287
288
289
290
291
292
293 public static boolean isOuterMostType(DetailAST node) {
294 boolean returnValue = true;
295 for (DetailAST parent = node.getParent();
296 parent != null;
297 parent = parent.getParent()) {
298 if (TokenUtil.isTypeDeclaration(parent.getType())) {
299 returnValue = false;
300 break;
301 }
302 }
303
304 return returnValue;
305 }
306
307
308
309
310
311
312
313
314
315 public static boolean isLocalVariableDef(DetailAST node) {
316 final boolean localVariableDef;
317
318 if (node.getType() == TokenTypes.VARIABLE_DEF) {
319 final DetailAST parent = node.getParent();
320 localVariableDef = TokenUtil.isOfType(parent, TokenTypes.SLIST,
321 TokenTypes.FOR_INIT, TokenTypes.FOR_EACH_CLAUSE);
322 }
323
324 else if (node.getType() == TokenTypes.RESOURCE) {
325 localVariableDef = node.getChildCount() > 1;
326 }
327
328
329 else if (node.getType() == TokenTypes.PARAMETER_DEF) {
330 final DetailAST parent = node.getParent();
331 localVariableDef = parent.getType() == TokenTypes.LITERAL_CATCH;
332 }
333
334 else {
335 localVariableDef = false;
336 }
337
338 return localVariableDef;
339 }
340
341
342
343
344
345
346
347
348
349 public static boolean isClassFieldDef(DetailAST node) {
350 return node.getType() == TokenTypes.VARIABLE_DEF
351 && !isLocalVariableDef(node);
352 }
353
354
355
356
357
358
359
360
361 public static boolean isInScope(DetailAST ast, Scope scope) {
362 final Scope surroundingScopeOfAstToken = getSurroundingScope(ast);
363 return surroundingScopeOfAstToken == scope;
364 }
365
366 }