com.sun.tools.javac.tree
Class Pretty

java.lang.Object
  extended by com.sun.tools.javac.tree.JCTree.Visitor
      extended by com.sun.tools.javac.tree.Pretty
Direct Known Subclasses:
MSPTreeCodeSerializer

public class Pretty
extends JCTree.Visitor

Prints out a tree as an indented Java source program.

This is NOT part of any API supported by Sun Microsystems. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.


Nested Class Summary
protected static class Pretty.UncheckedIOException
          Exception to propogate IOException through visitXXX methods
 
Field Summary
(package private)  java.util.Map<JCTree,java.lang.String> docComments
          A hashtable mapping trees to their documentation comments (can be null)
(package private)  Name enclClassName
          The enclosing class name.
protected  java.lang.String lineSep
           
protected  int lmargin
          The current left margin.
(package private)  java.io.Writer out
          The output stream on which trees are printed.
(package private)  int prec
          Visitor argument: the current precedence level.
private  boolean sourceOutput
          Set when we are producing source output.
 int width
          Indentation width (can be reassigned from outside).
 
Constructor Summary
Pretty(java.io.Writer out, boolean sourceOutput)
           
 
Method Summary
protected  void align()
          Align code to be indented to left margin.
protected  void close(int contextPrec, int ownPrec)
          Leave precedence level.
protected  void indent()
          Increase left margin by indentation width.
(package private)  boolean isEnumerator(JCTree t)
          Is the given tree an enumerator definition?
(package private)  boolean isUsed(Symbol t, JCTree cdef)
           
(package private) static int lineEndPos(java.lang.String s, int start)
           
protected  void open(int contextPrec, int ownPrec)
          Enter a new precedence level.
 java.lang.String operatorName(int tag)
           
 void print(java.lang.Object s)
          Print string, replacing all non-ascii character with unicode escapes.
 void printAnnotations(List<JCTree.JCAnnotation> trees)
           
private  void printBaseElementType(JCTree.JCArrayTypeTree tree)
           
 void printBlock(List<? extends JCTree> stats)
          Print a block.
private  void printBrackets(JCTree.JCArrayTypeTree tree)
           
 void printDocComment(JCTree tree)
          Print documentation comment, if it exists
 void printEnumBody(List<JCTree> stats)
          Print a block.
 void printExpr(JCTree tree)
          Derived visitor method: print expression tree at minimum precedence level for expression.
 void printExpr(JCTree tree, int prec)
          Visitor method: print expression tree.
<T extends JCTree>
void
printExprs(List<T> trees)
          Derived visitor method: print list of expression trees, separated by commas.
<T extends JCTree>
void
printExprs(List<T> trees, java.lang.String sep)
          Derived visitor method: print list of expression trees, separated by given string.
 void printFlags(long flags)
          Print a set of modifiers.
 void println()
          Print new line.
 void printStat(JCTree tree)
          Derived visitor method: print statement tree.
 void printStats(List<? extends JCTree> trees)
          Derived visitor method: print list of statements, each on a separate line.
 void printTypeParameters(List<JCTree.JCTypeParameter> trees)
          If type parameter list is non-empty, print it enclosed in "<...>" brackets.
 void printUnit(JCTree.JCCompilationUnit tree, JCTree.JCClassDecl cdef)
          Print unit consisting of package clause and import statements in toplevel, followed by class definition.
 void printVarDefName(JCTree.JCVariableDecl tree)
           
