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