@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); } }
public void visitAnnotation(JCAnnotation tree) { try { print("@"); printExpr(tree.annotationType); print("("); printExprs(tree.args); print(")"); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * Generates a {@link SuggestedFix} replacing the specified match (usually of another template) * with this template. */ @Override public Fix replace(ExpressionTemplateMatch match) { Inliner inliner = match.createInliner(); Context context = inliner.getContext(); if (annotations().containsKey(UseImportPolicy.class)) { ImportPolicy.bind(context, annotations().getInstance(UseImportPolicy.class).value()); } else { ImportPolicy.bind(context, ImportPolicy.IMPORT_TOP_LEVEL); } int prec = getPrecedence(match.getLocation(), context); SuggestedFix.Builder fix = SuggestedFix.builder(); try { StringWriter writer = new StringWriter(); pretty(inliner.getContext(), writer).printExpr(expression().inline(inliner), prec); fix.replace(match.getLocation(), writer.toString()); } catch (CouldNotResolveImportException e) { logger.log(SEVERE, "Failure to resolve in replacement", e); } catch (IOException e) { throw new RuntimeException(e); } return addImports(inliner, fix); }
public void visitAnnotation(JCAnnotation tree) { try { print("@"); printExpr(tree.annotationType); print("("); printExprs(tree.args); print(")"); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** Derived visitor method: print statement tree. */ public void printStat(JCTree tree) throws IOException { printExpr(tree, TreeInfo.notExpression); }
/** Derived visitor method: print statement tree. */ public void printStat(JCTree tree) throws IOException { printExpr(tree, TreeInfo.notExpression); }
/** Derived visitor method: print expression tree at minimum precedence level * for expression. */ public void printExpr(JCTree tree) throws IOException { printExpr(tree, TreeInfo.noPrec); }
/** Derived visitor method: print expression tree at minimum precedence level * for expression. */ public void printExpr(JCTree tree) throws IOException { printExpr(tree, TreeInfo.noPrec); }
/** 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(); }
/** Derived visitor method: print list of expression trees, separated by given string. * @param sep the separator string */ public <T extends JCTree> void printExprs(List<T> trees, String sep) throws IOException { if (trees.nonEmpty()) { printExpr(trees.head); for (List<T> l = trees.tail; l.nonEmpty(); l = l.tail) { print(sep); printExpr(l.head); } } }
public void visitExec(JCExpressionStatement tree) { try { printExpr(tree.expr); if (prec == TreeInfo.notExpression) print(";"); } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitThrow(JCThrow tree) { try { print("throw "); printExpr(tree.expr); print(";"); } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitParens(JCParens tree) { try { print("("); printExpr(tree.expr); print(")"); } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitThrow(JCThrow tree) { try { print("throw "); printExpr(tree.expr); print(";"); } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitCatch(JCCatch tree) { try { print(" catch ("); printExpr(tree.param); print(") "); printStat(tree.body); } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitReturn(JCReturn tree) { try { print("return"); if (tree.expr != null) { print(" "); printExpr(tree.expr); } print(";"); } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitImport(JCImport tree) { try { print("import "); if (tree.staticImport) print("static "); printExpr(tree.qualid); print(";"); println(); } catch (IOException e) { throw new UncheckedIOException(e); } }