/** * Print unit consisting of package clause and import statements in toplevel, followed by class * definition. if class definition == null, print all definitions in toplevel. * * @param tree The toplevel tree * @param cdef The class definition, which is assumed to be part of the toplevel tree. */ public void printUnit(JCCompilationUnit tree, JCClassDecl cdef) throws IOException { docComments = tree.docComments; printDocComment(tree); if (tree.pid != null) { print("package "); printExpr(tree.pid); print(";"); println(); } boolean firstImport = true; for (List<JCTree> l = tree.defs; l.nonEmpty() && (cdef == null || l.head.getTag() == JCTree.IMPORT); l = l.tail) { if (l.head.getTag() == JCTree.IMPORT) { JCImport imp = (JCImport) l.head; Name name = TreeInfo.name(imp.qualid); if (name == name.table.asterisk || cdef == null || isUsed(TreeInfo.symbol(imp.qualid), cdef)) { if (firstImport) { firstImport = false; println(); } printStat(imp); } } else { printStat(l.head); } } if (cdef != null) { printStat(cdef); println(); } }