langtools/src/share/classes/com/sun/tools/javac/tree/Pretty.java
author xdono
Thu, 02 Oct 2008 19:58:40 -0700
changeset 1264 076a3cde30d5
parent 1260 a772ba9ba43d
child 3149 0cd06d598d6f
permissions -rw-r--r--
6754988: Update copyright year Summary: Update for files that have been modified starting July 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
1264
076a3cde30d5 6754988: Update copyright year
xdono
parents: 1260
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
/** Prints out a tree as an indented Java source program.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public class Pretty extends JCTree.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    public Pretty(Writer out, boolean sourceOutput) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        this.out = out;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        this.sourceOutput = sourceOutput;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    /** Set when we are producing source output.  If we're not
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     *  producing source output, we can sometimes give more detail in
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     *  the output even though that detail would not be valid java
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     *  soruce.
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    private final boolean sourceOutput;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    /** The output stream on which trees are printed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    Writer out;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    /** Indentation width (can be reassigned from outside).
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public int width = 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /** The current left margin.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    int lmargin = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    /** The enclosing class name.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    Name enclClassName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /** A hashtable mapping trees to their documentation comments
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     *  (can be null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    Map<JCTree, String> docComments = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    /** Align code to be indented to left margin.
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    void align() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < lmargin; i++) out.write(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    /** Increase left margin by indentation width.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    void indent() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        lmargin = lmargin + width;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    /** Decrease left margin by indentation width.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    void undent() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        lmargin = lmargin - width;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    /** Enter a new precedence level. Emit a `(' if new precedence level
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     *  is less than precedence level so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     *  @param contextPrec    The precedence level in force so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     *  @param ownPrec        The new precedence level.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    void open(int contextPrec, int ownPrec) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        if (ownPrec < contextPrec) out.write("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    /** Leave precedence level. Emit a `(' if inner precedence level
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     *  is less than precedence level we revert to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     *  @param contextPrec    The precedence level we revert to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     *  @param ownPrec        The inner precedence level.
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    void close(int contextPrec, int ownPrec) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        if (ownPrec < contextPrec) out.write(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    /** Print string, replacing all non-ascii character with unicode escapes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    public void print(Object s) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        out.write(Convert.escapeUnicode(s.toString()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    /** Print new line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    public void println() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        out.write(lineSep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    String lineSep = System.getProperty("line.separator");
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    /**************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     * Traversal methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    /** Exception to propogate IOException through visitXXX methods */
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    private static class UncheckedIOException extends Error {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        static final long serialVersionUID = -4032692679158424751L;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        UncheckedIOException(IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            super(e.getMessage(), e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    /** Visitor argument: the current precedence level.
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    int prec;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    /** Visitor method: print expression tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     *  @param prec  The current precedence level.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    public void printExpr(JCTree tree, int prec) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        int prevPrec = this.prec;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            this.prec = prec;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            if (tree == null) print("/*missing*/");
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                tree.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        } catch (UncheckedIOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            IOException e = new IOException(ex.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            e.initCause(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            throw e;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            this.prec = prevPrec;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    /** Derived visitor method: print expression tree at minimum precedence level
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     *  for expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    public void printExpr(JCTree tree) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        printExpr(tree, TreeInfo.noPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    /** Derived visitor method: print statement tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    public void printStat(JCTree tree) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        printExpr(tree, TreeInfo.notExpression);
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    /** Derived visitor method: print list of expression trees, separated by given string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     *  @param sep the separator string
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    public <T extends JCTree> void printExprs(List<T> trees, String sep) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        if (trees.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            printExpr(trees.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            for (List<T> l = trees.tail; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                print(sep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                printExpr(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    /** Derived visitor method: print list of expression trees, separated by commas.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    public <T extends JCTree> void printExprs(List<T> trees) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        printExprs(trees, ", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    /** Derived visitor method: print list of statements, each on a separate line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    public void printStats(List<? extends JCTree> trees) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            printStat(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    /** Print a set of modifiers.
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    public void printFlags(long flags) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        if ((flags & SYNTHETIC) != 0) print("/*synthetic*/ ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        print(TreeInfo.flagNames(flags));
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        if ((flags & StandardFlags) != 0) print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        if ((flags & ANNOTATION) != 0) print("@");
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    public void printAnnotations(List<JCAnnotation> trees) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        for (List<JCAnnotation> l = trees; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            printStat(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    /** Print documentation comment, if it exists
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     *  @param tree    The tree for which a documentation comment should be printed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    public void printDocComment(JCTree tree) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        if (docComments != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            String dc = docComments.get(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            if (dc != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                print("/**"); println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                int pos = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                int endpos = lineEndPos(dc, pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                while (pos < dc.length()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                    align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                    print(" *");
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                    if (pos < dc.length() && dc.charAt(pos) > ' ') print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                    print(dc.substring(pos, endpos)); println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                    pos = endpos + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                    endpos = lineEndPos(dc, pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                align(); print(" */"); println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
//where
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    static int lineEndPos(String s, int start) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        int pos = s.indexOf('\n', start);
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        if (pos < 0) pos = s.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        return pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    /** If type parameter list is non-empty, print it enclosed in "<...>" brackets.
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    public void printTypeParameters(List<JCTypeParameter> trees) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        if (trees.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            print("<");
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            printExprs(trees);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            print(">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
    /** Print a block.
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    public void printBlock(List<? extends JCTree> stats) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        print("{");
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        printStats(stats);
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        undent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        print("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
    /** Print a block.
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    public void printEnumBody(List<JCTree> stats) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        print("{");
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        for (List<JCTree> l = stats; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            if (isEnumerator(l.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                if (!first) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                    print(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                    println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                printStat(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        for (List<JCTree> l = stats; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            if (!isEnumerator(l.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                printStat(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        undent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        print("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    /** Is the given tree an enumerator definition? */
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
    boolean isEnumerator(JCTree t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        return t.getTag() == JCTree.VARDEF && (((JCVariableDecl) t).mods.flags & ENUM) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    /** Print unit consisting of package clause and import statements in toplevel,
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
     *  followed by class definition. if class definition == null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
     *  print all definitions in toplevel.
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
     *  @param tree     The toplevel tree
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
     *  @param cdef     The class definition, which is assumed to be part of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
     *                  toplevel tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
    public void printUnit(JCCompilationUnit tree, JCClassDecl cdef) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        docComments = tree.docComments;
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        printDocComment(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        if (tree.pid != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            print("package ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
            printExpr(tree.pid);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        boolean firstImport = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        for (List<JCTree> l = tree.defs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        l.nonEmpty() && (cdef == null || l.head.getTag() == JCTree.IMPORT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            if (l.head.getTag() == JCTree.IMPORT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
                JCImport imp = (JCImport)l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                Name name = TreeInfo.name(imp.qualid);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   339
                if (name == name.table.names.asterisk ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                        cdef == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                        isUsed(TreeInfo.symbol(imp.qualid), cdef)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                    if (firstImport) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                        firstImport = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                        println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                    printStat(imp);
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                printStat(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        if (cdef != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
            printStat(cdef);
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
            println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
    boolean isUsed(final Symbol t, JCTree cdef) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        class UsedVisitor extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            public void scan(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                if (tree!=null && !result) tree.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
            boolean result = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
            public void visitIdent(JCIdent tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                if (tree.sym == t) result = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        UsedVisitor v = new UsedVisitor();
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        v.scan(cdef);
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        return v.result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
    /**************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
     * Visitor methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
     *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
    public void visitTopLevel(JCCompilationUnit tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
            printUnit(tree, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
    public void visitImport(JCImport tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            print("import ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            if (tree.staticImport) print("static ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
            printExpr(tree.qualid);
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
    public void visitClassDef(JCClassDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            println(); align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
            printDocComment(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
            printAnnotations(tree.mods.annotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            printFlags(tree.mods.flags & ~INTERFACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            Name enclClassNamePrev = enclClassName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
            enclClassName = tree.name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            if ((tree.mods.flags & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                print("interface " + tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                printTypeParameters(tree.typarams);
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                if (tree.implementing.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                    print(" extends ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                    printExprs(tree.implementing);
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                if ((tree.mods.flags & ENUM) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                    print("enum " + tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                    print("class " + tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                printTypeParameters(tree.typarams);
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                if (tree.extending != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                    print(" extends ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                    printExpr(tree.extending);
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                if (tree.implementing.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                    print(" implements ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                    printExprs(tree.implementing);
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
            print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
            if ((tree.mods.flags & ENUM) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                printEnumBody(tree.defs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
                printBlock(tree.defs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
            enclClassName = enclClassNamePrev;
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
    public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
            // when producing source output, omit anonymous constructors
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   442
            if (tree.name == tree.name.table.names.init &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                    enclClassName == null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                    sourceOutput) return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            println(); align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            printDocComment(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
            printExpr(tree.mods);
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            printTypeParameters(tree.typarams);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   449
            if (tree.name == tree.name.table.names.init) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                print(enclClassName != null ? enclClassName : tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
                printExpr(tree.restype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
                print(" " + tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
            print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
            printExprs(tree.params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
            print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
            if (tree.thrown.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
                print(" throws ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                printExprs(tree.thrown);
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
            if (tree.body != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
                print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
                printStat(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
                print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
    public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
            if (docComments != null && docComments.get(tree) != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                println(); align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
            printDocComment(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
            if ((tree.mods.flags & ENUM) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                print("/*public static final*/ ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                print(tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                if (tree.init != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                    print(" /* = ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
                    printExpr(tree.init);
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
                    print(" */");
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
                printExpr(tree.mods);
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
                if ((tree.mods.flags & VARARGS) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
                    printExpr(((JCArrayTypeTree) tree.vartype).elemtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
                    print("... " + tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                    printExpr(tree.vartype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                    print(" " + tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
                if (tree.init != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
                    print(" = ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                    printExpr(tree.init);
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
                if (prec == TreeInfo.notExpression) print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
    public void visitSkip(JCSkip tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
    public void visitBlock(JCBlock tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
            printFlags(tree.flags);
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
            printBlock(tree.stats);
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
    public void visitDoLoop(JCDoWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
            print("do ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
            printStat(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
            print(" while ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
            if (tree.cond.getTag() == JCTree.PARENS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
                printExpr(tree.cond);
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
                print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
                printExpr(tree.cond);
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
                print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
            print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
    public void visitWhileLoop(JCWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
            print("while ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
            if (tree.cond.getTag() == JCTree.PARENS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
                printExpr(tree.cond);
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
                print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
                printExpr(tree.cond);
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
                print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
            print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
            printStat(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
    public void visitForLoop(JCForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
            print("for (");
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            if (tree.init.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
                if (tree.init.head.getTag() == JCTree.VARDEF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
                    printExpr(tree.init.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
                    for (List<JCStatement> l = tree.init.tail; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
                        JCVariableDecl vdef = (JCVariableDecl)l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
                        print(", " + vdef.name + " = ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
                        printExpr(vdef.init);
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                    printExprs(tree.init);
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
            print("; ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
            if (tree.cond != null) printExpr(tree.cond);
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
            print("; ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
            printExprs(tree.step);
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
            print(") ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
            printStat(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
    public void visitForeachLoop(JCEnhancedForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
            print("for (");
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
            printExpr(tree.var);
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
            print(" : ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            printExpr(tree.expr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
            print(") ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
            printStat(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
    public void visitLabelled(JCLabeledStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
            print(tree.label + ": ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
            printStat(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
    public void visitSwitch(JCSwitch tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
            print("switch ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
            if (tree.selector.getTag() == JCTree.PARENS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
                printExpr(tree.selector);
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
                print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
                printExpr(tree.selector);
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
                print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
            print(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
            println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
            printStats(tree.cases);
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
            align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
            print("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
    public void visitCase(JCCase tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
            if (tree.pat == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
                print("default");
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
                print("case ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
                printExpr(tree.pat);
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
            print(": ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
            println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
            indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
            printStats(tree.stats);
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
            undent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
            align();
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
    public void visitSynchronized(JCSynchronized tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
            print("synchronized ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
            if (tree.lock.getTag() == JCTree.PARENS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
                printExpr(tree.lock);
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
                print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
                printExpr(tree.lock);
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
                print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
            print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
            printStat(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
    public void visitTry(JCTry tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
            print("try ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
            printStat(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
            for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
                printStat(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
            if (tree.finalizer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
                print(" finally ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
                printStat(tree.finalizer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
    public void visitCatch(JCCatch tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
            print(" catch (");
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
            printExpr(tree.param);
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
            print(") ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
            printStat(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
    public void visitConditional(JCConditional tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
            open(prec, TreeInfo.condPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
            printExpr(tree.cond, TreeInfo.condPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
            print(" ? ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
            printExpr(tree.truepart, TreeInfo.condPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
            print(" : ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
            printExpr(tree.falsepart, TreeInfo.condPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
            close(prec, TreeInfo.condPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
    public void visitIf(JCIf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
            print("if ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
            if (tree.cond.getTag() == JCTree.PARENS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
                printExpr(tree.cond);
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
                print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
                printExpr(tree.cond);
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
                print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
            print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
            printStat(tree.thenpart);
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
            if (tree.elsepart != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                print(" else ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                printStat(tree.elsepart);
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
    public void visitExec(JCExpressionStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
            printExpr(tree.expr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
            if (prec == TreeInfo.notExpression) print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
    public void visitBreak(JCBreak tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
            print("break");
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
            if (tree.label != null) print(" " + tree.label);
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
            print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
    public void visitContinue(JCContinue tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
            print("continue");
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
            if (tree.label != null) print(" " + tree.label);
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
            print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
    public void visitReturn(JCReturn tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
            print("return");
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
            if (tree.expr != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
                print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
                printExpr(tree.expr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
            print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
    public void visitThrow(JCThrow tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
            print("throw ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
            printExpr(tree.expr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
            print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
    public void visitAssert(JCAssert tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
            print("assert ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
            printExpr(tree.cond);
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
            if (tree.detail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
                print(" : ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
                printExpr(tree.detail);
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
            print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
    public void visitApply(JCMethodInvocation tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
            if (!tree.typeargs.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
                if (tree.meth.getTag() == JCTree.SELECT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
                    JCFieldAccess left = (JCFieldAccess)tree.meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
                    printExpr(left.selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
                    print(".<");
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
                    printExprs(tree.typeargs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
                    print(">" + left.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
                    print("<");
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
                    printExprs(tree.typeargs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
                    print(">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
                    printExpr(tree.meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
                printExpr(tree.meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
            print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
            printExprs(tree.args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
            print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
    public void visitNewClass(JCNewClass tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
            if (tree.encl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
                printExpr(tree.encl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
                print(".");
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
            print("new ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
            if (!tree.typeargs.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
                print("<");
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
                printExprs(tree.typeargs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
                print(">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
            printExpr(tree.clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
            print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
            printExprs(tree.args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
            print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
            if (tree.def != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
                Name enclClassNamePrev = enclClassName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
                enclClassName =
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
                        tree.def.name != null ? tree.def.name :
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   838
                            tree.type != null && tree.type.tsym.name != tree.type.tsym.name.table.names.empty
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   839
                                ? tree.type.tsym.name : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
                if ((tree.def.mods.flags & Flags.ENUM) != 0) print("/*enum*/");
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
                printBlock(tree.def.defs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
                enclClassName = enclClassNamePrev;
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
    public void visitNewArray(JCNewArray tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
            if (tree.elemtype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
                print("new ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
                JCTree elem = tree.elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
                if (elem instanceof JCArrayTypeTree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
                    printBaseElementType((JCArrayTypeTree) elem);
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
                    printExpr(elem);
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
                for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
                    print("[");
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
                    printExpr(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
                    print("]");
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
                if (elem instanceof JCArrayTypeTree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
                    printBrackets((JCArrayTypeTree) elem);
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
            if (tree.elems != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
                if (tree.elemtype != null) print("[]");
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
                print("{");
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
                printExprs(tree.elems);
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
                print("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
    public void visitParens(JCParens tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
            print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
            printExpr(tree.expr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
            print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
    public void visitAssign(JCAssign tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
            open(prec, TreeInfo.assignPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
            printExpr(tree.lhs, TreeInfo.assignPrec + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
            print(" = ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
            printExpr(tree.rhs, TreeInfo.assignPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
            close(prec, TreeInfo.assignPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
    public String operatorName(int tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
        switch(tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
            case JCTree.POS:     return "+";
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
            case JCTree.NEG:     return "-";
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
            case JCTree.NOT:     return "!";
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
            case JCTree.COMPL:   return "~";
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
            case JCTree.PREINC:  return "++";
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
            case JCTree.PREDEC:  return "--";
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
            case JCTree.POSTINC: return "++";
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
            case JCTree.POSTDEC: return "--";
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
            case JCTree.NULLCHK: return "<*nullchk*>";
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
            case JCTree.OR:      return "||";
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
            case JCTree.AND:     return "&&";
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
            case JCTree.EQ:      return "==";
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
            case JCTree.NE:      return "!=";
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
            case JCTree.LT:      return "<";
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
            case JCTree.GT:      return ">";
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
            case JCTree.LE:      return "<=";
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
            case JCTree.GE:      return ">=";
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
            case JCTree.BITOR:   return "|";
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
            case JCTree.BITXOR:  return "^";
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
            case JCTree.BITAND:  return "&";
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
            case JCTree.SL:      return "<<";
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
            case JCTree.SR:      return ">>";
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
            case JCTree.USR:     return ">>>";
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
            case JCTree.PLUS:    return "+";
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
            case JCTree.MINUS:   return "-";
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
            case JCTree.MUL:     return "*";
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
            case JCTree.DIV:     return "/";
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
            case JCTree.MOD:     return "%";
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
            default: throw new Error();
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
    public void visitAssignop(JCAssignOp tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
            open(prec, TreeInfo.assignopPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
            printExpr(tree.lhs, TreeInfo.assignopPrec + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
            print(" " + operatorName(tree.getTag() - JCTree.ASGOffset) + "= ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
            printExpr(tree.rhs, TreeInfo.assignopPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
            close(prec, TreeInfo.assignopPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
    public void visitUnary(JCUnary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
            int ownprec = TreeInfo.opPrec(tree.getTag());
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
            String opname = operatorName(tree.getTag());
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
            open(prec, ownprec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
            if (tree.getTag() <= JCTree.PREDEC) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
                print(opname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
                printExpr(tree.arg, ownprec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
                printExpr(tree.arg, ownprec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
                print(opname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
            close(prec, ownprec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
    public void visitBinary(JCBinary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
            int ownprec = TreeInfo.opPrec(tree.getTag());
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
            String opname = operatorName(tree.getTag());
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
            open(prec, ownprec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
            printExpr(tree.lhs, ownprec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
            print(" " + opname + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
            printExpr(tree.rhs, ownprec + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
            close(prec, ownprec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
    public void visitTypeCast(JCTypeCast tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
            open(prec, TreeInfo.prefixPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
            print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
            printExpr(tree.clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
            print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
            printExpr(tree.expr, TreeInfo.prefixPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
            close(prec, TreeInfo.prefixPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
    public void visitTypeTest(JCInstanceOf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
            open(prec, TreeInfo.ordPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
            printExpr(tree.expr, TreeInfo.ordPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
            print(" instanceof ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
            printExpr(tree.clazz, TreeInfo.ordPrec + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
            close(prec, TreeInfo.ordPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
    public void visitIndexed(JCArrayAccess tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
            printExpr(tree.indexed, TreeInfo.postfixPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
            print("[");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
            printExpr(tree.index);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
            print("]");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
    public void visitSelect(JCFieldAccess tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
            printExpr(tree.selected, TreeInfo.postfixPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
            print("." + tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
    public void visitIdent(JCIdent tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
            print(tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
    public void visitLiteral(JCLiteral tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
            switch (tree.typetag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
                case TypeTags.INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
                    print(tree.value.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
                case TypeTags.LONG:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
                    print(tree.value + "L");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
                case TypeTags.FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
                    print(tree.value + "F");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
                case TypeTags.DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
                    print(tree.value.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
                case TypeTags.CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
                    print("\'" +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
                            Convert.quote(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
                            String.valueOf((char)((Number)tree.value).intValue())) +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
                            "\'");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
                case TypeTags.BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
                    print(((Number)tree.value).intValue() == 1 ? "true" : "false");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
                case TypeTags.BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
                    print("null");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
                    print("\"" + Convert.quote(tree.value.toString()) + "\"");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
    public void visitTypeIdent(JCPrimitiveTypeTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
            switch(tree.typetag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
                case TypeTags.BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
                    print("byte");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
                case TypeTags.CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
                    print("char");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
                case TypeTags.SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
                    print("short");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
                case TypeTags.INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
                    print("int");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
                case TypeTags.LONG:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
                    print("long");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
                case TypeTags.FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
                    print("float");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
                case TypeTags.DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
                    print("double");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
                case TypeTags.BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
                    print("boolean");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
                case TypeTags.VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
                    print("void");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
                    print("error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
    public void visitTypeArray(JCArrayTypeTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
            printBaseElementType(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
            printBrackets(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
    // Prints the inner element type of a nested array
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
    private void printBaseElementType(JCArrayTypeTree tree) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
        JCTree elem = tree.elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
        while (elem instanceof JCWildcard)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
            elem = ((JCWildcard) elem).inner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
        if (elem instanceof JCArrayTypeTree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
            printBaseElementType((JCArrayTypeTree) elem);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
            printExpr(elem);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
    // prints the brackets of a nested array in reverse order
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
    private void printBrackets(JCArrayTypeTree tree) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
        JCTree elem;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
            elem = tree.elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
            print("[]");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
            if (!(elem instanceof JCArrayTypeTree)) break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
            tree = (JCArrayTypeTree) elem;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
    public void visitTypeApply(JCTypeApply tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
            printExpr(tree.clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
            print("<");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
            printExprs(tree.arguments);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
            print(">");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
    public void visitTypeParameter(JCTypeParameter tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
            print(tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
            if (tree.bounds.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
                print(" extends ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
                printExprs(tree.bounds, " & ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
    public void visitWildcard(JCWildcard tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
            print(tree.kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
            if (tree.kind.kind != BoundKind.UNBOUND)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
                printExpr(tree.inner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
    public void visitTypeBoundKind(TypeBoundKind tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
            print(String.valueOf(tree.kind));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
    public void visitErroneous(JCErroneous tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
            print("(ERROR)");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
    public void visitLetExpr(LetExpr tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
            print("(let " + tree.defs + " in " + tree.expr + ")");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
    public void visitModifiers(JCModifiers mods) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
            printAnnotations(mods.annotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
            printFlags(mods.flags);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
    public void visitAnnotation(JCAnnotation tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
            print("@");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
            printExpr(tree.annotationType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
            print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
            printExprs(tree.args);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
            print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
    public void visitTree(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
            print("(UNKNOWN: " + tree + ")");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
            println();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
            throw new UncheckedIOException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
}