(package private)  void undent()
          Decrease left margin by indentation width.
 void visitAnnotation(JCTree.JCAnnotation tree)
           
 void visitApply(JCTree.JCMethodInvocation tree)
           
 void visitAssert(JCTree.JCAssert tree)
           
 void visitAssign(JCTree.JCAssign tree)
           
 void visitAssignop(JCTree.JCAssignOp tree)
           
 void visitBinary(JCTree.JCBinary tree)
           
 void visitBlock(JCTree.JCBlock tree)
           
 void visitBracketExpr(JCTree.JCBracketExpr tree)
           
 void visitBracketStat(JCTree.JCBracketStat tree)
           
 void visitBreak(JCTree.JCBreak tree)
           
 void visitCase(JCTree.JCCase tree)
           
 void visitCatch(JCTree.JCCatch tree)
           
 void visitClassDef(JCTree.JCClassDecl tree)
           
 void visitConditional(JCTree.JCConditional tree)
           
 void visitContinue(JCTree.JCContinue tree)
           
 void visitDoLoop(JCTree.JCDoWhileLoop tree)
           
 void visitErroneous(JCTree.JCErroneous tree)
           
 void visitEscapeExpr(JCTree.JCEscapeExpr tree)
           
 void visitEscapeStat(JCTree.JCEscapeStat tree)
           
 void visitExec(JCTree.JCExpressionStatement tree)
           
 void visitForeachLoop(JCTree.JCEnhancedForLoop tree)
           
 void visitForLoop(JCTree.JCForLoop tree)
           
 void visitIdent(JCTree.JCIdent tree)
           
 void visitIf(JCTree.JCIf tree)
           
 void visitImport(JCTree.JCImport tree)
           
 void visitIndexed(JCTree.JCArrayAccess tree)
           
 void visitLabelled(JCTree.JCLabeledStatement tree)
           
 void visitLetExpr(JCTree.LetExpr tree)
           
 void visitLiteral(JCTree.JCLiteral tree)
           
 void visitMethodDef(JCTree.JCMethodDecl tree)
           
 void visitModifiers(JCTree.JCModifiers mods)
           
 void visitNewArray(JCTree.JCNewArray tree)
           
 void visitNewClass(JCTree.JCNewClass tree)
           
 void visitParens(JCTree.JCParens tree)
           
 void visitReturn(JCTree.JCReturn tree)
           
 void visitSelect(JCTree.JCFieldAccess tree)
           
 void visitSkip(JCTree.JCSkip tree)
           
 void visitSwitch(JCTree.JCSwitch tree)
           
 void visitSynchronized(JCTree.JCSynchronized tree)
           
 void visitThrow(JCTree.JCThrow tree)
           
 void visitTopLevel(JCTree.JCCompilationUnit tree)
          Visitor methods
 void visitTree(JCTree tree)
           
 void visitTry(JCTree.JCTry tree)
           
 void visitTypeApply(JCTree.JCTypeApply tree)
           
 void visitTypeArray(JCTree.JCArrayTypeTree tree)
           
 void visitTypeBoundKind(JCTree.TypeBoundKind tree)
           
 void visitTypeCast(JCTree.JCTypeCast tree)
           
 void visitTypeIdent(JCTree.JCPrimitiveTypeTree tree)
           
 void visitTypeParameter(JCTree.JCTypeParameter tree)
           
 void visitTypeTest(JCTree.JCInstanceOf tree)
           
 void visitUnary(JCTree.JCUnary tree)
           
 void visitVarDef(JCTree.JCVariableDecl tree)
           
 void visitWhileLoop(JCTree.JCWhileLoop tree)
           
 void visitWildcard(JCTree.JCWildcard tree)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sourceOutput

private final boolean sourceOutput
Set when we are producing source output. If we're not producing source output, we can sometimes give more detail in the output even though that detail would not be valid java soruce.


out

java.io.Writer out
The output stream on which trees are printed.


width

public int width
Indentation width (can be reassigned from outside).


lmargin

protected int lmargin
The current left margin.


enclClassName

Name enclClassName
The enclosing class name.


docComments

java.util.Map<JCTree,java.lang.String> docComments
A hashtable mapping trees to their documentation comments (can be null)


lineSep

protected java.lang.String lineSep

prec

int prec
Visitor argument: the current precedence level.

Constructor Detail

Pretty

public Pretty(java.io.Writer out,
              boolean sourceOutput)
Method Detail

align

protected void align()
              throws java.io.IOException
Align code to be indented to left margin.

