Thu, 07 Mar 2013 10:04:28 +0000
8009138: javac, equals-hashCode warning tuning
Reviewed-by: mcimadamore
jjg@1530 | 1 | /* |
jjg@1530 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
jjg@1530 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1530 | 4 | * |
jjg@1530 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1530 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1530 | 7 | * published by the Free Software Foundation. |
jjg@1530 | 8 | * |
jjg@1530 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1530 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1530 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1530 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1530 | 13 | * accompanied this code). |
jjg@1530 | 14 | * |
jjg@1530 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@1530 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1530 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1530 | 18 | * |
jjg@1530 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1530 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@1530 | 21 | * questions. |
jjg@1530 | 22 | */ |
jjg@1530 | 23 | |
jjg@1530 | 24 | import java.io.File; |
jjg@1530 | 25 | import java.io.IOException; |
jjg@1530 | 26 | import java.io.PrintWriter; |
jjg@1530 | 27 | import java.lang.reflect.Field; |
jjg@1530 | 28 | import java.util.ArrayList; |
jjg@1530 | 29 | import java.util.Arrays; |
jjg@1530 | 30 | import java.util.Collection; |
jjg@1530 | 31 | import java.util.EnumSet; |
jjg@1530 | 32 | import java.util.HashMap; |
jjg@1530 | 33 | import java.util.List; |
jjg@1530 | 34 | import java.util.Locale; |
jjg@1530 | 35 | import java.util.Map; |
jjg@1530 | 36 | import java.util.Set; |
jjg@1530 | 37 | |
jjg@1530 | 38 | import javax.lang.model.element.Name; |
jjg@1530 | 39 | import javax.lang.model.element.TypeElement; |
jjg@1530 | 40 | import javax.tools.FileObject; |
jjg@1530 | 41 | import javax.tools.JavaCompiler; |
jjg@1530 | 42 | import javax.tools.JavaFileObject; |
jjg@1530 | 43 | import javax.tools.StandardJavaFileManager; |
jjg@1530 | 44 | import javax.tools.StandardLocation; |
jjg@1530 | 45 | import javax.tools.ToolProvider; |
jjg@1530 | 46 | |
jjg@1530 | 47 | import com.sun.source.util.JavacTask; |
jjg@1530 | 48 | import com.sun.source.util.TaskEvent; |
jjg@1530 | 49 | import com.sun.source.util.TaskListener; |
jjg@1530 | 50 | import com.sun.source.util.Trees; |
jjg@1530 | 51 | import com.sun.tools.javac.api.JavacTrees; |
jjg@1530 | 52 | import com.sun.tools.javac.code.Annotations; |
jjg@1530 | 53 | import com.sun.tools.javac.code.Attribute; |
jjg@1530 | 54 | import com.sun.tools.javac.code.Flags; |
jjg@1530 | 55 | import com.sun.tools.javac.code.Kinds; |
jjg@1530 | 56 | import com.sun.tools.javac.code.Printer; |
jjg@1530 | 57 | import com.sun.tools.javac.code.Scope; |
jjg@1530 | 58 | import com.sun.tools.javac.code.Scope.CompoundScope; |
jjg@1530 | 59 | import com.sun.tools.javac.code.Symbol; |
jjg@1530 | 60 | import com.sun.tools.javac.code.Symbol.*; |
jjg@1530 | 61 | import com.sun.tools.javac.code.Type; |
jjg@1530 | 62 | import com.sun.tools.javac.code.Type.*; |
jjg@1530 | 63 | import com.sun.tools.javac.code.TypeTag; |
jjg@1530 | 64 | import com.sun.tools.javac.tree.JCTree; |
jjg@1530 | 65 | import com.sun.tools.javac.tree.JCTree.*; |
jjg@1530 | 66 | import com.sun.tools.javac.tree.Pretty; |
jjg@1530 | 67 | import com.sun.tools.javac.tree.TreeInfo; |
jjg@1530 | 68 | import com.sun.tools.javac.tree.TreeScanner; |
jjg@1530 | 69 | import com.sun.tools.javac.util.Assert; |
jjg@1530 | 70 | import com.sun.tools.javac.util.Context; |
jjg@1530 | 71 | import com.sun.tools.javac.util.Log; |
jjg@1530 | 72 | |
jjg@1530 | 73 | |
jjg@1530 | 74 | /** |
jjg@1530 | 75 | * Debug printer for javac internals, for when toString() just isn't enough. |
jjg@1530 | 76 | * |
jjg@1530 | 77 | * <p> |
jjg@1530 | 78 | * The printer provides an API to generate structured views of javac objects, |
jjg@1530 | 79 | * such as AST nodes, symbol, types and annotations. Various aspects of the |
jjg@1530 | 80 | * output can be configured, such as whether to show nulls, empty lists, or |
jjg@1530 | 81 | * a compressed representation of the source code. Visitors are used to walk |
jjg@1530 | 82 | * object hierarchies, and can be replaced with custom visitors if the default |
jjg@1530 | 83 | * visitors are not flexible enough. |
jjg@1530 | 84 | * |
jjg@1530 | 85 | * <p> |
jjg@1530 | 86 | * In general, nodes are printed with an initial line identifying the node |
jjg@1530 | 87 | * followed by indented lines for the child nodes. Currently, graphs are |
jjg@1530 | 88 | * represented by printing a spanning subtree. |
jjg@1530 | 89 | * |
jjg@1530 | 90 | * <p> |
jjg@1530 | 91 | * The printer can be accessed via a simple command-line utility, |
jjg@1530 | 92 | * which makes it easy to see the internal representation of source code, |
jjg@1530 | 93 | * such as simple test programs, during the compilation pipeline. |
jjg@1530 | 94 | * |
jjg@1530 | 95 | * <p><b>This is NOT part of any supported API. |
jjg@1530 | 96 | * If you write code that depends on this, you do so at your own risk. |
jjg@1530 | 97 | * This code and its internal interfaces are subject to change or |
jjg@1530 | 98 | * deletion without notice.</b> |
jjg@1530 | 99 | */ |
jjg@1530 | 100 | |
jjg@1530 | 101 | public class DPrinter { |
jjg@1530 | 102 | protected final PrintWriter out; |
jjg@1530 | 103 | protected final Trees trees; |
jjg@1530 | 104 | protected Printer printer; |
jjg@1530 | 105 | protected boolean showEmptyItems = true; |
jjg@1530 | 106 | protected boolean showNulls = true; |
jjg@1530 | 107 | protected boolean showPositions = false; |
jjg@1530 | 108 | protected boolean showSrc; |
jjg@1530 | 109 | protected boolean showTreeSymbols; |
jjg@1530 | 110 | protected boolean showTreeTypes; |
jjg@1530 | 111 | protected int maxSrcLength = 32; |
jjg@1530 | 112 | protected Locale locale = Locale.getDefault(); |
jjg@1530 | 113 | protected static final String NULL = "#null"; |
jjg@1530 | 114 | |
jjg@1530 | 115 | // <editor-fold defaultstate="collapsed" desc="Configuration"> |
jjg@1530 | 116 | |
jjg@1530 | 117 | public static DPrinter instance(Context context) { |
jjg@1530 | 118 | DPrinter dp = context.get(DPrinter.class); |
jjg@1530 | 119 | if (dp == null) { |
jjg@1530 | 120 | dp = new DPrinter(context); |
jjg@1530 | 121 | } |
jjg@1530 | 122 | return dp; |
jjg@1530 | 123 | |
jjg@1530 | 124 | } |
jjg@1530 | 125 | |
jjg@1530 | 126 | protected DPrinter(Context context) { |
jjg@1530 | 127 | context.put(DPrinter.class, this); |
jjg@1530 | 128 | out = context.get(Log.outKey); |
jjg@1530 | 129 | trees = JavacTrees.instance(context); |
jjg@1530 | 130 | } |
jjg@1530 | 131 | |
jjg@1530 | 132 | public DPrinter(PrintWriter out, Trees trees) { |
jjg@1530 | 133 | this.out = out; |
jjg@1530 | 134 | this.trees = trees; |
jjg@1530 | 135 | } |
jjg@1530 | 136 | |
jjg@1530 | 137 | public DPrinter emptyItems(boolean showEmptyItems) { |
jjg@1530 | 138 | this.showEmptyItems = showEmptyItems; |
jjg@1530 | 139 | return this; |
jjg@1530 | 140 | } |
jjg@1530 | 141 | |
jjg@1530 | 142 | public DPrinter nulls(boolean showNulls) { |
jjg@1530 | 143 | this.showNulls = showNulls; |
jjg@1530 | 144 | return this; |
jjg@1530 | 145 | } |
jjg@1530 | 146 | |
jjg@1530 | 147 | public DPrinter positions(boolean showPositions) { |
jjg@1530 | 148 | this.showPositions = showPositions; |
jjg@1530 | 149 | return this; |
jjg@1530 | 150 | } |
jjg@1530 | 151 | |
jjg@1530 | 152 | public DPrinter source(boolean showSrc) { |
jjg@1530 | 153 | this.showSrc = showSrc; |
jjg@1530 | 154 | return this; |
jjg@1530 | 155 | } |
jjg@1530 | 156 | |
jjg@1530 | 157 | public DPrinter source(int maxSrcLength) { |
jjg@1530 | 158 | this.showSrc = true; |
jjg@1530 | 159 | this.maxSrcLength = maxSrcLength; |
jjg@1530 | 160 | return this; |
jjg@1530 | 161 | } |
jjg@1530 | 162 | |
jjg@1530 | 163 | public DPrinter treeSymbols(boolean showTreeSymbols) { |
jjg@1530 | 164 | this.showTreeSymbols = showTreeSymbols; |
jjg@1530 | 165 | return this; |
jjg@1530 | 166 | } |
jjg@1530 | 167 | |
jjg@1530 | 168 | public DPrinter treeTypes(boolean showTreeTypes) { |
jjg@1530 | 169 | this.showTreeTypes = showTreeTypes; |
jjg@1530 | 170 | return this; |
jjg@1530 | 171 | } |
jjg@1530 | 172 | |
jjg@1530 | 173 | public DPrinter typeSymbolPrinter(Printer p) { |
jjg@1530 | 174 | printer = p; |
jjg@1530 | 175 | return this; |
jjg@1530 | 176 | } |
jjg@1530 | 177 | |
jjg@1530 | 178 | // </editor-fold> |
jjg@1530 | 179 | |
jjg@1530 | 180 | // <editor-fold defaultstate="collapsed" desc="Printing"> |
jjg@1530 | 181 | |
jjg@1530 | 182 | protected enum Details { |
jjg@1530 | 183 | /** A one-line non-recursive summary */ |
jjg@1530 | 184 | SUMMARY, |
jjg@1530 | 185 | /** Multi-line, possibly recursive. */ |
jjg@1530 | 186 | FULL |
jjg@1530 | 187 | }; |
jjg@1530 | 188 | |
jjg@1530 | 189 | public void printAnnotations(String label, Annotations annotations) { |
jjg@1530 | 190 | printAnnotations(label, annotations, Details.FULL); |
jjg@1530 | 191 | } |
jjg@1530 | 192 | |
jjg@1530 | 193 | protected void printAnnotations(String label, Annotations annotations, Details details) { |
jjg@1530 | 194 | if (annotations == null) { |
jjg@1530 | 195 | printNull(label); |
jjg@1530 | 196 | } else { |
jjg@1530 | 197 | // no SUMMARY format currently available to use |
jjg@1530 | 198 | |
jjg@1530 | 199 | // use reflection to get at private fields |
jjg@1530 | 200 | Object DECL_NOT_STARTED = getField(null, Annotations.class, "DECL_NOT_STARTED"); |
jjg@1530 | 201 | Object DECL_IN_PROGRESS = getField(null, Annotations.class, "DECL_IN_PROGRESS"); |
jjg@1530 | 202 | Object attributes = getField(annotations, Annotations.class, "attributes"); |
jjg@1530 | 203 | Object type_attributes = getField(annotations, Annotations.class, "type_attributes"); |
jjg@1530 | 204 | |
jjg@1530 | 205 | if (!showEmptyItems) { |
jjg@1530 | 206 | if (attributes instanceof List && ((List) attributes).isEmpty() |
jjg@1530 | 207 | && attributes != DECL_NOT_STARTED |
jjg@1530 | 208 | && attributes != DECL_IN_PROGRESS |
jjg@1530 | 209 | && type_attributes instanceof List && ((List) type_attributes).isEmpty()) |
jjg@1530 | 210 | return; |
jjg@1530 | 211 | } |
jjg@1530 | 212 | |
jjg@1537 | 213 | printString(label, hashString(annotations)); |
jjg@1530 | 214 | |
jjg@1530 | 215 | indent(+1); |
jjg@1530 | 216 | if (attributes == DECL_NOT_STARTED) |
jjg@1530 | 217 | printString("attributes", "DECL_NOT_STARTED"); |
jjg@1530 | 218 | else if (attributes == DECL_IN_PROGRESS) |
jjg@1530 | 219 | printString("attributes", "DECL_IN_PROGRESS"); |
jjg@1530 | 220 | else if (attributes instanceof List) |
jjg@1530 | 221 | printList("attributes", (List) attributes); |
jjg@1530 | 222 | else |
jjg@1530 | 223 | printObject("attributes", attributes, Details.SUMMARY); |
jjg@1530 | 224 | |
jjg@1530 | 225 | if (attributes instanceof List) |
jjg@1530 | 226 | printList("type_attributes", (List) type_attributes); |
jjg@1530 | 227 | else |
jjg@1530 | 228 | printObject("type_attributes", type_attributes, Details.SUMMARY); |
jjg@1530 | 229 | indent(-1); |
jjg@1530 | 230 | } |
jjg@1530 | 231 | } |
jjg@1530 | 232 | |
jjg@1530 | 233 | public void printAttribute(String label, Attribute attr) { |
jjg@1530 | 234 | if (attr == null) { |
jjg@1530 | 235 | printNull(label); |
jjg@1530 | 236 | } else { |
jjg@1530 | 237 | printString(label, attr.getClass().getSimpleName()); |
jjg@1530 | 238 | |
jjg@1530 | 239 | indent(+1); |
jjg@1530 | 240 | attr.accept(attrVisitor); |
jjg@1530 | 241 | indent(-1); |
jjg@1530 | 242 | } |
jjg@1530 | 243 | } |
jjg@1530 | 244 | |
jjg@1530 | 245 | public void printFileObject(String label, FileObject fo) { |
jjg@1530 | 246 | if (fo == null) { |
jjg@1530 | 247 | printNull(label); |
jjg@1530 | 248 | } else { |
jjg@1530 | 249 | printString(label, fo.getName()); |
jjg@1530 | 250 | } |
jjg@1530 | 251 | } |
jjg@1530 | 252 | |
jjg@1530 | 253 | protected <T> void printImplClass(T item, Class<? extends T> stdImplClass) { |
jjg@1530 | 254 | if (item.getClass() != stdImplClass) |
jjg@1530 | 255 | printString("impl", item.getClass().getName()); |
jjg@1530 | 256 | } |
jjg@1530 | 257 | |
jjg@1530 | 258 | public void printInt(String label, int i) { |
jjg@1530 | 259 | printString(label, String.valueOf(i)); |
jjg@1530 | 260 | } |
jjg@1530 | 261 | |
jjg@1530 | 262 | public void printList(String label, List<?> list) { |
jjg@1530 | 263 | if (list == null) { |
jjg@1530 | 264 | printNull(label); |
jjg@1530 | 265 | } else if (!list.isEmpty() || showEmptyItems) { |
jjg@1530 | 266 | printString(label, "[" + list.size() + "]"); |
jjg@1530 | 267 | |
jjg@1530 | 268 | indent(+1); |
jjg@1530 | 269 | int i = 0; |
jjg@1530 | 270 | for (Object item: list) { |
jjg@1530 | 271 | printObject(String.valueOf(i++), item, Details.FULL); |
jjg@1530 | 272 | } |
jjg@1530 | 273 | indent(-1); |
jjg@1530 | 274 | } |
jjg@1530 | 275 | } |
jjg@1530 | 276 | |
jjg@1530 | 277 | public void printName(String label, Name name) { |
jjg@1530 | 278 | if (name == null) { |
jjg@1530 | 279 | printNull(label); |
jjg@1530 | 280 | } else { |
jjg@1530 | 281 | printString(label, name.toString()); |
jjg@1530 | 282 | } |
jjg@1530 | 283 | } |
jjg@1530 | 284 | |
jjg@1530 | 285 | public void printNull(String label) { |
jjg@1530 | 286 | if (showNulls) |
jjg@1530 | 287 | printString(label, NULL); |
jjg@1530 | 288 | } |
jjg@1530 | 289 | |
jjg@1530 | 290 | protected void printObject(String label, Object item, Details details) { |
jjg@1530 | 291 | if (item == null) { |
jjg@1530 | 292 | printNull(label); |
jjg@1530 | 293 | } else if (item instanceof Attribute) { |
jjg@1530 | 294 | printAttribute(label, (Attribute) item); |
jjg@1530 | 295 | } else if (item instanceof Symbol) { |
jjg@1530 | 296 | printSymbol(label, (Symbol) item, details); |
jjg@1530 | 297 | } else if (item instanceof Type) { |
jjg@1530 | 298 | printType(label, (Type) item, details); |
jjg@1530 | 299 | } else if (item instanceof JCTree) { |
jjg@1530 | 300 | printTree(label, (JCTree) item); |
jjg@1530 | 301 | } else if (item instanceof List) { |
jjg@1530 | 302 | printList(label, (List) item); |
jjg@1530 | 303 | } else if (item instanceof Name) { |
jjg@1530 | 304 | printName(label, (Name) item); |
jjg@1530 | 305 | } else { |
jjg@1530 | 306 | printString(label, String.valueOf(item)); |
jjg@1530 | 307 | } |
jjg@1530 | 308 | } |
jjg@1530 | 309 | |
jjg@1530 | 310 | public void printScope(String label, Scope scope) { |
jjg@1530 | 311 | printScope(label, scope, Details.FULL); |
jjg@1530 | 312 | } |
jjg@1530 | 313 | |
jjg@1530 | 314 | public void printScope(String label, Scope scope, Details details) { |
jjg@1530 | 315 | if (scope == null) { |
jjg@1530 | 316 | printNull(label); |
jjg@1530 | 317 | } else { |
jjg@1530 | 318 | switch (details) { |
jjg@1530 | 319 | case SUMMARY: { |
jjg@1530 | 320 | indent(); |
jjg@1530 | 321 | out.print(label); |
jjg@1530 | 322 | out.print(": ["); |
jjg@1530 | 323 | String sep = ""; |
jjg@1530 | 324 | for (Symbol sym: scope.getElements()) { |
jjg@1530 | 325 | out.print(sep); |
jjg@1530 | 326 | out.print(sym.name); |
jjg@1530 | 327 | sep = ","; |
jjg@1530 | 328 | } |
jjg@1530 | 329 | out.println("]"); |
jjg@1530 | 330 | break; |
jjg@1530 | 331 | } |
jjg@1530 | 332 | |
jjg@1530 | 333 | case FULL: { |
jjg@1530 | 334 | indent(); |
jjg@1530 | 335 | out.println(label); |
jjg@1530 | 336 | |
jjg@1530 | 337 | indent(+1); |
jjg@1530 | 338 | printImplClass(scope, Scope.class); |
jjg@1530 | 339 | printSymbol("owner", scope.owner, Details.SUMMARY); |
jjg@1530 | 340 | printScope("next", scope.next, Details.SUMMARY); |
jjg@1530 | 341 | printObject("shared", getField(scope, Scope.class, "shared"), Details.SUMMARY); |
jjg@1530 | 342 | if (scope instanceof CompoundScope) { |
jjg@1530 | 343 | printObject("subScopes", |
jjg@1530 | 344 | getField(scope, CompoundScope.class, "subScopes"), |
jjg@1530 | 345 | Details.FULL); |
jjg@1530 | 346 | } else { |
jjg@1530 | 347 | for (Symbol sym : scope.getElements()) { |
jjg@1530 | 348 | printSymbol(sym.name.toString(), sym, Details.SUMMARY); |
jjg@1530 | 349 | } |
jjg@1530 | 350 | } |
jjg@1530 | 351 | indent(-1); |
jjg@1530 | 352 | break; |
jjg@1530 | 353 | } |
jjg@1530 | 354 | } |
jjg@1530 | 355 | } |
jjg@1530 | 356 | } |
jjg@1530 | 357 | |
jjg@1530 | 358 | public void printSource(String label, JCTree tree) { |
jjg@1530 | 359 | printString(label, Pretty.toSimpleString(tree, maxSrcLength)); |
jjg@1530 | 360 | } |
jjg@1530 | 361 | |
jjg@1530 | 362 | public void printString(String label, String text) { |
jjg@1530 | 363 | indent(); |
jjg@1530 | 364 | out.print(label); |
jjg@1530 | 365 | out.print(": "); |
jjg@1530 | 366 | out.print(text); |
jjg@1530 | 367 | out.println(); |
jjg@1530 | 368 | } |
jjg@1530 | 369 | |
jjg@1530 | 370 | public void printSymbol(String label, Symbol symbol) { |
jjg@1530 | 371 | printSymbol(label, symbol, Details.FULL); |
jjg@1530 | 372 | } |
jjg@1530 | 373 | |
jjg@1530 | 374 | protected void printSymbol(String label, Symbol sym, Details details) { |
jjg@1530 | 375 | if (sym == null) { |
jjg@1530 | 376 | printNull(label); |
jjg@1530 | 377 | } else { |
jjg@1530 | 378 | switch (details) { |
jjg@1530 | 379 | case SUMMARY: |
jjg@1530 | 380 | printString(label, toString(sym)); |
jjg@1530 | 381 | break; |
jjg@1530 | 382 | |
jjg@1530 | 383 | case FULL: |
jjg@1530 | 384 | indent(); |
jjg@1530 | 385 | out.print(label); |
jjg@1537 | 386 | out.println(": " + |
jjg@1537 | 387 | info(sym.getClass(), |
jjg@1537 | 388 | String.format("0x%x--%s", sym.kind, Kinds.kindName(sym)), |
jjg@1537 | 389 | sym.getKind()) |
jjg@1530 | 390 | + " " + sym.name |
jjg@1537 | 391 | + " " + hashString(sym)); |
jjg@1530 | 392 | |
jjg@1530 | 393 | indent(+1); |
jjg@1530 | 394 | if (showSrc) { |
jjg@1530 | 395 | JCTree tree = (JCTree) trees.getTree(sym); |
jjg@1530 | 396 | if (tree != null) |
jjg@1530 | 397 | printSource("src", tree); |
jjg@1530 | 398 | } |
jjg@1530 | 399 | printString("flags", String.format("0x%x--%s", |
jjg@1530 | 400 | sym.flags_field, Flags.toString(sym.flags_field))); |
jjg@1530 | 401 | printObject("completer", sym.completer, Details.SUMMARY); // what if too long? |
jjg@1530 | 402 | printSymbol("owner", sym.owner, Details.SUMMARY); |
jjg@1530 | 403 | printType("type", sym.type, Details.SUMMARY); |
jjg@1530 | 404 | printType("erasure", sym.erasure_field, Details.SUMMARY); |
jjg@1537 | 405 | sym.accept(symVisitor, null); |
jjg@1530 | 406 | printAnnotations("annotations", sym.annotations, Details.SUMMARY); |
jjg@1530 | 407 | indent(-1); |
jjg@1530 | 408 | } |
jjg@1530 | 409 | } |
jjg@1530 | 410 | } |
jjg@1530 | 411 | |
jjg@1530 | 412 | protected String toString(Symbol sym) { |
jjg@1530 | 413 | return (printer != null) ? printer.visit(sym, locale) : String.valueOf(sym); |
jjg@1530 | 414 | } |
jjg@1530 | 415 | |
jjg@1530 | 416 | protected void printTree(String label, JCTree tree) { |
jjg@1530 | 417 | if (tree == null) { |
jjg@1530 | 418 | printNull(label); |
jjg@1530 | 419 | } else { |
jjg@1530 | 420 | indent(); |
jjg@1537 | 421 | String ext; |
jjg@1537 | 422 | try { |
jjg@1537 | 423 | ext = tree.getKind().name(); |
jjg@1537 | 424 | } catch (Throwable t) { |
jjg@1537 | 425 | ext = "n/a"; |
jjg@1537 | 426 | } |
jjg@1537 | 427 | out.print(label + ": " + info(tree.getClass(), tree.getTag(), ext)); |
jjg@1530 | 428 | if (showPositions) { |
jjg@1530 | 429 | // We can always get start position, but to get end position |
jjg@1530 | 430 | // and/or line+offset, we would need a JCCompilationUnit |
jjg@1530 | 431 | out.print(" pos:" + tree.pos); |
jjg@1530 | 432 | } |
jjg@1530 | 433 | if (showTreeTypes && tree.type != null) |
jjg@1530 | 434 | out.print(" type:" + toString(tree.type)); |
jjg@1530 | 435 | Symbol sym; |
jjg@1530 | 436 | if (showTreeSymbols && (sym = TreeInfo.symbolFor(tree)) != null) |
jjg@1530 | 437 | out.print(" sym:" + toString(sym)); |
jjg@1530 | 438 | out.println(); |
jjg@1530 | 439 | |
jjg@1530 | 440 | indent(+1); |
jjg@1530 | 441 | if (showSrc) { |
jjg@1530 | 442 | indent(); |
jjg@1530 | 443 | out.println("src: " + Pretty.toSimpleString(tree, maxSrcLength)); |
jjg@1530 | 444 | } |
jjg@1530 | 445 | tree.accept(treeVisitor); |
jjg@1530 | 446 | indent(-1); |
jjg@1530 | 447 | } |
jjg@1530 | 448 | } |
jjg@1530 | 449 | |
jjg@1530 | 450 | public void printType(String label, Type type) { |
jjg@1530 | 451 | printType(label, type, Details.FULL); |
jjg@1530 | 452 | } |
jjg@1530 | 453 | |
jjg@1530 | 454 | protected void printType(String label, Type type, Details details) { |
jjg@1530 | 455 | if (type == null) |
jjg@1530 | 456 | printNull(label); |
jjg@1530 | 457 | else { |
jjg@1530 | 458 | switch (details) { |
jjg@1530 | 459 | case SUMMARY: |
jjg@1530 | 460 | printString(label, toString(type)); |
jjg@1530 | 461 | break; |
jjg@1530 | 462 | |
jjg@1530 | 463 | case FULL: |
jjg@1530 | 464 | indent(); |
jjg@1530 | 465 | out.print(label); |
jjg@1537 | 466 | out.println(": " + info(type.getClass(), type.getTag(), type.getKind()) |
jjg@1537 | 467 | + " " + hashString(type)); |
jjg@1530 | 468 | |
jjg@1530 | 469 | indent(+1); |
jjg@1530 | 470 | printSymbol("tsym", type.tsym, Details.SUMMARY); |
jjg@1530 | 471 | printObject("constValue", type.constValue(), Details.SUMMARY); |
jjg@1537 | 472 | type.accept(typeVisitor, null); |
jjg@1530 | 473 | indent(-1); |
jjg@1530 | 474 | } |
jjg@1530 | 475 | } |
jjg@1530 | 476 | } |
jjg@1530 | 477 | |
jjg@1530 | 478 | protected String toString(Type type) { |
jjg@1530 | 479 | return (printer != null) ? printer.visit(type, locale) : String.valueOf(type); |
jjg@1530 | 480 | } |
jjg@1530 | 481 | |
jjg@1537 | 482 | protected String hashString(Object obj) { |
jjg@1537 | 483 | return String.format("#%x", obj.hashCode()); |
jjg@1537 | 484 | } |
jjg@1537 | 485 | |
jjg@1537 | 486 | protected String info(Class<?> clazz, Object internal, Object external) { |
jjg@1537 | 487 | return String.format("%s,%s,%s", clazz.getSimpleName(), internal, external); |
jjg@1537 | 488 | } |
jjg@1537 | 489 | |
jjg@1530 | 490 | private int indent = 0; |
jjg@1530 | 491 | |
jjg@1530 | 492 | protected void indent() { |
jjg@1530 | 493 | for (int i = 0; i < indent; i++) { |
jjg@1530 | 494 | out.print(" "); |
jjg@1530 | 495 | } |
jjg@1530 | 496 | } |
jjg@1530 | 497 | |
jjg@1530 | 498 | protected void indent(int n) { |
jjg@1530 | 499 | indent += n; |
jjg@1530 | 500 | } |
jjg@1530 | 501 | |
jjg@1530 | 502 | protected Object getField(Object o, Class<?> clazz, String name) { |
jjg@1530 | 503 | try { |
jjg@1530 | 504 | Field f = clazz.getDeclaredField(name); |
jjg@1530 | 505 | boolean prev = f.isAccessible(); |
jjg@1530 | 506 | f.setAccessible(true); |
jjg@1530 | 507 | try { |
jjg@1530 | 508 | return f.get(o); |
jjg@1530 | 509 | } finally { |
jjg@1530 | 510 | f.setAccessible(prev); |
jjg@1530 | 511 | } |
jjg@1530 | 512 | } catch (ReflectiveOperationException e) { |
jjg@1530 | 513 | return e; |
jjg@1530 | 514 | } catch (SecurityException e) { |
jjg@1530 | 515 | return e; |
jjg@1530 | 516 | } |
jjg@1530 | 517 | } |
jjg@1530 | 518 | |
jjg@1530 | 519 | // </editor-fold> |
jjg@1530 | 520 | |
jjg@1530 | 521 | // <editor-fold defaultstate="collapsed" desc="JCTree visitor methods"> |
jjg@1530 | 522 | |
jjg@1530 | 523 | protected JCTree.Visitor treeVisitor = new TreeVisitor(); |
jjg@1530 | 524 | |
jjg@1530 | 525 | /** |
jjg@1530 | 526 | * Default visitor class for JCTree (AST) objects. |
jjg@1530 | 527 | */ |
jjg@1530 | 528 | public class TreeVisitor extends JCTree.Visitor { |
jjg@1530 | 529 | @Override |
jjg@1530 | 530 | public void visitTopLevel(JCCompilationUnit tree) { |
jjg@1530 | 531 | printList("packageAnnotations", tree.packageAnnotations); |
jjg@1530 | 532 | printTree("pid", tree.pid); |
jjg@1530 | 533 | printList("defs", tree.defs); |
jjg@1530 | 534 | } |
jjg@1530 | 535 | |
jjg@1530 | 536 | @Override |
jjg@1530 | 537 | public void visitImport(JCImport tree) { |
jjg@1530 | 538 | printTree("qualid", tree.qualid); |
jjg@1530 | 539 | } |
jjg@1530 | 540 | |
jjg@1530 | 541 | @Override |
jjg@1530 | 542 | public void visitClassDef(JCClassDecl tree) { |
jjg@1530 | 543 | printName("name", tree.name); |
jjg@1530 | 544 | printTree("mods", tree.mods); |
jjg@1530 | 545 | printList("typarams", tree.typarams); |
jjg@1530 | 546 | printTree("extending", tree.extending); |
jjg@1530 | 547 | printList("implementing", tree.implementing); |
jjg@1530 | 548 | printList("defs", tree.defs); |
jjg@1530 | 549 | } |
jjg@1530 | 550 | |
jjg@1530 | 551 | @Override |
jjg@1530 | 552 | public void visitMethodDef(JCMethodDecl tree) { |
jjg@1530 | 553 | printName("name", tree.name); |
jjg@1530 | 554 | printTree("mods", tree.mods); |
jjg@1530 | 555 | printTree("restype", tree.restype); |
jjg@1530 | 556 | printList("typarams", tree.typarams); |
jjg@1530 | 557 | printTree("recvparam", tree.recvparam); |
jjg@1530 | 558 | printList("params", tree.params); |
jjg@1530 | 559 | printList("thrown", tree.thrown); |
jjg@1530 | 560 | printTree("defaultValue", tree.defaultValue); |
jjg@1530 | 561 | printTree("body", tree.body); |
jjg@1530 | 562 | } |
jjg@1530 | 563 | |
jjg@1530 | 564 | @Override |
jjg@1530 | 565 | public void visitVarDef(JCVariableDecl tree) { |
jjg@1530 | 566 | printName("name", tree.name); |
jjg@1530 | 567 | printTree("mods", tree.mods); |
jjg@1530 | 568 | printTree("vartype", tree.vartype); |
jjg@1530 | 569 | printTree("init", tree.init); |
jjg@1530 | 570 | } |
jjg@1530 | 571 | |
jjg@1530 | 572 | @Override |
jjg@1530 | 573 | public void visitSkip(JCSkip tree) { |
jjg@1530 | 574 | } |
jjg@1530 | 575 | |
jjg@1530 | 576 | @Override |
jjg@1530 | 577 | public void visitBlock(JCBlock tree) { |
jjg@1530 | 578 | printList("stats", tree.stats); |
jjg@1530 | 579 | } |
jjg@1530 | 580 | |
jjg@1530 | 581 | @Override |
jjg@1530 | 582 | public void visitDoLoop(JCDoWhileLoop tree) { |
jjg@1530 | 583 | printTree("body", tree.body); |
jjg@1530 | 584 | printTree("cond", tree.cond); |
jjg@1530 | 585 | } |
jjg@1530 | 586 | |
jjg@1530 | 587 | @Override |
jjg@1530 | 588 | public void visitWhileLoop(JCWhileLoop tree) { |
jjg@1530 | 589 | printTree("cond", tree.cond); |
jjg@1530 | 590 | printTree("body", tree.body); |
jjg@1530 | 591 | } |
jjg@1530 | 592 | |
jjg@1530 | 593 | @Override |
jjg@1530 | 594 | public void visitForLoop(JCForLoop tree) { |
jjg@1530 | 595 | printList("init", tree.init); |
jjg@1530 | 596 | printTree("cond", tree.cond); |
jjg@1530 | 597 | printList("step", tree.step); |
jjg@1530 | 598 | printTree("body", tree.body); |
jjg@1530 | 599 | } |
jjg@1530 | 600 | |
jjg@1530 | 601 | @Override |
jjg@1530 | 602 | public void visitForeachLoop(JCEnhancedForLoop tree) { |
jjg@1530 | 603 | printTree("var", tree.var); |
jjg@1530 | 604 | printTree("expr", tree.expr); |
jjg@1530 | 605 | printTree("body", tree.body); |
jjg@1530 | 606 | } |
jjg@1530 | 607 | |
jjg@1530 | 608 | @Override |
jjg@1530 | 609 | public void visitLabelled(JCLabeledStatement tree) { |
jjg@1530 | 610 | printTree("body", tree.body); |
jjg@1530 | 611 | } |
jjg@1530 | 612 | |
jjg@1530 | 613 | @Override |
jjg@1530 | 614 | public void visitSwitch(JCSwitch tree) { |
jjg@1530 | 615 | printTree("selector", tree.selector); |
jjg@1530 | 616 | printList("cases", tree.cases); |
jjg@1530 | 617 | } |
jjg@1530 | 618 | |
jjg@1530 | 619 | @Override |
jjg@1530 | 620 | public void visitCase(JCCase tree) { |
jjg@1530 | 621 | printTree("pat", tree.pat); |
jjg@1530 | 622 | printList("stats", tree.stats); |
jjg@1530 | 623 | } |
jjg@1530 | 624 | |
jjg@1530 | 625 | @Override |
jjg@1530 | 626 | public void visitSynchronized(JCSynchronized tree) { |
jjg@1530 | 627 | printTree("lock", tree.lock); |
jjg@1530 | 628 | printTree("body", tree.body); |
jjg@1530 | 629 | } |
jjg@1530 | 630 | |
jjg@1530 | 631 | @Override |
jjg@1530 | 632 | public void visitTry(JCTry tree) { |
jjg@1530 | 633 | printList("resources", tree.resources); |
jjg@1530 | 634 | printTree("body", tree.body); |
jjg@1530 | 635 | printList("catchers", tree.catchers); |
jjg@1530 | 636 | printTree("finalizer", tree.finalizer); |
jjg@1530 | 637 | } |
jjg@1530 | 638 | |
jjg@1530 | 639 | @Override |
jjg@1530 | 640 | public void visitCatch(JCCatch tree) { |
jjg@1530 | 641 | printTree("param", tree.param); |
jjg@1530 | 642 | printTree("body", tree.body); |
jjg@1530 | 643 | } |
jjg@1530 | 644 | |
jjg@1530 | 645 | @Override |
jjg@1530 | 646 | public void visitConditional(JCConditional tree) { |
jjg@1530 | 647 | printTree("cond", tree.cond); |
jjg@1530 | 648 | printTree("truepart", tree.truepart); |
jjg@1530 | 649 | printTree("falsepart", tree.falsepart); |
jjg@1530 | 650 | } |
jjg@1530 | 651 | |
jjg@1530 | 652 | @Override |
jjg@1530 | 653 | public void visitIf(JCIf tree) { |
jjg@1530 | 654 | printTree("cond", tree.cond); |
jjg@1530 | 655 | printTree("thenpart", tree.thenpart); |
jjg@1530 | 656 | printTree("elsepart", tree.elsepart); |
jjg@1530 | 657 | } |
jjg@1530 | 658 | |
jjg@1530 | 659 | @Override |
jjg@1530 | 660 | public void visitExec(JCExpressionStatement tree) { |
jjg@1530 | 661 | printTree("expr", tree.expr); |
jjg@1530 | 662 | } |
jjg@1530 | 663 | |
jjg@1530 | 664 | @Override |
jjg@1530 | 665 | public void visitBreak(JCBreak tree) { |
jjg@1530 | 666 | printName("label", tree.label); |
jjg@1530 | 667 | } |
jjg@1530 | 668 | |
jjg@1530 | 669 | @Override |
jjg@1530 | 670 | public void visitContinue(JCContinue tree) { |
jjg@1530 | 671 | printName("label", tree.label); |
jjg@1530 | 672 | } |
jjg@1530 | 673 | |
jjg@1530 | 674 | @Override |
jjg@1530 | 675 | public void visitReturn(JCReturn tree) { |
jjg@1530 | 676 | printTree("expr", tree.expr); |
jjg@1530 | 677 | } |
jjg@1530 | 678 | |
jjg@1530 | 679 | @Override |
jjg@1530 | 680 | public void visitThrow(JCThrow tree) { |
jjg@1530 | 681 | printTree("expr", tree.expr); |
jjg@1530 | 682 | } |
jjg@1530 | 683 | |
jjg@1530 | 684 | @Override |
jjg@1530 | 685 | public void visitAssert(JCAssert tree) { |
jjg@1530 | 686 | printTree("cond", tree.cond); |
jjg@1530 | 687 | printTree("detail", tree.detail); |
jjg@1530 | 688 | } |
jjg@1530 | 689 | |
jjg@1530 | 690 | @Override |
jjg@1530 | 691 | public void visitApply(JCMethodInvocation tree) { |
jjg@1530 | 692 | printList("typeargs", tree.typeargs); |
jjg@1530 | 693 | printTree("meth", tree.meth); |
jjg@1530 | 694 | printList("args", tree.args); |
jjg@1530 | 695 | } |
jjg@1530 | 696 | |
jjg@1530 | 697 | @Override |
jjg@1530 | 698 | public void visitNewClass(JCNewClass tree) { |
jjg@1530 | 699 | printTree("encl", tree.encl); |
jjg@1530 | 700 | printList("typeargs", tree.typeargs); |
jjg@1530 | 701 | printTree("clazz", tree.clazz); |
jjg@1530 | 702 | printList("args", tree.args); |
jjg@1530 | 703 | printTree("def", tree.def); |
jjg@1530 | 704 | } |
jjg@1530 | 705 | |
jjg@1530 | 706 | @Override |
jjg@1530 | 707 | public void visitNewArray(JCNewArray tree) { |
jjg@1530 | 708 | printList("annotations", tree.annotations); |
jjg@1530 | 709 | printTree("elemtype", tree.elemtype); |
jjg@1530 | 710 | printList("dims", tree.dims); |
jjg@1530 | 711 | printList("dimAnnotations", tree.dimAnnotations); |
jjg@1530 | 712 | printList("elems", tree.elems); |
jjg@1530 | 713 | } |
jjg@1530 | 714 | |
jjg@1530 | 715 | @Override |
jjg@1530 | 716 | public void visitLambda(JCLambda tree) { |
jjg@1530 | 717 | printTree("body", tree.body); |
jjg@1530 | 718 | printList("params", tree.params); |
jjg@1530 | 719 | } |
jjg@1530 | 720 | |
jjg@1530 | 721 | @Override |
jjg@1530 | 722 | public void visitParens(JCParens tree) { |
jjg@1530 | 723 | printTree("expr", tree.expr); |
jjg@1530 | 724 | } |
jjg@1530 | 725 | |
jjg@1530 | 726 | @Override |
jjg@1530 | 727 | public void visitAssign(JCAssign tree) { |
jjg@1530 | 728 | printTree("lhs", tree.lhs); |
jjg@1530 | 729 | printTree("rhs", tree.rhs); |
jjg@1530 | 730 | } |
jjg@1530 | 731 | |
jjg@1530 | 732 | @Override |
jjg@1530 | 733 | public void visitAssignop(JCAssignOp tree) { |
jjg@1530 | 734 | printTree("lhs", tree.lhs); |
jjg@1530 | 735 | printTree("rhs", tree.rhs); |
jjg@1530 | 736 | } |
jjg@1530 | 737 | |
jjg@1530 | 738 | @Override |
jjg@1530 | 739 | public void visitUnary(JCUnary tree) { |
jjg@1530 | 740 | printTree("arg", tree.arg); |
jjg@1530 | 741 | } |
jjg@1530 | 742 | |
jjg@1530 | 743 | @Override |
jjg@1530 | 744 | public void visitBinary(JCBinary tree) { |
jjg@1530 | 745 | printTree("lhs", tree.lhs); |
jjg@1530 | 746 | printTree("rhs", tree.rhs); |
jjg@1530 | 747 | } |
jjg@1530 | 748 | |
jjg@1530 | 749 | @Override |
jjg@1530 | 750 | public void visitTypeCast(JCTypeCast tree) { |
jjg@1530 | 751 | printTree("clazz", tree.clazz); |
jjg@1530 | 752 | printTree("expr", tree.expr); |
jjg@1530 | 753 | } |
jjg@1530 | 754 | |
jjg@1530 | 755 | @Override |
jjg@1530 | 756 | public void visitTypeTest(JCInstanceOf tree) { |
jjg@1530 | 757 | printTree("expr", tree.expr); |
jjg@1530 | 758 | printTree("clazz", tree.clazz); |
jjg@1530 | 759 | } |
jjg@1530 | 760 | |
jjg@1530 | 761 | @Override |
jjg@1530 | 762 | public void visitIndexed(JCArrayAccess tree) { |
jjg@1530 | 763 | printTree("indexed", tree.indexed); |
jjg@1530 | 764 | printTree("index", tree.index); |
jjg@1530 | 765 | } |
jjg@1530 | 766 | |
jjg@1530 | 767 | @Override |
jjg@1530 | 768 | public void visitSelect(JCFieldAccess tree) { |
jjg@1530 | 769 | printTree("selected", tree.selected); |
jjg@1530 | 770 | } |
jjg@1530 | 771 | |
jjg@1530 | 772 | @Override |
jjg@1530 | 773 | public void visitReference(JCMemberReference tree) { |
jjg@1530 | 774 | printTree("expr", tree.expr); |
jjg@1530 | 775 | printList("typeargs", tree.typeargs); |
jjg@1530 | 776 | } |
jjg@1530 | 777 | |
jjg@1530 | 778 | @Override |
jjg@1530 | 779 | public void visitIdent(JCIdent tree) { |
jjg@1530 | 780 | printName("name", tree.name); |
jjg@1530 | 781 | } |
jjg@1530 | 782 | |
jjg@1530 | 783 | @Override |
jjg@1530 | 784 | public void visitLiteral(JCLiteral tree) { |
jjg@1530 | 785 | printString("value", Pretty.toSimpleString(tree, 32)); |
jjg@1530 | 786 | } |
jjg@1530 | 787 | |
jjg@1530 | 788 | @Override |
jjg@1530 | 789 | public void visitTypeIdent(JCPrimitiveTypeTree tree) { |
jjg@1530 | 790 | printString("typetag", tree.typetag.name()); |
jjg@1530 | 791 | } |
jjg@1530 | 792 | |
jjg@1530 | 793 | @Override |
jjg@1530 | 794 | public void visitTypeArray(JCArrayTypeTree tree) { |
jjg@1530 | 795 | printTree("elemtype", tree.elemtype); |
jjg@1530 | 796 | } |
jjg@1530 | 797 | |
jjg@1530 | 798 | @Override |
jjg@1530 | 799 | public void visitTypeApply(JCTypeApply tree) { |
jjg@1530 | 800 | printTree("clazz", tree.clazz); |
jjg@1530 | 801 | printList("arguments", tree.arguments); |
jjg@1530 | 802 | } |
jjg@1530 | 803 | |
jjg@1530 | 804 | @Override |
jjg@1530 | 805 | public void visitTypeUnion(JCTypeUnion tree) { |
jjg@1530 | 806 | printList("alternatives", tree.alternatives); |
jjg@1530 | 807 | } |
jjg@1530 | 808 | |
jjg@1530 | 809 | @Override |
jjg@1530 | 810 | public void visitTypeIntersection(JCTypeIntersection tree) { |
jjg@1530 | 811 | printList("bounds", tree.bounds); |
jjg@1530 | 812 | } |
jjg@1530 | 813 | |
jjg@1530 | 814 | @Override |
jjg@1530 | 815 | public void visitTypeParameter(JCTypeParameter tree) { |
jjg@1530 | 816 | printName("name", tree.name); |
jjg@1530 | 817 | printList("annotations", tree.annotations); |
jjg@1530 | 818 | printList("bounds", tree.bounds); |
jjg@1530 | 819 | } |
jjg@1530 | 820 | |
jjg@1530 | 821 | @Override |
jjg@1530 | 822 | public void visitWildcard(JCWildcard tree) { |
jjg@1530 | 823 | printTree("kind", tree.kind); |
jjg@1530 | 824 | printTree("inner", tree.inner); |
jjg@1530 | 825 | } |
jjg@1530 | 826 | |
jjg@1530 | 827 | @Override |
jjg@1530 | 828 | public void visitTypeBoundKind(TypeBoundKind tree) { |
jjg@1530 | 829 | printString("kind", tree.kind.name()); |
jjg@1530 | 830 | } |
jjg@1530 | 831 | |
jjg@1530 | 832 | @Override |
jjg@1530 | 833 | public void visitModifiers(JCModifiers tree) { |
jjg@1530 | 834 | printList("annotations", tree.annotations); |
jjg@1530 | 835 | printString("flags", String.valueOf(Flags.asFlagSet(tree.flags))); |
jjg@1530 | 836 | } |
jjg@1530 | 837 | |
jjg@1530 | 838 | @Override |
jjg@1530 | 839 | public void visitAnnotation(JCAnnotation tree) { |
jjg@1530 | 840 | printTree("annotationType", tree.annotationType); |
jjg@1530 | 841 | printList("args", tree.args); |
jjg@1530 | 842 | } |
jjg@1530 | 843 | |
jjg@1530 | 844 | @Override |
jjg@1530 | 845 | public void visitAnnotatedType(JCAnnotatedType tree) { |
jjg@1530 | 846 | printList("annotations", tree.annotations); |
jjg@1530 | 847 | printTree("underlyingType", tree.underlyingType); |
jjg@1530 | 848 | } |
jjg@1530 | 849 | |
jjg@1530 | 850 | @Override |
jjg@1530 | 851 | public void visitErroneous(JCErroneous tree) { |
jjg@1530 | 852 | printList("errs", tree.errs); |
jjg@1530 | 853 | } |
jjg@1530 | 854 | |
jjg@1530 | 855 | @Override |
jjg@1530 | 856 | public void visitLetExpr(LetExpr tree) { |
jjg@1530 | 857 | printList("defs", tree.defs); |
jjg@1530 | 858 | printTree("expr", tree.expr); |
jjg@1530 | 859 | } |
jjg@1530 | 860 | |
jjg@1530 | 861 | @Override |
jjg@1530 | 862 | public void visitTree(JCTree tree) { |
jjg@1530 | 863 | Assert.error(); |
jjg@1530 | 864 | } |
jjg@1530 | 865 | } |
jjg@1530 | 866 | |
jjg@1530 | 867 | // </editor-fold> |
jjg@1530 | 868 | |
jjg@1530 | 869 | // <editor-fold defaultstate="collapsed" desc="Symbol visitor"> |
jjg@1530 | 870 | |
jjg@1537 | 871 | protected Symbol.Visitor<Void,Void> symVisitor = new SymbolVisitor(); |
jjg@1530 | 872 | |
jjg@1530 | 873 | /** |
jjg@1530 | 874 | * Default visitor class for Symbol objects. |
jjg@1530 | 875 | * Note: each visitXYZ method ends by calling the corresponding |
jjg@1530 | 876 | * visit method for its superclass. |
jjg@1530 | 877 | */ |
jjg@1537 | 878 | class SymbolVisitor implements Symbol.Visitor<Void,Void> { |
jjg@1530 | 879 | @Override |
jjg@1537 | 880 | public Void visitClassSymbol(ClassSymbol sym, Void ignore) { |
jjg@1530 | 881 | printName("fullname", sym.fullname); |
jjg@1530 | 882 | printName("flatname", sym.flatname); |
jjg@1530 | 883 | printScope("members", sym.members_field); |
jjg@1530 | 884 | printFileObject("sourcefile", sym.sourcefile); |
jjg@1530 | 885 | printFileObject("classfile", sym.classfile); |
jjg@1530 | 886 | // trans-local? |
jjg@1530 | 887 | // pool? |
jjg@1537 | 888 | return visitTypeSymbol(sym, null); |
jjg@1530 | 889 | } |
jjg@1530 | 890 | |
jjg@1530 | 891 | @Override |
jjg@1537 | 892 | public Void visitMethodSymbol(MethodSymbol sym, Void ignore) { |
jjg@1530 | 893 | // code |
jjg@1530 | 894 | printList("params", sym.params); |
jjg@1530 | 895 | printList("savedParameterNames", sym.savedParameterNames); |
jjg@1537 | 896 | return visitSymbol(sym, null); |
jjg@1530 | 897 | } |
jjg@1530 | 898 | |
jjg@1530 | 899 | @Override |
jjg@1537 | 900 | public Void visitPackageSymbol(PackageSymbol sym, Void ignore) { |
jjg@1530 | 901 | printName("fullname", sym.fullname); |
jjg@1530 | 902 | printScope("members", sym.members_field); |
jjg@1530 | 903 | printSymbol("package-info", sym.package_info, Details.SUMMARY); |
jjg@1537 | 904 | return visitTypeSymbol(sym, null); |
jjg@1530 | 905 | } |
jjg@1530 | 906 | |
jjg@1530 | 907 | @Override |
jjg@1537 | 908 | public Void visitOperatorSymbol(OperatorSymbol sym, Void ignore) { |
jjg@1530 | 909 | printInt("opcode", sym.opcode); |
jjg@1537 | 910 | return visitMethodSymbol(sym, null); |
jjg@1530 | 911 | } |
jjg@1530 | 912 | |
jjg@1530 | 913 | @Override |
jjg@1537 | 914 | public Void visitVarSymbol(VarSymbol sym, Void ignore) { |
jjg@1530 | 915 | printInt("pos", sym.pos); |
jjg@1530 | 916 | printInt("adm", sym.adr); |
jjg@1530 | 917 | // data is a private field, and the standard accessors may |
jjg@1530 | 918 | // mutate it as part of lazy evaluation. Therefore, use |
jjg@1530 | 919 | // reflection to get the raw data. |
jjg@1530 | 920 | printObject("data", getField(sym, VarSymbol.class, "data"), Details.SUMMARY); |
jjg@1537 | 921 | return visitSymbol(sym, null); |
jjg@1530 | 922 | } |
jjg@1530 | 923 | |
jjg@1530 | 924 | @Override |
jjg@1537 | 925 | public Void visitTypeSymbol(TypeSymbol sym, Void ignore) { |
jjg@1537 | 926 | return visitSymbol(sym, null); |
jjg@1530 | 927 | } |
jjg@1530 | 928 | |
jjg@1530 | 929 | @Override |
jjg@1537 | 930 | public Void visitSymbol(Symbol sym, Void ignore) { |
jjg@1530 | 931 | return null; |
jjg@1530 | 932 | } |
jjg@1530 | 933 | } |
jjg@1530 | 934 | |
jjg@1530 | 935 | // </editor-fold> |
jjg@1530 | 936 | |
jjg@1530 | 937 | // <editor-fold defaultstate="collapsed" desc="Type visitor"> |
jjg@1530 | 938 | |
jjg@1537 | 939 | protected Type.Visitor<Void,Void> typeVisitor = new TypeVisitor(); |
jjg@1530 | 940 | |
jjg@1530 | 941 | /** |
jjg@1530 | 942 | * Default visitor class for Type objects. |
jjg@1530 | 943 | * Note: each visitXYZ method ends by calling the corresponding |
jjg@1530 | 944 | * visit method for its superclass. |
jjg@1530 | 945 | */ |
jjg@1537 | 946 | public class TypeVisitor implements Type.Visitor<Void,Void> { |
jjg@1537 | 947 | public Void visitAnnotatedType(AnnotatedType type, Void ignore) { |
jjg@1530 | 948 | printList("typeAnnotations", type.typeAnnotations); |
jjg@1530 | 949 | printType("underlyingType", type.underlyingType, Details.FULL); |
jjg@1537 | 950 | return visitType(type, null); |
jjg@1530 | 951 | } |
jjg@1530 | 952 | |
jjg@1537 | 953 | public Void visitArrayType(ArrayType type, Void ignore) { |
jjg@1530 | 954 | printType("elemType", type.elemtype, Details.FULL); |
jjg@1537 | 955 | return visitType(type, null); |
jjg@1530 | 956 | } |
jjg@1530 | 957 | |
jjg@1537 | 958 | public Void visitCapturedType(CapturedType type, Void ignore) { |
jjg@1530 | 959 | printType("wildcard", type.wildcard, Details.FULL); |
jjg@1537 | 960 | return visitTypeVar(type, null); |
jjg@1530 | 961 | } |
jjg@1530 | 962 | |
jjg@1537 | 963 | public Void visitClassType(ClassType type, Void ignore) { |
jjg@1530 | 964 | printType("outer", type.getEnclosingType(), Details.SUMMARY); |
jjg@1530 | 965 | printList("typarams", type.typarams_field); |
jjg@1530 | 966 | printList("allparams", type.allparams_field); |
jjg@1530 | 967 | printType("supertype", type.supertype_field, Details.SUMMARY); |
jjg@1530 | 968 | printList("interfaces", type.interfaces_field); |
jjg@1530 | 969 | printList("allinterfaces", type.all_interfaces_field); |
jjg@1537 | 970 | return visitType(type, null); |
jjg@1530 | 971 | } |
jjg@1530 | 972 | |
jjg@1537 | 973 | public Void visitErrorType(ErrorType type, Void ignore) { |
jjg@1530 | 974 | printType("originalType", type.getOriginalType(), Details.FULL); |
jjg@1537 | 975 | return visitClassType(type, null); |
jjg@1530 | 976 | } |
jjg@1530 | 977 | |
jjg@1537 | 978 | public Void visitForAll(ForAll type, Void ignore) { |
jjg@1530 | 979 | printList("tvars", type.tvars); |
jjg@1530 | 980 | return visitDelegatedType(type); |
jjg@1530 | 981 | } |
jjg@1530 | 982 | |
jjg@1537 | 983 | public Void visitMethodType(MethodType type, Void ignore) { |
jjg@1530 | 984 | printList("argtypes", type.argtypes); |
jjg@1530 | 985 | printType("restype", type.restype, Details.FULL); |
jjg@1530 | 986 | printList("thrown", type.thrown); |
jjg@1537 | 987 | return visitType(type, null); |
jjg@1530 | 988 | } |
jjg@1530 | 989 | |
jjg@1537 | 990 | public Void visitPackageType(PackageType type, Void ignore) { |
jjg@1537 | 991 | return visitType(type, null); |
jjg@1530 | 992 | } |
jjg@1530 | 993 | |
jjg@1537 | 994 | public Void visitTypeVar(TypeVar type, Void ignore) { |
jjg@1530 | 995 | // For TypeVars (and not subtypes), the bound should always be |
jjg@1530 | 996 | // null or bot. So, only print the bound for subtypes of TypeVar, |
jjg@1530 | 997 | // or if the bound is (erroneously) not null or bot. |
jjg@1530 | 998 | if (!type.hasTag(TypeTag.TYPEVAR) |
jjg@1530 | 999 | || !(type.bound == null || type.bound.hasTag(TypeTag.BOT))) { |
jjg@1530 | 1000 | printType("bound", type.bound, Details.FULL); |
jjg@1530 | 1001 | } |
jjg@1530 | 1002 | printType("lower", type.lower, Details.FULL); |
jjg@1537 | 1003 | return visitType(type, null); |
jjg@1530 | 1004 | } |
jjg@1530 | 1005 | |
jjg@1537 | 1006 | public Void visitUndetVar(UndetVar type, Void ignore) { |
jjg@1530 | 1007 | for (UndetVar.InferenceBound ib: UndetVar.InferenceBound.values()) |
jjg@1530 | 1008 | printList("bounds." + ib, type.getBounds(ib)); |
mcimadamore@1550 | 1009 | printInt("declaredCount", type.declaredCount); |
jjg@1530 | 1010 | printType("inst", type.inst, Details.SUMMARY); |
jjg@1530 | 1011 | return visitDelegatedType(type); |
jjg@1530 | 1012 | } |
jjg@1530 | 1013 | |
jjg@1537 | 1014 | public Void visitWildcardType(WildcardType type, Void ignore) { |
jjg@1530 | 1015 | printType("type", type.type, Details.SUMMARY); |
jjg@1530 | 1016 | printString("kind", type.kind.name()); |
jjg@1530 | 1017 | printType("bound", type.bound, Details.SUMMARY); |
jjg@1537 | 1018 | return visitType(type, null); |
jjg@1530 | 1019 | } |
jjg@1530 | 1020 | |
jjg@1530 | 1021 | protected Void visitDelegatedType(DelegatedType type) { |
jjg@1530 | 1022 | printType("qtype", type.qtype, Details.FULL); |
jjg@1537 | 1023 | return visitType(type, null); |
jjg@1530 | 1024 | } |
jjg@1530 | 1025 | |
jjg@1537 | 1026 | public Void visitType(Type type, Void ignore) { |
jjg@1530 | 1027 | return null; |
jjg@1530 | 1028 | } |
jjg@1530 | 1029 | } |
jjg@1530 | 1030 | |
jjg@1530 | 1031 | // </editor-fold> |
jjg@1530 | 1032 | |
jjg@1530 | 1033 | // <editor-fold defaultstate="collapsed" desc="Attribute (annotations) visitor"> |
jjg@1530 | 1034 | |
jjg@1530 | 1035 | protected Attribute.Visitor attrVisitor = new AttributeVisitor(); |
jjg@1530 | 1036 | |
jjg@1530 | 1037 | /** |
jjg@1530 | 1038 | * Default visitor class for Attribute (annotation) objects. |
jjg@1530 | 1039 | */ |
jjg@1530 | 1040 | public class AttributeVisitor implements Attribute.Visitor { |
jjg@1530 | 1041 | |
jjg@1530 | 1042 | public void visitConstant(Attribute.Constant a) { |
jjg@1530 | 1043 | printObject("value", a.value, Details.SUMMARY); |
jjg@1530 | 1044 | visitAttribute(a); |
jjg@1530 | 1045 | } |
jjg@1530 | 1046 | |
jjg@1530 | 1047 | public void visitClass(Attribute.Class a) { |
jjg@1530 | 1048 | printObject("classType", a.classType, Details.SUMMARY); |
jjg@1530 | 1049 | visitAttribute(a); |
jjg@1530 | 1050 | } |
jjg@1530 | 1051 | |
jjg@1530 | 1052 | public void visitCompound(Attribute.Compound a) { |
jjg@1530 | 1053 | if (a instanceof Attribute.TypeCompound) { |
jjg@1530 | 1054 | Attribute.TypeCompound ta = (Attribute.TypeCompound) a; |
jjg@1530 | 1055 | // consider a custom printer? |
jjg@1530 | 1056 | printObject("position", ta.position, Details.SUMMARY); |
jjg@1530 | 1057 | } |
jjg@1530 | 1058 | printObject("synthesized", a.isSynthesized(), Details.SUMMARY); |
jjg@1530 | 1059 | printList("values", a.values); |
jjg@1530 | 1060 | visitAttribute(a); |
jjg@1530 | 1061 | } |
jjg@1530 | 1062 | |
jjg@1530 | 1063 | public void visitArray(Attribute.Array a) { |
jjg@1530 | 1064 | printList("values", Arrays.asList(a.values)); |
jjg@1530 | 1065 | visitAttribute(a); |
jjg@1530 | 1066 | } |
jjg@1530 | 1067 | |
jjg@1530 | 1068 | public void visitEnum(Attribute.Enum a) { |
jjg@1530 | 1069 | printSymbol("value", a.value, Details.SUMMARY); |
jjg@1530 | 1070 | visitAttribute(a); |
jjg@1530 | 1071 | } |
jjg@1530 | 1072 | |
jjg@1530 | 1073 | public void visitError(Attribute.Error a) { |
jjg@1530 | 1074 | visitAttribute(a); |
jjg@1530 | 1075 | } |
jjg@1530 | 1076 | |
jjg@1530 | 1077 | public void visitAttribute(Attribute a) { |
jjg@1530 | 1078 | printType("type", a.type, Details.SUMMARY); |
jjg@1530 | 1079 | } |
jjg@1530 | 1080 | |
jjg@1530 | 1081 | } |
jjg@1530 | 1082 | // </editor-fold> |
jjg@1530 | 1083 | |
jjg@1530 | 1084 | // <editor-fold defaultstate="collapsed" desc="Utility front end"> |
jjg@1530 | 1085 | |
jjg@1530 | 1086 | /** |
jjg@1530 | 1087 | * Utility class to invoke DPrinter from the command line. |
jjg@1530 | 1088 | */ |
jjg@1530 | 1089 | static class Main { |
jjg@1530 | 1090 | public static void main(String... args) throws IOException { |
jjg@1538 | 1091 | Main m = new Main(); |
jjg@1530 | 1092 | PrintWriter out = new PrintWriter(System.out); |
jjg@1530 | 1093 | try { |
jjg@1530 | 1094 | if (args.length == 0) |
jjg@1538 | 1095 | m.usage(out); |
jjg@1530 | 1096 | else |
jjg@1538 | 1097 | m.run(out, args); |
jjg@1530 | 1098 | } finally { |
jjg@1530 | 1099 | out.flush(); |
jjg@1530 | 1100 | } |
jjg@1530 | 1101 | } |
jjg@1530 | 1102 | |
jjg@1538 | 1103 | void usage(PrintWriter out) { |
jjg@1530 | 1104 | out.println("Usage:"); |
jjg@1530 | 1105 | out.println(" java " + Main.class.getName() + " mode [options] [javac-options]"); |
jjg@1538 | 1106 | out.print("where mode is one of: "); |
jjg@1538 | 1107 | String sep = ""; |
jjg@1538 | 1108 | for (Handler h: getHandlers().values()) { |
jjg@1538 | 1109 | out.print(sep); |
jjg@1538 | 1110 | out.print(h.name); |
jjg@1538 | 1111 | sep = ", "; |
jjg@1538 | 1112 | } |
jjg@1538 | 1113 | out.println(); |
jjg@1538 | 1114 | out.println("and where options include:"); |
jjg@1530 | 1115 | out.println(" -before PARSE|ENTER|ANALYZE|GENERATE|ANNOTATION_PROCESSING|ANNOTATION_PROCESSING_ROUND"); |
jjg@1530 | 1116 | out.println(" -after PARSE|ENTER|ANALYZE|GENERATE|ANNOTATION_PROCESSING|ANNOTATION_PROCESSING_ROUND"); |
jjg@1530 | 1117 | out.println(" -showPositions"); |
jjg@1530 | 1118 | out.println(" -showSource"); |
jjg@1530 | 1119 | out.println(" -showTreeSymbols"); |
jjg@1530 | 1120 | out.println(" -showTreeTypes"); |
jjg@1530 | 1121 | out.println(" -hideEmptyItems"); |
jjg@1530 | 1122 | out.println(" -hideNulls"); |
jjg@1530 | 1123 | } |
jjg@1530 | 1124 | |
jjg@1530 | 1125 | void run(PrintWriter out, String... args) throws IOException { |
jjg@1530 | 1126 | JavaCompiler c = ToolProvider.getSystemJavaCompiler(); |
jjg@1530 | 1127 | StandardJavaFileManager fm = c.getStandardFileManager(null, null, null); |
jjg@1530 | 1128 | |
jjg@1530 | 1129 | // DPrinter options |
jjg@1530 | 1130 | final Set<TaskEvent.Kind> before = EnumSet.noneOf(TaskEvent.Kind.class); |
jjg@1530 | 1131 | final Set<TaskEvent.Kind> after = EnumSet.noneOf(TaskEvent.Kind.class); |
jjg@1530 | 1132 | boolean showPositions = false; |
jjg@1530 | 1133 | boolean showSource = false; |
jjg@1530 | 1134 | boolean showTreeSymbols = false; |
jjg@1530 | 1135 | boolean showTreeTypes = false; |
jjg@1530 | 1136 | boolean showEmptyItems = true; |
jjg@1530 | 1137 | boolean showNulls = true; |
jjg@1530 | 1138 | |
jjg@1530 | 1139 | // javac options |
jjg@1530 | 1140 | Collection<String> options = new ArrayList<String>(); |
jjg@1530 | 1141 | Collection<File> files = new ArrayList<File>(); |
jjg@1530 | 1142 | String classpath = null; |
jjg@1530 | 1143 | String classoutdir = null; |
jjg@1530 | 1144 | |
jjg@1530 | 1145 | final Handler h = getHandlers().get(args[0]); |
jjg@1530 | 1146 | if (h == null) |
jjg@1530 | 1147 | throw new IllegalArgumentException(args[0]); |
jjg@1530 | 1148 | |
jjg@1530 | 1149 | for (int i = 1; i < args.length; i++) { |
jjg@1530 | 1150 | String arg = args[i]; |
jjg@1530 | 1151 | if (arg.equals("-before") && i + 1 < args.length) { |
jjg@1530 | 1152 | before.add(getKind(args[++i])); |
jjg@1530 | 1153 | } else if (arg.equals("-after") && i + 1 < args.length) { |
jjg@1530 | 1154 | after.add(getKind(args[++i])); |
jjg@1530 | 1155 | } else if (arg.equals("-showPositions")) { |
jjg@1530 | 1156 | showPositions = true; |
jjg@1530 | 1157 | } else if (arg.equals("-showSource")) { |
jjg@1530 | 1158 | showSource = true; |
jjg@1530 | 1159 | } else if (arg.equals("-showTreeSymbols")) { |
jjg@1530 | 1160 | showTreeSymbols = true; |
jjg@1530 | 1161 | } else if (arg.equals("-showTreeTypes")) { |
jjg@1530 | 1162 | showTreeTypes = true; |
jjg@1530 | 1163 | } else if (arg.equals("-hideEmptyLists")) { |
jjg@1530 | 1164 | showEmptyItems = false; |
jjg@1530 | 1165 | } else if (arg.equals("-hideNulls")) { |
jjg@1530 | 1166 | showNulls = false; |
jjg@1530 | 1167 | } else if (arg.equals("-classpath") && i + 1 < args.length) { |
jjg@1530 | 1168 | classpath = args[++i]; |
jjg@1530 | 1169 | } else if (arg.equals("-d") && i + 1 < args.length) { |
jjg@1530 | 1170 | classoutdir = args[++i]; |
jjg@1530 | 1171 | } else if (arg.startsWith("-")) { |
jjg@1530 | 1172 | int n = c.isSupportedOption(arg); |
jjg@1530 | 1173 | if (n < 0) throw new IllegalArgumentException(arg); |
jjg@1530 | 1174 | options.add(arg); |
jjg@1530 | 1175 | while (n > 0) options.add(args[++i]); |
jjg@1530 | 1176 | } else if (arg.endsWith(".java")) { |
jjg@1530 | 1177 | files.add(new File(arg)); |
jjg@1530 | 1178 | } |
jjg@1530 | 1179 | } |
jjg@1530 | 1180 | |
jjg@1530 | 1181 | if (classoutdir != null) { |
jjg@1530 | 1182 | fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(classoutdir))); |
jjg@1530 | 1183 | } |
jjg@1530 | 1184 | |
jjg@1530 | 1185 | if (classpath != null) { |
jjg@1530 | 1186 | Collection<File> path = new ArrayList<File>(); |
jjg@1530 | 1187 | for (String p: classpath.split(File.pathSeparator)) { |
jjg@1530 | 1188 | if (p.isEmpty()) continue; |
jjg@1530 | 1189 | File f = new File(p); |
jjg@1530 | 1190 | if (f.exists()) path.add(f); |
jjg@1530 | 1191 | } |
jjg@1530 | 1192 | fm.setLocation(StandardLocation.CLASS_PATH, path); |
jjg@1530 | 1193 | } |
jjg@1530 | 1194 | Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files); |
jjg@1530 | 1195 | |
jjg@1530 | 1196 | JavacTask task = (JavacTask) c.getTask(out, fm, null, options, null, fos); |
jjg@1530 | 1197 | final Trees trees = Trees.instance(task); |
jjg@1530 | 1198 | |
jjg@1530 | 1199 | final DPrinter dprinter = new DPrinter(out, trees); |
jjg@1530 | 1200 | dprinter.source(showSource) |
jjg@1530 | 1201 | .emptyItems(showEmptyItems) |
jjg@1530 | 1202 | .nulls(showNulls) |
jjg@1530 | 1203 | .positions(showPositions) |
jjg@1530 | 1204 | .treeSymbols(showTreeSymbols) |
jjg@1530 | 1205 | .treeTypes(showTreeTypes); |
jjg@1530 | 1206 | |
jjg@1530 | 1207 | if (before.isEmpty() && after.isEmpty()) { |
jjg@1530 | 1208 | if (h.name.equals("trees") && !showTreeSymbols && !showTreeTypes) |
jjg@1530 | 1209 | after.add(TaskEvent.Kind.PARSE); |
jjg@1530 | 1210 | else |
jjg@1530 | 1211 | after.add(TaskEvent.Kind.ANALYZE); |
jjg@1530 | 1212 | } |
jjg@1530 | 1213 | |
jjg@1530 | 1214 | task.addTaskListener(new TaskListener() { |
jjg@1530 | 1215 | public void started(TaskEvent e) { |
jjg@1530 | 1216 | if (before.contains(e.getKind())) |
jjg@1530 | 1217 | handle(e); |
jjg@1530 | 1218 | } |
jjg@1530 | 1219 | |
jjg@1530 | 1220 | public void finished(TaskEvent e) { |
jjg@1530 | 1221 | if (after.contains(e.getKind())) |
jjg@1530 | 1222 | handle(e); |
jjg@1530 | 1223 | } |
jjg@1530 | 1224 | |
jjg@1530 | 1225 | private void handle(TaskEvent e) { |
jjg@1530 | 1226 | switch (e.getKind()) { |
jjg@1530 | 1227 | case PARSE: |
jjg@1530 | 1228 | case ENTER: |
jjg@1530 | 1229 | h.handle(e.getSourceFile().getName(), |
jjg@1530 | 1230 | (JCTree) e.getCompilationUnit(), |
jjg@1530 | 1231 | dprinter); |
jjg@1530 | 1232 | break; |
jjg@1530 | 1233 | |
jjg@1530 | 1234 | default: |
jjg@1530 | 1235 | TypeElement elem = e.getTypeElement(); |
jjg@1530 | 1236 | h.handle(elem.toString(), |
jjg@1530 | 1237 | (JCTree) trees.getTree(elem), |
jjg@1530 | 1238 | dprinter); |
jjg@1530 | 1239 | break; |
jjg@1530 | 1240 | } |
jjg@1530 | 1241 | } |
jjg@1530 | 1242 | }); |
jjg@1530 | 1243 | |
jjg@1530 | 1244 | task.call(); |
jjg@1530 | 1245 | } |
jjg@1530 | 1246 | |
jjg@1530 | 1247 | TaskEvent.Kind getKind(String s) { |
jjg@1530 | 1248 | return TaskEvent.Kind.valueOf(s.toUpperCase()); |
jjg@1530 | 1249 | } |
jjg@1530 | 1250 | |
jjg@1530 | 1251 | static protected abstract class Handler { |
jjg@1530 | 1252 | final String name; |
jjg@1530 | 1253 | Handler(String name) { |
jjg@1530 | 1254 | this.name = name; |
jjg@1530 | 1255 | } |
jjg@1530 | 1256 | abstract void handle(String label, JCTree tree, DPrinter dprinter); |
jjg@1530 | 1257 | } |
jjg@1530 | 1258 | |
jjg@1530 | 1259 | Map<String,Handler> getHandlers() { |
jjg@1530 | 1260 | Map<String,Handler> map = new HashMap<String, Handler>(); |
jjg@1530 | 1261 | for (Handler h: defaultHandlers) { |
jjg@1530 | 1262 | map.put(h.name, h); |
jjg@1530 | 1263 | } |
jjg@1530 | 1264 | return map; |
jjg@1530 | 1265 | } |
jjg@1530 | 1266 | |
jjg@1530 | 1267 | protected final Handler[] defaultHandlers = { |
jjg@1530 | 1268 | new Handler("trees") { |
jjg@1530 | 1269 | @Override |
jjg@1530 | 1270 | void handle(String name, JCTree tree, DPrinter dprinter) { |
jjg@1530 | 1271 | dprinter.printTree(name, tree); |
jjg@1530 | 1272 | dprinter.out.println(); |
jjg@1530 | 1273 | } |
jjg@1530 | 1274 | }, |
jjg@1530 | 1275 | |
jjg@1530 | 1276 | new Handler("symbols") { |
jjg@1530 | 1277 | @Override |
jjg@1530 | 1278 | void handle(String name, JCTree tree, final DPrinter dprinter) { |
jjg@1530 | 1279 | TreeScanner ds = new TreeScanner() { |
jjg@1530 | 1280 | @Override |
jjg@1530 | 1281 | public void visitClassDef(JCClassDecl tree) { |
jjg@1530 | 1282 | visitDecl(tree, tree.sym); |
jjg@1530 | 1283 | super.visitClassDef(tree); |
jjg@1530 | 1284 | } |
jjg@1530 | 1285 | |
jjg@1530 | 1286 | @Override |
jjg@1530 | 1287 | public void visitMethodDef(JCMethodDecl tree) { |
jjg@1530 | 1288 | visitDecl(tree, tree.sym); |
jjg@1530 | 1289 | super.visitMethodDef(tree); |
jjg@1530 | 1290 | } |
jjg@1530 | 1291 | |
jjg@1530 | 1292 | @Override |
jjg@1530 | 1293 | public void visitVarDef(JCVariableDecl tree) { |
jjg@1530 | 1294 | visitDecl(tree, tree.sym); |
jjg@1530 | 1295 | super.visitVarDef(tree); |
jjg@1530 | 1296 | } |
jjg@1530 | 1297 | |
jjg@1530 | 1298 | void visitDecl(JCTree tree, Symbol sym) { |
jjg@1530 | 1299 | dprinter.printSymbol(sym.name.toString(), sym); |
jjg@1530 | 1300 | dprinter.out.println(); |
jjg@1530 | 1301 | } |
jjg@1530 | 1302 | }; |
jjg@1530 | 1303 | ds.scan(tree); |
jjg@1530 | 1304 | } |
jjg@1530 | 1305 | }, |
jjg@1530 | 1306 | |
jjg@1530 | 1307 | new Handler("types") { |
jjg@1530 | 1308 | @Override |
jjg@1530 | 1309 | void handle(String name, JCTree tree, final DPrinter dprinter) { |
jjg@1530 | 1310 | TreeScanner ts = new TreeScanner() { |
jjg@1530 | 1311 | @Override |
jjg@1530 | 1312 | public void scan(JCTree tree) { |
jjg@1530 | 1313 | if (tree == null) { |
jjg@1530 | 1314 | return; |
jjg@1530 | 1315 | } |
jjg@1530 | 1316 | if (tree.type != null) { |
jjg@1530 | 1317 | String label = Pretty.toSimpleString(tree); |
jjg@1530 | 1318 | dprinter.printType(label, tree.type); |
jjg@1530 | 1319 | dprinter.out.println(); |
jjg@1530 | 1320 | } |
jjg@1530 | 1321 | super.scan(tree); |
jjg@1530 | 1322 | } |
jjg@1530 | 1323 | }; |
jjg@1530 | 1324 | ts.scan(tree); |
jjg@1530 | 1325 | } |
jjg@1530 | 1326 | } |
jjg@1530 | 1327 | }; |
jjg@1530 | 1328 | } |
jjg@1530 | 1329 | |
jjg@1530 | 1330 | // </editor-fold> |
jjg@1530 | 1331 | |
jjg@1530 | 1332 | } |