changeset 10950 | e87b50888909 |
parent 9300 | c2de4dd9853b |
child 11141 | 7d112a1ecd0f |
10949:42f7cc0468dd | 10950:e87b50888909 |
---|---|
34 |
34 |
35 import com.sun.tools.javac.code.Symbol.*; |
35 import com.sun.tools.javac.code.Symbol.*; |
36 import com.sun.tools.javac.tree.JCTree.*; |
36 import com.sun.tools.javac.tree.JCTree.*; |
37 |
37 |
38 import static com.sun.tools.javac.code.Flags.*; |
38 import static com.sun.tools.javac.code.Flags.*; |
39 import static com.sun.tools.javac.code.Flags.ANNOTATION; |
|
40 import static com.sun.tools.javac.tree.JCTree.Tag.*; |
|
39 |
41 |
40 /** Prints out a tree as an indented Java source program. |
42 /** Prints out a tree as an indented Java source program. |
41 * |
43 * |
42 * <p><b>This is NOT part of any supported API. |
44 * <p><b>This is NOT part of any supported API. |
43 * If you write code that depends on this, you do so at your own risk. |
45 * If you write code that depends on this, you do so at your own risk. |
308 print("}"); |
310 print("}"); |
309 } |
311 } |
310 |
312 |
311 /** Is the given tree an enumerator definition? */ |
313 /** Is the given tree an enumerator definition? */ |
312 boolean isEnumerator(JCTree t) { |
314 boolean isEnumerator(JCTree t) { |
313 return t.getTag() == JCTree.VARDEF && (((JCVariableDecl) t).mods.flags & ENUM) != 0; |
315 return t.hasTag(VARDEF) && (((JCVariableDecl) t).mods.flags & ENUM) != 0; |
314 } |
316 } |
315 |
317 |
316 /** Print unit consisting of package clause and import statements in toplevel, |
318 /** Print unit consisting of package clause and import statements in toplevel, |
317 * followed by class definition. if class definition == null, |
319 * followed by class definition. if class definition == null, |
318 * print all definitions in toplevel. |
320 * print all definitions in toplevel. |
329 print(";"); |
331 print(";"); |
330 println(); |
332 println(); |
331 } |
333 } |
332 boolean firstImport = true; |
334 boolean firstImport = true; |
333 for (List<JCTree> l = tree.defs; |
335 for (List<JCTree> l = tree.defs; |
334 l.nonEmpty() && (cdef == null || l.head.getTag() == JCTree.IMPORT); |
336 l.nonEmpty() && (cdef == null || l.head.hasTag(IMPORT)); |
335 l = l.tail) { |
337 l = l.tail) { |
336 if (l.head.getTag() == JCTree.IMPORT) { |
338 if (l.head.hasTag(IMPORT)) { |
337 JCImport imp = (JCImport)l.head; |
339 JCImport imp = (JCImport)l.head; |
338 Name name = TreeInfo.name(imp.qualid); |
340 Name name = TreeInfo.name(imp.qualid); |
339 if (name == name.table.names.asterisk || |
341 if (name == name.table.names.asterisk || |
340 cdef == null || |
342 cdef == null || |
341 isUsed(TreeInfo.symbol(imp.qualid), cdef)) { |
343 isUsed(TreeInfo.symbol(imp.qualid), cdef)) { |
482 printDocComment(tree); |
484 printDocComment(tree); |
483 if ((tree.mods.flags & ENUM) != 0) { |
485 if ((tree.mods.flags & ENUM) != 0) { |
484 print("/*public static final*/ "); |
486 print("/*public static final*/ "); |
485 print(tree.name); |
487 print(tree.name); |
486 if (tree.init != null) { |
488 if (tree.init != null) { |
487 if (sourceOutput && tree.init.getTag() == JCTree.NEWCLASS) { |
489 if (sourceOutput && tree.init.hasTag(NEWCLASS)) { |
488 print(" /*enum*/ "); |
490 print(" /*enum*/ "); |
489 JCNewClass init = (JCNewClass) tree.init; |
491 JCNewClass init = (JCNewClass) tree.init; |
490 if (init.args != null && init.args.nonEmpty()) { |
492 if (init.args != null && init.args.nonEmpty()) { |
491 print("("); |
493 print("("); |
492 print(init.args); |
494 print(init.args); |
543 try { |
545 try { |
544 print("do "); |
546 print("do "); |
545 printStat(tree.body); |
547 printStat(tree.body); |
546 align(); |
548 align(); |
547 print(" while "); |
549 print(" while "); |
548 if (tree.cond.getTag() == JCTree.PARENS) { |
550 if (tree.cond.hasTag(PARENS)) { |
549 printExpr(tree.cond); |
551 printExpr(tree.cond); |
550 } else { |
552 } else { |
551 print("("); |
553 print("("); |
552 printExpr(tree.cond); |
554 printExpr(tree.cond); |
553 print(")"); |
555 print(")"); |
559 } |
561 } |
560 |
562 |
561 public void visitWhileLoop(JCWhileLoop tree) { |
563 public void visitWhileLoop(JCWhileLoop tree) { |
562 try { |
564 try { |
563 print("while "); |
565 print("while "); |
564 if (tree.cond.getTag() == JCTree.PARENS) { |
566 if (tree.cond.hasTag(PARENS)) { |
565 printExpr(tree.cond); |
567 printExpr(tree.cond); |
566 } else { |
568 } else { |
567 print("("); |
569 print("("); |
568 printExpr(tree.cond); |
570 printExpr(tree.cond); |
569 print(")"); |
571 print(")"); |
577 |
579 |
578 public void visitForLoop(JCForLoop tree) { |
580 public void visitForLoop(JCForLoop tree) { |
579 try { |
581 try { |
580 print("for ("); |
582 print("for ("); |
581 if (tree.init.nonEmpty()) { |
583 if (tree.init.nonEmpty()) { |
582 if (tree.init.head.getTag() == JCTree.VARDEF) { |
584 if (tree.init.head.hasTag(VARDEF)) { |
583 printExpr(tree.init.head); |
585 printExpr(tree.init.head); |
584 for (List<JCStatement> l = tree.init.tail; l.nonEmpty(); l = l.tail) { |
586 for (List<JCStatement> l = tree.init.tail; l.nonEmpty(); l = l.tail) { |
585 JCVariableDecl vdef = (JCVariableDecl)l.head; |
587 JCVariableDecl vdef = (JCVariableDecl)l.head; |
586 print(", " + vdef.name + " = "); |
588 print(", " + vdef.name + " = "); |
587 printExpr(vdef.init); |
589 printExpr(vdef.init); |
624 } |
626 } |
625 |
627 |
626 public void visitSwitch(JCSwitch tree) { |
628 public void visitSwitch(JCSwitch tree) { |
627 try { |
629 try { |
628 print("switch "); |
630 print("switch "); |
629 if (tree.selector.getTag() == JCTree.PARENS) { |
631 if (tree.selector.hasTag(PARENS)) { |
630 printExpr(tree.selector); |
632 printExpr(tree.selector); |
631 } else { |
633 } else { |
632 print("("); |
634 print("("); |
633 printExpr(tree.selector); |
635 printExpr(tree.selector); |
634 print(")"); |
636 print(")"); |
663 } |
665 } |
664 |
666 |
665 public void visitSynchronized(JCSynchronized tree) { |
667 public void visitSynchronized(JCSynchronized tree) { |
666 try { |
668 try { |
667 print("synchronized "); |
669 print("synchronized "); |
668 if (tree.lock.getTag() == JCTree.PARENS) { |
670 if (tree.lock.hasTag(PARENS)) { |
669 printExpr(tree.lock); |
671 printExpr(tree.lock); |
670 } else { |
672 } else { |
671 print("("); |
673 print("("); |
672 printExpr(tree.lock); |
674 printExpr(tree.lock); |
673 print(")"); |
675 print(")"); |
734 } |
736 } |
735 |
737 |
736 public void visitIf(JCIf tree) { |
738 public void visitIf(JCIf tree) { |
737 try { |
739 try { |
738 print("if "); |
740 print("if "); |
739 if (tree.cond.getTag() == JCTree.PARENS) { |
741 if (tree.cond.hasTag(PARENS)) { |
740 printExpr(tree.cond); |
742 printExpr(tree.cond); |
741 } else { |
743 } else { |
742 print("("); |
744 print("("); |
743 printExpr(tree.cond); |
745 printExpr(tree.cond); |
744 print(")"); |
746 print(")"); |
821 } |
823 } |
822 |
824 |
823 public void visitApply(JCMethodInvocation tree) { |
825 public void visitApply(JCMethodInvocation tree) { |
824 try { |
826 try { |
825 if (!tree.typeargs.isEmpty()) { |
827 if (!tree.typeargs.isEmpty()) { |
826 if (tree.meth.getTag() == JCTree.SELECT) { |
828 if (tree.meth.hasTag(SELECT)) { |
827 JCFieldAccess left = (JCFieldAccess)tree.meth; |
829 JCFieldAccess left = (JCFieldAccess)tree.meth; |
828 printExpr(left.selected); |
830 printExpr(left.selected); |
829 print(".<"); |
831 print(".<"); |
830 printExprs(tree.typeargs); |
832 printExprs(tree.typeargs); |
831 print(">" + left.name); |
833 print(">" + left.name); |
880 public void visitNewArray(JCNewArray tree) { |
882 public void visitNewArray(JCNewArray tree) { |
881 try { |
883 try { |
882 if (tree.elemtype != null) { |
884 if (tree.elemtype != null) { |
883 print("new "); |
885 print("new "); |
884 JCTree elem = tree.elemtype; |
886 JCTree elem = tree.elemtype; |
885 if (elem.getTag() == JCTree.TYPEARRAY) |
887 if (elem.hasTag(TYPEARRAY)) |
886 printBaseElementType((JCArrayTypeTree) elem); |
888 printBaseElementType((JCArrayTypeTree) elem); |
887 else |
889 else |
888 printExpr(elem); |
890 printExpr(elem); |
889 for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) { |
891 for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) { |
890 print("["); |
892 print("["); |
925 } catch (IOException e) { |
927 } catch (IOException e) { |
926 throw new UncheckedIOException(e); |
928 throw new UncheckedIOException(e); |
927 } |
929 } |
928 } |
930 } |
929 |
931 |
930 public String operatorName(int tag) { |
932 public String operatorName(JCTree.Tag tag) { |
931 switch(tag) { |
933 switch(tag) { |
932 case JCTree.POS: return "+"; |
934 case POS: return "+"; |
933 case JCTree.NEG: return "-"; |
935 case NEG: return "-"; |
934 case JCTree.NOT: return "!"; |
936 case NOT: return "!"; |
935 case JCTree.COMPL: return "~"; |
937 case COMPL: return "~"; |
936 case JCTree.PREINC: return "++"; |
938 case PREINC: return "++"; |
937 case JCTree.PREDEC: return "--"; |
939 case PREDEC: return "--"; |
938 case JCTree.POSTINC: return "++"; |
940 case POSTINC: return "++"; |
939 case JCTree.POSTDEC: return "--"; |
941 case POSTDEC: return "--"; |
940 case JCTree.NULLCHK: return "<*nullchk*>"; |
942 case NULLCHK: return "<*nullchk*>"; |
941 case JCTree.OR: return "||"; |
943 case OR: return "||"; |
942 case JCTree.AND: return "&&"; |
944 case AND: return "&&"; |
943 case JCTree.EQ: return "=="; |
945 case EQ: return "=="; |
944 case JCTree.NE: return "!="; |
946 case NE: return "!="; |
945 case JCTree.LT: return "<"; |
947 case LT: return "<"; |
946 case JCTree.GT: return ">"; |
948 case GT: return ">"; |
947 case JCTree.LE: return "<="; |
949 case LE: return "<="; |
948 case JCTree.GE: return ">="; |
950 case GE: return ">="; |
949 case JCTree.BITOR: return "|"; |
951 case BITOR: return "|"; |
950 case JCTree.BITXOR: return "^"; |
952 case BITXOR: return "^"; |
951 case JCTree.BITAND: return "&"; |
953 case BITAND: return "&"; |
952 case JCTree.SL: return "<<"; |
954 case SL: return "<<"; |
953 case JCTree.SR: return ">>"; |
955 case SR: return ">>"; |
954 case JCTree.USR: return ">>>"; |
956 case USR: return ">>>"; |
955 case JCTree.PLUS: return "+"; |
957 case PLUS: return "+"; |
956 case JCTree.MINUS: return "-"; |
958 case MINUS: return "-"; |
957 case JCTree.MUL: return "*"; |
959 case MUL: return "*"; |
958 case JCTree.DIV: return "/"; |
960 case DIV: return "/"; |
959 case JCTree.MOD: return "%"; |
961 case MOD: return "%"; |
960 default: throw new Error(); |
962 default: throw new Error(); |
961 } |
963 } |
962 } |
964 } |
963 |
965 |
964 public void visitAssignop(JCAssignOp tree) { |
966 public void visitAssignop(JCAssignOp tree) { |
965 try { |
967 try { |
966 open(prec, TreeInfo.assignopPrec); |
968 open(prec, TreeInfo.assignopPrec); |
967 printExpr(tree.lhs, TreeInfo.assignopPrec + 1); |
969 printExpr(tree.lhs, TreeInfo.assignopPrec + 1); |
968 print(" " + operatorName(tree.getTag() - JCTree.ASGOffset) + "= "); |
970 print(" " + operatorName(tree.getTag().noAssignOp()) + "= "); |
969 printExpr(tree.rhs, TreeInfo.assignopPrec); |
971 printExpr(tree.rhs, TreeInfo.assignopPrec); |
970 close(prec, TreeInfo.assignopPrec); |
972 close(prec, TreeInfo.assignopPrec); |
971 } catch (IOException e) { |
973 } catch (IOException e) { |
972 throw new UncheckedIOException(e); |
974 throw new UncheckedIOException(e); |
973 } |
975 } |
976 public void visitUnary(JCUnary tree) { |
978 public void visitUnary(JCUnary tree) { |
977 try { |
979 try { |
978 int ownprec = TreeInfo.opPrec(tree.getTag()); |
980 int ownprec = TreeInfo.opPrec(tree.getTag()); |
979 String opname = operatorName(tree.getTag()); |
981 String opname = operatorName(tree.getTag()); |
980 open(prec, ownprec); |
982 open(prec, ownprec); |
981 if (tree.getTag() <= JCTree.PREDEC) { |
983 if (!tree.getTag().isPostUnaryOp()) { |
982 print(opname); |
984 print(opname); |
983 printExpr(tree.arg, ownprec); |
985 printExpr(tree.arg, ownprec); |
984 } else { |
986 } else { |
985 printExpr(tree.arg, ownprec); |
987 printExpr(tree.arg, ownprec); |
986 print(opname); |
988 print(opname); |
1151 private void printBrackets(JCArrayTypeTree tree) throws IOException { |
1153 private void printBrackets(JCArrayTypeTree tree) throws IOException { |
1152 JCTree elem; |
1154 JCTree elem; |
1153 while (true) { |
1155 while (true) { |
1154 elem = tree.elemtype; |
1156 elem = tree.elemtype; |
1155 print("[]"); |
1157 print("[]"); |
1156 if (elem.getTag() != JCTree.TYPEARRAY) break; |
1158 if (!elem.hasTag(TYPEARRAY)) break; |
1157 tree = (JCArrayTypeTree) elem; |
1159 tree = (JCArrayTypeTree) elem; |
1158 } |
1160 } |
1159 } |
1161 } |
1160 |
1162 |
1161 public void visitTypeApply(JCTypeApply tree) { |
1163 public void visitTypeApply(JCTypeApply tree) { |