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 com.puppycrawl.tools.checkstyle.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.Scope;
24 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25
26
27
28
29
30 public final class ScopeUtil {
31
32
33 private ScopeUtil() {
34 }
35
36
37
38
39
40
41
42 public static Scope getScopeFromMods(DetailAST aMods) {
43
44 Scope returnValue = Scope.PACKAGE;
45 for (DetailAST token = aMods.getFirstChild(); token != null
46 && returnValue == Scope.PACKAGE;
47 token = token.getNextSibling()) {
48 if ("public".equals(token.getText())) {
49 returnValue = Scope.PUBLIC;
50 }
51 else if ("protected".equals(token.getText())) {
52 returnValue = Scope.PROTECTED;
53 }
54 else if ("private".equals(token.getText())) {
55 returnValue = Scope.PRIVATE;
56 }
57 }
58 return returnValue;
59 }
60
61
62
63
64
65
66 public static Scope getSurroundingScope(DetailAST node) {
67 Scope returnValue = null;
68 for (DetailAST token = node.getParent();
69 token != null;
70 token = token.getParent()) {
71 final int type = token.getType();
72 if (type == TokenTypes.CLASS_DEF
73 || type == TokenTypes.INTERFACE_DEF
74 || type == TokenTypes.ANNOTATION_DEF
75 || type == TokenTypes.ENUM_DEF) {
76 final DetailAST mods =
77 token.findFirstToken(TokenTypes.MODIFIERS);
78 final Scope modScope = getScopeFromMods(mods);
79 if (returnValue == null || returnValue.isIn(modScope)) {
80 returnValue = modScope;
81 }
82 }
83 else if (type == TokenTypes.LITERAL_NEW) {
84 returnValue = Scope.ANONINNER;
85
86 break;
87 }
88 }
89
90 return returnValue;
91 }
92
93
94
95
96
97
98
99 public static boolean isInClassBlock(DetailAST node) {
100 return isInBlockOf(node, TokenTypes.CLASS_DEF);
101 }
102
103
104
105
106
107
108
109 public static boolean isInInterfaceBlock(DetailAST node) {
110 return isInBlockOf(node, TokenTypes.INTERFACE_DEF);
111 }
112
113
114
115
116
117
118
119 public static boolean isInAnnotationBlock(DetailAST node) {
120 return isInBlockOf(node, TokenTypes.ANNOTATION_DEF);
121 }
122
123
124
125
126
127
128
129
130 private static boolean isInBlockOf(DetailAST node, int tokenType) {
131 boolean returnValue = false;
132
133
134 for (DetailAST token = node.getParent();
135 token != null && !returnValue;
136 token = token.getParent()) {
137 final int type = token.getType();
138 if (type == tokenType) {
139 returnValue = true;
140 }
141 else if (type == TokenTypes.CLASS_DEF
142 || type == TokenTypes.ENUM_DEF
143 || type == TokenTypes.INTERFACE_DEF
144 || type == TokenTypes.ANNOTATION_DEF
145 || type == TokenTypes.LITERAL_NEW) {
146 break;
147 }
148 }
149
150 return returnValue;
151 }
152
153
154
155
156
157
158
159
160
161 public static boolean isInInterfaceOrAnnotationBlock(DetailAST node) {
162 return isInInterfaceBlock(node) || isInAnnotationBlock(node);
163 }
164
165
166
167
168
169
170
171 public static boolean isInEnumBlock(DetailAST node) {
172 boolean returnValue = false;
173
174
175 for (DetailAST token = node.getParent();
176 token != null && !returnValue;
177 token = token.getParent()) {
178 final int type = token.getType();
179 if (type == TokenTypes.ENUM_DEF) {
180 returnValue = true;
181 }
182 else if (type == TokenTypes.INTERFACE_DEF
183 || type == TokenTypes.ANNOTATION_DEF
184 || type == TokenTypes.CLASS_DEF
185 || type == TokenTypes.LITERAL_NEW) {
186 break;
187 }
188 }
189
190 return returnValue;
191 }
192
193
194
195
196
197
198
199
200 public static boolean isInCodeBlock(DetailAST node) {
201 boolean returnValue = false;
202
203
204 for (DetailAST token = node.getParent();
205 token != null;
206 token = token.getParent()) {
207 final int type = token.getType();
208 if (type == TokenTypes.METHOD_DEF
209 || type == TokenTypes.CTOR_DEF
210 || type == TokenTypes.INSTANCE_INIT
211 || type == TokenTypes.STATIC_INIT
212 || type == TokenTypes.LAMBDA) {
213 returnValue = true;
214 break;
215 }
216 }
217
218 return returnValue;
219 }
220
221
222
223
224
225
226
227 public static boolean isOuterMostType(DetailAST node) {
228 boolean returnValue = true;
229 for (DetailAST parent = node.getParent();
230 parent != null;
231 parent = parent.getParent()) {
232 if (parent.getType() == TokenTypes.CLASS_DEF
233 || parent.getType() == TokenTypes.INTERFACE_DEF
234 || parent.getType() == TokenTypes.ANNOTATION_DEF
235 || parent.getType() == TokenTypes.ENUM_DEF) {
236 returnValue = false;
237 break;
238 }
239 }
240
241 return returnValue;
242 }
243
244
245
246
247
248
249
250
251 public static boolean isLocalVariableDef(DetailAST node) {
252 boolean localVariableDef = false;
253
254 if (node.getType() == TokenTypes.VARIABLE_DEF) {
255 final DetailAST parent = node.getParent();
256 final int type = parent.getType();
257 localVariableDef = type == TokenTypes.SLIST
258 || type == TokenTypes.FOR_INIT
259 || type == TokenTypes.FOR_EACH_CLAUSE;
260 }
261
262 if (node.getType() == TokenTypes.PARAMETER_DEF) {
263 final DetailAST parent = node.getParent();
264 localVariableDef = parent.getType() == TokenTypes.LITERAL_CATCH;
265 }
266
267 if (node.getType() == TokenTypes.RESOURCE) {
268 localVariableDef = true;
269 }
270 return localVariableDef;
271 }
272
273
274
275
276
277
278
279
280 public static boolean isClassFieldDef(DetailAST node) {
281 return node.getType() == TokenTypes.VARIABLE_DEF && !isLocalVariableDef(node);
282 }
283
284
285
286
287
288
289
290 public static boolean isInScope(DetailAST ast, Scope scope) {
291 final Scope surroundingScopeOfAstToken = getSurroundingScope(ast);
292 return surroundingScopeOfAstToken == scope;
293 }
294
295 }