1 19 package org.netbeans.modules.java.source.builder; 20 21 import org.netbeans.modules.java.source.engine.RootTree; 22 import org.netbeans.api.java.source.query.CommentHandler; 23 24 import com.sun.source.tree.*; 25 import com.sun.source.util.TreeScanner; 26 27 import javax.lang.model.element.Modifier; 28 import javax.lang.model.type.NullType; 29 30 import java.io.PrintStream ; 31 import java.util.List ; 32 import java.util.Set ; 33 import org.netbeans.api.java.source.query.CommentSet; 34 35 38 public final class DumpAST extends TreeScanner<Object ,Object > { 39 CommentHandler comments; 40 PrintStream out; 41 int indent; 42 43 public static void dump(Tree tree, CommentHandler comments, PrintStream out) { 44 tree.accept(new DumpAST(out, comments), null); 45 } 46 47 DumpAST(PrintStream out, CommentHandler comments) { 48 this.out = out; 49 this.comments = comments; 50 indent = 0; 51 } 52 53 private void indent() { 54 for (int i = 0; i < indent; i++) 55 out.print(". "); 56 } 57 58 private void dump(String name, Tree tree) { 59 indent++; 60 indent(); 61 out.print(name + ": "); 62 if (tree != null) { 63 out.println(); 64 indent++; 65 scan(tree, null); 66 indent--; 67 } 68 else 69 out.println("null"); 70 indent--; 71 } 72 73 private void dump(String name, List <? extends Tree> trees) { 74 indent++; 75 indent(); 76 out.print(name); 77 if (trees == null) 78 out.println(": null"); 79 else if (trees.isEmpty()) 80 out.println(" {}"); 81 else { 82 out.println(" {"); 83 indent++; 84 for (Tree t : trees) 85 scan(t, null); 86 indent--; 87 indent(); 88 out.println("}"); 89 } 90 indent--; 91 } 92 93 private void dumpField(String name, Object o) { 94 indent++; 95 indent(); 96 out.println(name + ": " + (o != null ? o.toString() : "null")); 97 indent--; 98 } 99 100 private void dumpFlags(Set <Modifier> flags) { 101 StringBuffer buf = new StringBuffer (); 102 for (Modifier mod : flags) { 103 buf.append(mod.name()); 104 buf.append(' '); 105 } 106 dumpField("flags", buf.toString().trim()); 107 } 108 109 private void header(String s, Tree tree) { 110 indent(); 111 out.println(s); 112 dumpField("pos", Integer.toString(((com.sun.tools.javac.tree.JCTree)tree).pos)); 113 if (comments.hasComments((com.sun.tools.javac.tree.JCTree)tree)) { 114 CommentSet cs = comments.getComments((com.sun.tools.javac.tree.JCTree)tree); 115 dumpField("comments", cs.toString()); 116 } 117 } 118 119 @Override 120 public Object visitCompilationUnit(CompilationUnitTree tree, Object o) { 121 header("CompilationUnit", tree); 122 dump("packageAnnotations", tree.getPackageAnnotations()); 123 dump("pid", tree.getPackageName()); 124 dumpField("sourcefile", tree.getSourceFile()); 125 dumpField("namedImportScope", tree.getImports()); 126 dump("defs", tree.getTypeDecls()); 127 return o; 128 } 129 130 @Override 131 public Object visitImport(ImportTree tree, Object o) { 132 header("Import", tree); 133 dumpField("staticImport", Boolean.toString(tree.isStatic())); 134 dump("qualid", tree.getQualifiedIdentifier()); 135 return o; 136 } 137 138 @Override 139 public Object visitClass(ClassTree tree, Object o) { 140 header("Class", tree); 141 dumpField("name", tree.getSimpleName()); 142 dump("mods", tree.getModifiers()); 143 dump("typarams", tree.getTypeParameters()); 144 dump("extending", tree.getExtendsClause()); 145 dump("implementing", tree.getImplementsClause()); 146 dump("defs", tree.getMembers()); 147 return o; 148 } 149 150 @Override 151 public Object visitMethod(MethodTree tree, Object o) { 152 header("Method", tree); 153 dumpField("name", tree.getName()); 154 dump("mods", tree.getModifiers()); 155 dump("restype", tree.getReturnType()); 156 dump("typarams", tree.getTypeParameters()); 157 dump("params", tree.getParameters()); 158 dump("thrown", tree.getThrows()); 159 dump("defaultValue", tree.getDefaultValue()); 160 dump("body", tree.getBody()); 161 return o; 162 } 163 164 @Override 165 public Object visitVariable(VariableTree tree, Object o) { 166 header("Variable", tree); 167 dumpField("name", tree.getName()); 168 dump("mods", tree.getModifiers()); 169 dump("vartype", tree.getType()); 170 dump("init", tree.getInitializer()); 171 return o; 172 } 173 174 @Override 175 public Object visitEmptyStatement(EmptyStatementTree tree, Object o) { 176 header("EmptyStatement", tree); 177 return o; 178 } 179 180 @Override 181 public Object visitBlock(BlockTree tree, Object o) { 182 header("Block", tree); 183 dumpField("(flags & Static)", tree.isStatic()); 184 dump("stats", tree.getStatements()); 185 return o; 186 } 187 188 @Override 189 public Object visitDoWhileLoop(DoWhileLoopTree tree, Object o) { 190 header("DoWhileLoop", tree); 191 dump("body", tree.getStatement()); 192 dump("cond", tree.getCondition()); 193 return o; 194 } 195 196 @Override 197 public Object visitWhileLoop(WhileLoopTree tree, Object o) { 198 header("WhileLoop", tree); 199 dump("cond", tree.getCondition()); 200 dump("body", tree.getStatement()); 201 return o; 202 } 203 204 @Override 205 public Object visitForLoop(ForLoopTree tree, Object o) { 206 header("ForLoop", tree); 207 dump("init", tree.getInitializer()); 208 dump("cond", tree.getCondition()); 209 dump("step", tree.getUpdate()); 210 dump("body", tree.getStatement()); 211 return o; 212 } 213 214 @Override 215 public Object visitEnhancedForLoop(EnhancedForLoopTree tree, Object o) { 216 header("EnhancedForLoop", tree); 217 dump("var", tree.getVariable()); 218 dump("expr", tree.getExpression()); 219 dump("body", tree.getStatement()); 220 return o; 221 } 222 223 @Override 224 public Object visitLabeledStatement(LabeledStatementTree tree, Object o) { 225 header("LabeledStatement", tree); 226 dumpField("label", tree.getLabel()); 227 dump("body", tree.getStatement()); 228 return o; 229 } 230 231 @Override 232 public Object visitSwitch(SwitchTree tree, Object o) { 233 header("Switch", tree); 234 dump("selector", tree.getExpression()); 235 dump("cases", tree.getCases()); 236 return o; 237 } 238 239 @Override 240 public Object visitCase(CaseTree tree, Object o) { 241 header("Case", tree); 242 dump("pat", tree.getExpression()); 243 dump("stats", tree.getStatements()); 244 return o; 245 } 246 247 @Override 248 public Object visitSynchronized(SynchronizedTree tree, Object o) { 249 header("Synchronized", tree); 250 dump("lock", tree.getExpression()); 251 dump("body", tree.getBlock()); 252 return o; 253 } 254 255 @Override 256 public Object visitTry(TryTree tree, Object o) { 257 header("Try", tree); 258 dump("body", tree.getBlock()); 259 dump("catchers", tree.getCatches()); 260 dump("finalizer", tree.getFinallyBlock()); 261 return o; 262 } 263 264 @Override 265 public Object visitCatch(CatchTree tree, Object o) { 266 header("Catch", tree); 267 dump("param", tree.getParameter()); 268 dump("body", tree.getBlock()); 269 return o; 270 } 271 272 @Override 273 public Object visitConditionalExpression(ConditionalExpressionTree tree, Object o) { 274 header("ConditionalExpression", tree); 275 dump("cond", tree.getCondition()); 276 dump("truepart", tree.getTrueExpression()); 277 dump("falsepart", tree.getFalseExpression()); 278 return o; 279 } 280 281 @Override 282 public Object visitIf(IfTree tree, Object o) { 283 header("If", tree); 284 dump("cond", tree.getCondition()); 285 dump("thenpart", tree.getThenStatement()); 286 dump("elsepart", tree.getElseStatement()); 287 return o; 288 } 289 290 @Override 291 public Object visitExpressionStatement(ExpressionStatementTree tree, Object o) { 292 header("ExpressionStatement", tree); 293 dump("expr", tree.getExpression()); 294 return o; 295 } 296 297 @Override 298 public Object visitBreak(BreakTree tree, Object o) { 299 header("Break", tree); 300 dumpField("label", tree.getLabel()); 301 return o; 302 } 303 304 @Override 305 public Object visitContinue(ContinueTree tree, Object o) { 306 header("Continue", tree); 307 dumpField("label", tree.getLabel()); 308 return o; 309 } 310 311 @Override 312 public Object visitReturn(ReturnTree tree, Object o) { 313 header("Return", tree); 314 dump("expr", tree.getExpression()); 315 return o; 316 } 317 318 @Override 319 public Object visitThrow(ThrowTree tree, Object o) { 320 header("Throw", tree); 321 dump("expr", tree.getExpression()); 322 return o; 323 } 324 325 @Override 326 public Object visitAssert(AssertTree tree, Object o) { 327 header("Assert", tree); 328 dump("cond", tree.getCondition()); 329 dump("detail", tree.getDetail()); 330 return o; 331 } 332 333 @Override 334 public Object visitMethodInvocation(MethodInvocationTree tree, Object o) { 335 header("MethodInvocation", tree); 336 dump("typeargs", tree.getTypeArguments()); 337 dump("meth", tree.getMethodSelect()); 338 dump("args", tree.getArguments()); 339 return o; 340 } 341 342 @Override 343 public Object visitNewClass(NewClassTree tree, Object o) { 344 header("NewClass", tree); 345 dump("encl", tree.getEnclosingExpression()); 346 dump("typeargs", tree.getTypeArguments()); 347 dump("clazz", tree.getIdentifier()); 348 dump("args", tree.getArguments()); 349 dump("def", tree.getClassBody()); 350 return o; 351 } 352 353 @Override 354 public Object visitNewArray(NewArrayTree tree, Object o) { 355 header("NewArray", tree); 356 dump("elemtype", tree.getType()); 357 dump("dims", tree.getDimensions()); 358 dump("elems", tree.getInitializers()); 359 return o; 360 } 361 362 @Override 363 public Object visitParenthesized(ParenthesizedTree tree, Object o) { 364 header("Parenthesized", tree); 365 dump("expr", tree.getExpression()); 366 return o; 367 } 368 369 @Override 370 public Object visitAssignment(AssignmentTree tree, Object o) { 371 header("Assignment", tree); 372 dump("lhs", tree.getVariable()); 373 dump("rhs", tree.getExpression()); 374 return o; 375 } 376 377 @Override 378 public Object visitCompoundAssignment(CompoundAssignmentTree tree, Object o) { 379 header("CompoundAssignment", tree); 380 dump("lhs", tree.getVariable()); 381 dump("rhs", tree.getExpression()); 382 return o; 383 } 384 385 @Override 386 public Object visitUnary(UnaryTree tree, Object o) { 387 header("Unary", tree); 388 dump("arg", tree.getExpression()); 389 return o; 390 } 391 392 @Override 393 public Object visitBinary(BinaryTree tree, Object o) { 394 header("Binary", tree); 395 dump("lhs", tree.getLeftOperand()); 396 dump("rhs", tree.getRightOperand()); 397 return o; 398 } 399 400 @Override 401 public Object visitTypeCast(TypeCastTree tree, Object o) { 402 header("TypeCast", tree); 403 dump("clazz", tree.getType()); 404 dump("expr", tree.getExpression()); 405 return o; 406 } 407 408 @Override 409 public Object visitInstanceOf(InstanceOfTree tree, Object o) { 410 header("InstanceOf", tree); 411 dump("expr", tree.getExpression()); 412 dump("clazz", tree.getType()); 413 return o; 414 } 415 416 @Override 417 public Object visitArrayAccess(ArrayAccessTree tree, Object o) { 418 header("ArrayAccess", tree); 419 dump("indexed", tree.getExpression()); 420 dump("index", tree.getIndex()); 421 return o; 422 } 423 424 @Override 425 public Object visitMemberSelect(MemberSelectTree tree, Object o) { 426 header("MemberSelect", tree); 427 dumpField("name", tree.getIdentifier()); 428 dump("selected", tree.getExpression()); 429 return o; 430 } 431 432 @Override 433 public Object visitIdentifier(IdentifierTree tree, Object o) { 434 header("Identifier", tree); 435 dumpField("name", tree.getName()); 436 return o; 437 } 438 439 @Override 440 public Object visitLiteral(LiteralTree tree, Object o) { 441 header("Literal", tree); 442 dumpField("typetag", tree.getKind()); 443 dumpField("value", tree.getValue()); 444 return o; 445 } 446 447 @Override 448 public Object visitPrimitiveType(PrimitiveTypeTree tree, Object o) { 449 header("PrimitiveType", tree); 450 dumpField("typetag", tree.getKind()); 451 return o; 452 } 453 454 @Override 455 public Object visitArrayType(ArrayTypeTree tree, Object o) { 456 header("ArrayType", tree); 457 dump("elemtype", tree.getType()); 458 return o; 459 } 460 461 @Override 462 public Object visitParameterizedType(ParameterizedTypeTree tree, Object o) { 463 header("ParameterizedType", tree); 464 dump("clazz", tree.getType()); 465 dump("arguments", tree.getTypeArguments()); 466 return o; 467 } 468 469 @Override 470 public Object visitTypeParameter(TypeParameterTree tree, Object o) { 471 header("TypeParameter", tree); 472 dumpField("name", tree.getName()); 473 dump("bounds", tree.getBounds()); 474 return o; 475 } 476 477 @Override 478 public Object visitWildcard(WildcardTree tree, Object o) { 479 header("Wildcard", tree); 480 dumpField("kind", tree.getKind()); 481 dump("inner", tree.getBound()); 482 return o; 483 } 484 485 @Override 486 public Object visitModifiers(ModifiersTree tree, Object o) { 487 header("Modifiers", tree); 488 dumpFlags(tree.getFlags()); 489 dump("annotations", tree.getAnnotations()); 490 return o; 491 } 492 493 @Override 494 public Object visitAnnotation(AnnotationTree tree, Object o) { 495 header("Annotation", tree); 496 dump("annotationType", tree.getAnnotationType()); 497 dump("args", tree.getArguments()); 498 return o; 499 } 500 501 @Override 502 public Object visitErroneous(ErroneousTree tree, Object o) { 503 header("Erroneous", tree); 504 return o; 505 } 506 507 @Override 508 public Object visitOther(Tree tree, Object o) { 509 assert false; 510 return o; 511 } 512 } 513 | Popular Tags |