@Override public void printExpr(JCTree tree, int prec) throws IOException { EndPosTable endPositions = unit.endPositions; /* * Modifiers, and specifically flags like final, appear to just need weird special * handling. * * Note: we can't use {@code TreeInfo.getEndPos()} or {@code JCTree.getEndPosition()} * here, because they will return the end position of an enclosing AST node for trees * whose real end positions aren't stored. */ int endPos = endPositions.getEndPos(tree); boolean hasRealEndPosition = endPos != Position.NOPOS; if (tree.getKind() != Kind.MODIFIERS && hasRealEndPosition) { writer.append(unitContents.substring(tree.getStartPosition(), endPos)); } else { super.printExpr(tree, prec); } }
/** Returns a {@code String} representation of a statement, including semicolon. */ private static String printStatement(Context context, JCStatement statement) { StringWriter writer = new StringWriter(); try { pretty(context, writer).printStat(statement); } catch (IOException e) { throw new AssertionError("StringWriter cannot throw IOExceptions"); } return writer.toString(); }
/** * Returns a {@code String} representation of a sequence of statements, with semicolons and * newlines. */ private static String printStatements(Context context, Iterable<JCStatement> statements) { StringWriter writer = new StringWriter(); try { pretty(context, writer).printStats(com.sun.tools.javac.util.List.from(statements)); } catch (IOException e) { throw new AssertionError("StringWriter cannot throw IOExceptions"); } return writer.toString(); }
print("{"); println(); indent(); boolean first = true; for (List<JCTree> l = stats; l.nonEmpty(); l = l.tail) { if (isEnumerator(l.head)) { if (!first) { print(","); println(); align(); printStat(l.head); first = false; print(";"); println(); for (List<JCTree> l = stats; l.nonEmpty(); l = l.tail) { if (!isEnumerator(l.head)) { align(); printStat(l.head); println(); undent(); align(); print("}");
printDocComment(tree); if (tree.pid != null) { print("package "); printExpr(tree.pid); print(";"); println(); if (name == name.table.asterisk || cdef == null || isUsed(TreeInfo.symbol(imp.qualid), cdef)) { if (firstImport) { firstImport = false; println(); printStat(imp); printStat(l.head); printStat(cdef); println();
public void visitSwitch(JCSwitch tree) { try { print("switch "); if (tree.selector.tag == JCTree.PARENS) { printExpr(tree.selector); } else { print("("); printExpr(tree.selector); print(")"); } print(" {"); println(); printStats(tree.cases); align(); print("}"); } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitDoLoop(JCDoWhileLoop tree) { try { print("do "); printStat(tree.body); align(); print(" while "); if (tree.cond.tag == JCTree.PARENS) { printExpr(tree.cond); } else { print("("); printExpr(tree.cond); print(")"); } print(";"); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** Convert a tree to a pretty-printed string. */ public String toString() { StringWriter s = new StringWriter(); try { new Pretty(s, false).printExpr(this); } catch (IOException e) { // should never happen, because StringWriter is defined // never to throw any IOExceptions throw new AssertionError(e); } return s.toString(); }
public void visitTry(JCTry tree) { try { print("try "); if (tree.resources.nonEmpty()) { print("("); boolean first = true; for (JCTree var : tree.resources) { if (!first) { println(); indent(); } printStat(var); first = false; } print(") "); } printStat(tree.body); for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) { printStat(l.head); } if (tree.finalizer != null) { print(" finally "); printStat(tree.finalizer); } } catch (IOException e) { throw new UncheckedIOException(e); } }
/** Emit plain Java source for a class. * @param env The attribution environment of the outermost class * containing this class. * @param cdef The class definition to be printed. */ JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException { JavaFileObject outFile = fileManager.getJavaFileForOutput(CLASS_OUTPUT, cdef.sym.flatname.toString(), JavaFileObject.Kind.SOURCE, null); if (inputFiles.contains(outFile)) { log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile); return null; } else { BufferedWriter out = new BufferedWriter(outFile.openWriter()); try { new Pretty(out, true).printUnit(env.toplevel, cdef); if (verbose) printVerbose("wrote.file", outFile); } finally { out.close(); } return outFile; } }
public void visitAnnotation(JCAnnotation tree) { try { print("@"); printExpr(tree.annotationType); print("("); printExprs(tree.args); print(")"); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** Convert a expression-statement tree to a pretty-printed string. */ @Override public String toString() { StringWriter s = new StringWriter(); try { new Pretty(s, false).printStat(this); } catch (IOException e) { // should never happen, because StringWriter is defined // never to throw any IOExceptions throw new AssertionError(e); } return s.toString(); } }
private String operatorName(Tag tag) { // dummy instance, just to access a public method which should be static return new Pretty(null, false).operatorName(tag); }
public void visitAssignop(JCAssignOp tree) { try { open(prec, TreeInfo.assignopPrec); printExpr(tree.lhs, TreeInfo.assignopPrec + 1); print(" " + operatorName(tree.tag - JCTree.ASGOffset) + "= "); printExpr(tree.rhs, TreeInfo.assignopPrec); close(prec, TreeInfo.assignopPrec); } catch (IOException e) { throw new UncheckedIOException(e); } }