1 package com.puppycrawl.tools.checkstyle.api; 20 21 import antlr.collections.AST; 22 23 29 public final class ScopeUtils 30 { 31 33 private ScopeUtils() 34 { 35 } 36 38 44 public static Scope getScopeFromMods(DetailAST aMods) 45 { 46 Scope retVal = Scope.PACKAGE; for (AST token = aMods.getFirstChild(); 48 token != null; 49 token = token.getNextSibling()) 50 { 51 if ("public".equals(token.getText())) { 52 retVal = Scope.PUBLIC; 53 break; 54 } 55 else if ("protected".equals(token.getText())) { 56 retVal = Scope.PROTECTED; 57 break; 58 } 59 else if ("private".equals(token.getText())) { 60 retVal = Scope.PRIVATE; 61 break; 62 } 63 } 64 return retVal; 65 } 66 67 72 public static Scope getSurroundingScope(DetailAST aAST) 73 { 74 Scope retVal = null; 75 for (DetailAST token = aAST.getParent(); 76 token != null; 77 token = token.getParent()) 78 { 79 final int type = token.getType(); 80 if ((type == TokenTypes.CLASS_DEF) 81 || (type == TokenTypes.INTERFACE_DEF) 82 || (type == TokenTypes.ANNOTATION_DEF) 83 || (type == TokenTypes.ENUM_DEF)) 84 { 85 final DetailAST mods = 86 token.findFirstToken(TokenTypes.MODIFIERS); 87 final Scope modScope = ScopeUtils.getScopeFromMods(mods); 88 if ((retVal == null) || (retVal.isIn(modScope))) { 89 retVal = modScope; 90 } 91 } 92 else if (type == TokenTypes.LITERAL_NEW) { 93 retVal = Scope.ANONINNER; 94 break; } 96 } 97 98 return retVal; 99 } 100 101 108 public static boolean inInterfaceBlock(DetailAST aAST) 109 { 110 boolean retVal = false; 111 112 for (DetailAST token = aAST.getParent(); 114 token != null; 115 token = token.getParent()) 116 { 117 final int type = token.getType(); 118 if ((type == TokenTypes.CLASS_DEF) 119 || (type == TokenTypes.ENUM_DEF) 120 || (type == TokenTypes.ANNOTATION_DEF)) 121 { 122 break; } 124 else if (type == TokenTypes.LITERAL_NEW) { 125 break; } 127 else if (type == TokenTypes.INTERFACE_DEF) { 128 retVal = true; 129 break; 130 } 131 } 132 133 return retVal; 134 } 135 136 143 public static boolean inAnnotationBlock(DetailAST aAST) 144 { 145 boolean retVal = false; 146 147 for (DetailAST token = aAST.getParent(); 149 token != null; 150 token = token.getParent()) 151 { 152 final int type = token.getType(); 153 if ((type == TokenTypes.CLASS_DEF) 154 || (type == TokenTypes.ENUM_DEF) 155 || (type == TokenTypes.INTERFACE_DEF)) 156 { 157 break; } 159 else if (type == TokenTypes.LITERAL_NEW) { 160 break; } 162 else if (type == TokenTypes.ANNOTATION_DEF) { 163 retVal = true; 164 break; 165 } 166 } 167 168 return retVal; 169 } 170 171 179 public static boolean inInterfaceOrAnnotationBlock(DetailAST aAST) 180 { 181 return inInterfaceBlock(aAST) || inAnnotationBlock(aAST); 182 } 183 184 191 public static boolean inEnumBlock(DetailAST aAST) 192 { 193 boolean retVal = false; 194 195 for (DetailAST token = aAST.getParent(); 197 token != null; 198 token = token.getParent()) 199 { 200 final int type = token.getType(); 201 if ((type == TokenTypes.INTERFACE_DEF) 202 || (type == TokenTypes.ANNOTATION_DEF) 203 || (type == TokenTypes.CLASS_DEF)) 204 { 205 break; } 207 else if (type == TokenTypes.LITERAL_NEW) { 208 break; } 210 else if (type == TokenTypes.ENUM_DEF) { 211 retVal = true; 212 break; 213 } 214 } 215 216 return retVal; 217 } 218 219 226 public static boolean inCodeBlock(DetailAST aAST) 227 { 228 boolean retVal = false; 229 230 for (DetailAST token = aAST.getParent(); 232 token != null; 233 token = token.getParent()) 234 { 235 final int type = token.getType(); 236 if ((type == TokenTypes.METHOD_DEF) 237 || (type == TokenTypes.CTOR_DEF) 238 || (type == TokenTypes.INSTANCE_INIT) 239 || (type == TokenTypes.STATIC_INIT)) 240 { 241 retVal = true; 242 break; 243 } 244 } 245 246 return retVal; 247 } 248 249 255 public static boolean isOuterMostType(DetailAST aAST) 256 { 257 boolean retVal = true; 258 for (DetailAST parent = aAST.getParent(); 259 parent != null; 260 parent = parent.getParent()) 261 { 262 if ((parent.getType() == TokenTypes.CLASS_DEF) 263 || (parent.getType() == TokenTypes.INTERFACE_DEF) 264 || (parent.getType() == TokenTypes.ANNOTATION_DEF) 265 || (parent.getType() == TokenTypes.ENUM_DEF)) 266 { 267 retVal = false; 268 break; 269 } 270 } 271 272 return retVal; 273 } 274 275 282 public static boolean isLocalVariableDef(DetailAST aAST) 283 { 284 if (aAST.getType() == TokenTypes.VARIABLE_DEF) { 286 final DetailAST parent = aAST.getParent(); 287 if (parent != null) { 288 final int type = parent.getType(); 289 return (type == TokenTypes.SLIST) 290 || (type == TokenTypes.FOR_INIT) 291 || (type == TokenTypes.FOR_EACH_CLAUSE); 292 } 293 } 294 else if (aAST.getType() == TokenTypes.PARAMETER_DEF) { 296 final DetailAST parent = aAST.getParent(); 297 if (parent != null) { 298 return (parent.getType() == TokenTypes.LITERAL_CATCH); 299 } 300 } 301 return false; 302 } 303 } 304 | Popular Tags |