Throws:
java.io.IOException

indent

protected void indent()
Increase left margin by indentation width.


undent

void undent()
Decrease left margin by indentation width.


open

protected void open(int contextPrec,
                    int ownPrec)
             throws java.io.IOException
Enter a new precedence level. Emit a `(' if new precedence level is less than precedence level so far.

Parameters:
contextPrec - The precedence level in force so far.
ownPrec - The new precedence level.
Throws:
java.io.IOException

close

protected void close(int contextPrec,
                     int ownPrec)
              throws java.io.IOException
Leave precedence level. Emit a `(' if inner precedence level is less than precedence level we revert to.

Parameters:
contextPrec - The precedence level we revert to.
ownPrec - The inner precedence level.
Throws:
java.io.IOException

print

public void print(java.lang.Object s)
           throws java.io.IOException
Print string, replacing all non-ascii character with unicode escapes.

Throws:
java.io.IOException

println

public void println()
             throws java.io.IOException
Print new line.

Throws:
java.io.IOException

printExpr

public void printExpr(JCTree tree,
                      int prec)
               throws java.io.IOException
Visitor method: print expression tree.

Parameters:
prec - The current precedence level.
Throws:
java.io.IOException

printExpr

public void printExpr(JCTree tree)
               throws java.io.IOException
Derived visitor method: print expression tree at minimum precedence level for expression.

Throws:
java.io.IOException

printStat

public void printStat(JCTree tree)
               throws java.io.IOException
Derived visitor method: print statement tree.

Throws:
java.io.IOException

printExprs

public <T extends JCTree> void printExprs(List<T> trees,
                                          java.lang.String sep)
                throws java.io.IOException
Derived visitor method: print list of expression trees, separated by given string.

Parameters:
sep - the separator string
Throws:
java.io.IOException

printExprs

public <T extends JCTree> void printExprs(List<T> trees)
                throws java.io.IOException
Derived visitor method: print list of expression trees, separated by commas.

Throws:
java.io.IOException

printStats

public void printStats(List<? extends JCTree> trees)
                throws java.io.IOException
Derived visitor method: print list of statements, each on a separate line.

Throws:
java.io.IOException

printFlags

public void printFlags(long flags)
                throws java.io.IOException
Print a set of modifiers.

Throws:
java.io.IOException

printAnnotations

public void printAnnotations(List<JCTree.JCAnnotation> trees)
                      throws java.io.IOException
Throws:
java.io.IOException

printDocComment

public void printDocComment(JCTree tree)
                     throws java.io.IOException
Print documentation comment, if it exists

Parameters:
tree - The tree for which a documentation comment should be printed.
Throws:
java.io.IOException

lineEndPos

static int lineEndPos(java.lang.String s,
                      int start)

printTypeParameters

public void printTypeParameters(List<JCTree.JCTypeParameter> trees)
                         throws java.io.IOException
If type parameter list is non-empty, print it enclosed in "<...>" brackets.

Throws:
java.io.IOException

printBlock

public void printBlock(List<? extends JCTree> stats)
                throws java.io.IOException
Print a block.

Throws:
java.io.IOException

printEnumBody

public void printEnumBody(List<JCTree> stats)
                   throws java.io.IOException
Print a block.

Throws:
java.io.IOException

isEnumerator

boolean isEnumerator(JCTree t)
Is the given tree an enumerator definition?


printUnit

public void printUnit(JCTree.JCCompilationUnit tree,
                      JCTree.JCClassDecl cdef)
               throws java.io.IOException
Print unit consisting of package clause and import statements in toplevel, followed by class definition. if class definition == null, print all definitions in toplevel.

Parameters:
tree - The toplevel tree
cdef - The class definition, which is assumed to be part of the toplevel tree.
Throws:
java.io.IOException

isUsed

boolean isUsed(Symbol t,
               JCTree cdef)

visitTopLevel

public void visitTopLevel(JCTree.JCCompilationUnit tree)
Visitor methods

Overrides:
visitTopLevel in class JCTree.Visitor

visitImport

public void visitImport(JCTree.JCImport tree)
Overrides:
visitImport in class JCTree.Visitor

visitClassDef

public void visitClassDef(JCTree.JCClassDecl tree)
Overrides:
visitClassDef in class JCTree.Visitor

visitMethodDef

public void visitMethodDef(JCTree.JCMethodDecl tree)
Overrides:
visitMethodDef in class JCTree.Visitor

visitVarDef

public void visitVarDef(JCTree.JCVariableDecl tree)
Overrides:
visitVarDef in class JCTree.Visitor

printVarDefName

public void printVarDefName(JCTree.JCVariableDecl tree)
                     throws java.io.IOException
Throws:
java.io.IOException

visitSkip

public void visitSkip(JCTree.JCSkip tree)
Overrides:
visitSkip in class JCTree.Visitor

visitBlock

public void visitBlock(JCTree.JCBlock tree)
Overrides:
visitBlock in class JCTree.Visitor

visitBracketExpr

public void visitBracketExpr(JCTree.JCBracketExpr tree)
Overrides:
visitBracketExpr in class JCTree.Visitor

visitBracketStat

public void visitBracketStat(JCTree.JCBracketStat tree)
Overrides:
visitBracketStat in class JCTree.Visitor

visitEscapeExpr

public void visitEscapeExpr(JCTree.JCEscapeExpr tree)
Overrides:
visitEscapeExpr in class JCTree.Visitor

visitEscapeStat

public void visitEscapeStat(JCTree.JCEscapeStat tree)
Overrides:
visitEscapeStat in class JCTree.Visitor

visitDoLoop

public void visitDoLoop(JCTree.JCDoWhileLoop tree)
Overrides:
visitDoLoop in class JCTree.Visitor

visitWhileLoop

public void visitWhileLoop(JCTree.JCWhileLoop tree)
Overrides:
visitWhileLoop in class JCTree.Visitor

visitForLoop

public void visitForLoop(JCTree.JCForLoop tree)
Overrides:
visitForLoop in class JCTree.Visitor

visitForeachLoop

public void visitForeachLoop(JCTree.JCEnhancedForLoop tree)
Overrides:
visitForeachLoop in class JCTree.Visitor

visitLabelled

public void visitLabelled(JCTree.JCLabeledStatement tree)
Overrides:
visitLabelled in class JCTree.Visitor

visitSwitch

public void visitSwitch(JCTree.JCSwitch tree)
Overrides:
visitSwitch in class JCTree.Visitor

visitCase

public void visitCase(JCTree.JCCase tree)
Overrides:
visitCase in class JCTree.Visitor

visitSynchronized

public void visitSynchronized(JCTree.JCSynchronized tree)
Overrides:
visitSynchronized in class JCTree.Visitor

visitTry

public void visitTry(JCTree.JCTry tree)
Overrides:
visitTry in class JCTree.Visitor

visitCatch

public void visitCatch(JCTree.JCCatch tree)
Overrides:
visitCatch in class JCTree.Visitor

visitConditional

public void visitConditional(JCTree.JCConditional tree)
Overrides:
visitConditional in class JCTree.Visitor

visitIf

public void visitIf(JCTree.JCIf tree)
Overrides:
visitIf in class JCTree.Visitor

visitExec

public void visitExec(JCTree.JCExpressionStatement tree)
Overrides:
visitExec in class JCTree.Visitor

visitBreak

public void visitBreak(JCTree.JCBreak tree)
Overrides:
visitBreak in class JCTree.Visitor

visitContinue

public void visitContinue(JCTree.JCContinue tree)
Overrides:
visitContinue in class JCTree.Visitor

visitReturn

public void visitReturn(JCTree.JCReturn tree)
Overrides:
visitReturn in class JCTree.Visitor

visitThrow

public void visitThrow(JCTree.JCThrow tree)
Overrides:
visitThrow in class JCTree.Visitor

visitAssert

public void visitAssert(JCTree.JCAssert tree)
Overrides:
visitAssert in class JCTree.Visitor

visitApply

public void visitApply(JCTree.JCMethodInvocation tree)
Overrides:
visitApply in class JCTree.Visitor

visitNewClass

public void visitNewClass(JCTree.JCNewClass tree)
Overrides:
visitNewClass in class JCTree.Visitor

visitNewArray

public void visitNewArray(JCTree.JCNewArray tree)
Overrides:
visitNewArray in class JCTree.Visitor

visitParens

public void visitParens(JCTree.JCParens tree)
Overrides:
visitParens in class JCTree.Visitor

visitAssign

public void visitAssign(JCTree.JCAssign tree)
Overrides:
visitAssign in class JCTree.Visitor

operatorName

public java.lang.String operatorName(int tag)

visitAssignop

public void visitAssignop(JCTree.JCAssignOp tree)
Overrides:
visitAssignop in class JCTree.Visitor

visitUnary

public void visitUnary(JCTree.JCUnary tree)
Overrides:
visitUnary in class JCTree.Visitor

visitBinary

public void visitBinary(JCTree.JCBinary tree)
Overrides:
visitBinary in class JCTree.Visitor

visitTypeCast

public void visitTypeCast(JCTree.JCTypeCast tree)
Overrides:
visitTypeCast in class JCTree.Visitor

visitTypeTest

public void visitTypeTest(JCTree.JCInstanceOf tree)
Overrides:
visitTypeTest in class JCTree.Visitor

visitIndexed

public void visitIndexed(JCTree.JCArrayAccess tree)
Overrides:
visitIndexed in class JCTree.Visitor

visitSelect

public void visitSelect(JCTree.JCFieldAccess tree)
Overrides:
visitSelect in class JCTree.Visitor

visitIdent

public void visitIdent(JCTree.JCIdent tree)
Overrides:
visitIdent in class JCTree.Visitor

visitLiteral

public void visitLiteral(JCTree.JCLiteral tree)
Overrides:
visitLiteral in class JCTree.Visitor

visitTypeIdent

public void visitTypeIdent(JCTree.JCPrimitiveTypeTree tree)
Overrides:
visitTypeIdent in class JCTree.Visitor

visitTypeArray

public void visitTypeArray(JCTree.JCArrayTypeTree tree)
Overrides:
visitTypeArray in class JCTree.Visitor

printBaseElementType

private void printBaseElementType(JCTree.JCArrayTypeTree tree)
                           throws java.io.IOException
Throws:
java.io.IOException

printBrackets

private void printBrackets(JCTree.JCArrayTypeTree tree)
                    throws java.io.IOException
Throws:
java.io.IOException

visitTypeApply

public void visitTypeApply(JCTree.JCTypeApply tree)
Overrides:
visitTypeApply in class JCTree.Visitor

visitTypeParameter

public void visitTypeParameter(JCTree.JCTypeParameter tree)
Overrides:
visitTypeParameter in class JCTree.Visitor

visitWildcard

public void visitWildcard(JCTree.JCWildcard tree)
Overrides:
visitWildcard in class JCTree.Visitor

visitTypeBoundKind

public void visitTypeBoundKind(JCTree.TypeBoundKind tree)
Overrides:
visitTypeBoundKind in class JCTree.Visitor

visitErroneous

public void visitErroneous(JCTree.JCErroneous tree)
Overrides:
visitErroneous in class JCTree.Visitor

visitLetExpr

public void visitLetExpr(JCTree.LetExpr tree)
Overrides:
visitLetExpr in class JCTree.Visitor

visitModifiers

public void visitModifiers(JCTree.JCModifiers mods)
Overrides:
visitModifiers in class JCTree.Visitor

visitAnnotation

public void visitAnnotation(JCTree.JCAnnotation tree)
Overrides:
visitAnnotation in class JCTree.Visitor

visitTree

public void visitTree(JCTree tree)
Overrides:
visitTree in class JCTree.Visitor