src/share/classes/com/sun/tools/javac/api/JavacTrees.java

Tue, 28 Feb 2012 10:33:49 -0800

author
jjg
date
Tue, 28 Feb 2012 10:33:49 -0800
changeset 1210
62e611704863
parent 1138
7375d4979bd3
child 1280
5c0b3faeb0b0
permissions
-rw-r--r--

7093891: support multiple task listeners
Reviewed-by: darcy, mcimadamore

duke@1 1 /*
jjg@1210 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.api;
duke@1 27
duke@1 28 import java.io.IOException;
jjg@1210 29
duke@1 30 import javax.annotation.processing.ProcessingEnvironment;
duke@1 31 import javax.lang.model.element.AnnotationMirror;
duke@1 32 import javax.lang.model.element.AnnotationValue;
duke@1 33 import javax.lang.model.element.Element;
duke@1 34 import javax.lang.model.element.ExecutableElement;
duke@1 35 import javax.lang.model.element.TypeElement;
duke@1 36 import javax.lang.model.type.DeclaredType;
jjg@988 37 import javax.lang.model.type.TypeKind;
duke@1 38 import javax.lang.model.type.TypeMirror;
jjg@308 39 import javax.tools.Diagnostic;
duke@1 40 import javax.tools.JavaCompiler;
duke@1 41 import javax.tools.JavaFileObject;
duke@1 42
jjg@988 43 import com.sun.source.tree.CatchTree;
duke@1 44 import com.sun.source.tree.CompilationUnitTree;
duke@1 45 import com.sun.source.tree.Scope;
duke@1 46 import com.sun.source.tree.Tree;
jjg@1210 47 import com.sun.source.util.JavacTask;
duke@1 48 import com.sun.source.util.SourcePositions;
duke@1 49 import com.sun.source.util.TreePath;
duke@1 50 import com.sun.source.util.Trees;
jjg@672 51 import com.sun.tools.javac.code.Flags;
jjg@1210 52 import com.sun.tools.javac.code.Symbol;
duke@1 53 import com.sun.tools.javac.code.Symbol.ClassSymbol;
duke@1 54 import com.sun.tools.javac.code.Symbol.TypeSymbol;
jjg@988 55 import com.sun.tools.javac.code.Type.UnionClassType;
duke@1 56 import com.sun.tools.javac.comp.Attr;
duke@1 57 import com.sun.tools.javac.comp.AttrContext;
duke@1 58 import com.sun.tools.javac.comp.Enter;
duke@1 59 import com.sun.tools.javac.comp.Env;
duke@1 60 import com.sun.tools.javac.comp.MemberEnter;
duke@1 61 import com.sun.tools.javac.comp.Resolve;
duke@1 62 import com.sun.tools.javac.model.JavacElements;
ksrini@1138 63 import com.sun.tools.javac.parser.EndPosTable;
duke@1 64 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
jjg@1210 65 import com.sun.tools.javac.tree.JCTree;
duke@1 66 import com.sun.tools.javac.tree.JCTree.*;
duke@1 67 import com.sun.tools.javac.tree.TreeCopier;
duke@1 68 import com.sun.tools.javac.tree.TreeInfo;
duke@1 69 import com.sun.tools.javac.tree.TreeMaker;
jjg@1090 70 import com.sun.tools.javac.util.Assert;
duke@1 71 import com.sun.tools.javac.util.Context;
jjg@308 72 import com.sun.tools.javac.util.JCDiagnostic;
duke@1 73 import com.sun.tools.javac.util.List;
duke@1 74 import com.sun.tools.javac.util.Log;
duke@1 75 import com.sun.tools.javac.util.Pair;
duke@1 76
duke@1 77 /**
duke@1 78 * Provides an implementation of Trees.
duke@1 79 *
jjg@581 80 * <p><b>This is NOT part of any supported API.
duke@1 81 * If you write code that depends on this, you do so at your own
duke@1 82 * risk. This code and its internal interfaces are subject to change
duke@1 83 * or deletion without notice.</b></p>
duke@1 84 *
duke@1 85 * @author Peter von der Ah&eacute;
duke@1 86 */
duke@1 87 public class JavacTrees extends Trees {
duke@1 88
jjg@696 89 // in a world of a single context per compilation, these would all be final
jjg@696 90 private Resolve resolve;
jjg@696 91 private Enter enter;
jjg@696 92 private Log log;
jjg@696 93 private MemberEnter memberEnter;
jjg@696 94 private Attr attr;
jjg@696 95 private TreeMaker treeMaker;
jjg@696 96 private JavacElements elements;
jjg@696 97 private JavacTaskImpl javacTaskImpl;
duke@1 98
jjg@1210 99 // called reflectively from Trees.instance(CompilationTask task)
duke@1 100 public static JavacTrees instance(JavaCompiler.CompilationTask task) {
duke@1 101 if (!(task instanceof JavacTaskImpl))
duke@1 102 throw new IllegalArgumentException();
duke@1 103 return instance(((JavacTaskImpl)task).getContext());
duke@1 104 }
duke@1 105
jjg@1210 106 // called reflectively from Trees.instance(ProcessingEnvironment env)
duke@1 107 public static JavacTrees instance(ProcessingEnvironment env) {
duke@1 108 if (!(env instanceof JavacProcessingEnvironment))
duke@1 109 throw new IllegalArgumentException();
duke@1 110 return instance(((JavacProcessingEnvironment)env).getContext());
duke@1 111 }
duke@1 112
duke@1 113 public static JavacTrees instance(Context context) {
duke@1 114 JavacTrees instance = context.get(JavacTrees.class);
duke@1 115 if (instance == null)
duke@1 116 instance = new JavacTrees(context);
duke@1 117 return instance;
duke@1 118 }
duke@1 119
duke@1 120 private JavacTrees(Context context) {
duke@1 121 context.put(JavacTrees.class, this);
jjg@696 122 init(context);
jjg@696 123 }
jjg@696 124
jjg@696 125 public void updateContext(Context context) {
jjg@696 126 init(context);
jjg@696 127 }
jjg@696 128
jjg@696 129 private void init(Context context) {
duke@1 130 attr = Attr.instance(context);
duke@1 131 enter = Enter.instance(context);
duke@1 132 elements = JavacElements.instance(context);
duke@1 133 log = Log.instance(context);
duke@1 134 resolve = Resolve.instance(context);
duke@1 135 treeMaker = TreeMaker.instance(context);
duke@1 136 memberEnter = MemberEnter.instance(context);
jjg@1210 137
jjg@1210 138 JavacTask t = context.get(JavacTask.class);
jjg@1210 139 if (t instanceof JavacTaskImpl)
jjg@1210 140 javacTaskImpl = (JavacTaskImpl) t;
duke@1 141 }
duke@1 142
duke@1 143 public SourcePositions getSourcePositions() {
duke@1 144 return new SourcePositions() {
duke@1 145 public long getStartPosition(CompilationUnitTree file, Tree tree) {
duke@1 146 return TreeInfo.getStartPos((JCTree) tree);
duke@1 147 }
duke@1 148
duke@1 149 public long getEndPosition(CompilationUnitTree file, Tree tree) {
ksrini@1138 150 EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
ksrini@1138 151 return TreeInfo.getEndPos((JCTree) tree, endPosTable);
duke@1 152 }
duke@1 153 };
duke@1 154 }
duke@1 155
duke@1 156 public JCClassDecl getTree(TypeElement element) {
duke@1 157 return (JCClassDecl) getTree((Element) element);
duke@1 158 }
duke@1 159
duke@1 160 public JCMethodDecl getTree(ExecutableElement method) {
duke@1 161 return (JCMethodDecl) getTree((Element) method);
duke@1 162 }
duke@1 163
duke@1 164 public JCTree getTree(Element element) {
duke@1 165 Symbol symbol = (Symbol) element;
duke@1 166 TypeSymbol enclosing = symbol.enclClass();
duke@1 167 Env<AttrContext> env = enter.getEnv(enclosing);
duke@1 168 if (env == null)
duke@1 169 return null;
duke@1 170 JCClassDecl classNode = env.enclClass;
duke@1 171 if (classNode != null) {
duke@1 172 if (TreeInfo.symbolFor(classNode) == element)
duke@1 173 return classNode;
duke@1 174 for (JCTree node : classNode.getMembers())
duke@1 175 if (TreeInfo.symbolFor(node) == element)
duke@1 176 return node;
duke@1 177 }
duke@1 178 return null;
duke@1 179 }
duke@1 180
duke@1 181 public JCTree getTree(Element e, AnnotationMirror a) {
duke@1 182 return getTree(e, a, null);
duke@1 183 }
duke@1 184
duke@1 185 public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
duke@1 186 Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
duke@1 187 if (treeTopLevel == null)
duke@1 188 return null;
duke@1 189 return treeTopLevel.fst;
duke@1 190 }
duke@1 191
duke@1 192 public TreePath getPath(CompilationUnitTree unit, Tree node) {
duke@1 193 return TreePath.getPath(unit, node);
duke@1 194 }
duke@1 195
duke@1 196 public TreePath getPath(Element e) {
duke@1 197 return getPath(e, null, null);
duke@1 198 }
duke@1 199
duke@1 200 public TreePath getPath(Element e, AnnotationMirror a) {
duke@1 201 return getPath(e, a, null);
duke@1 202 }
duke@1 203
duke@1 204 public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
duke@1 205 final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
duke@1 206 if (treeTopLevel == null)
duke@1 207 return null;
duke@1 208 return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
duke@1 209 }
duke@1 210
duke@1 211 public Element getElement(TreePath path) {
jjg@672 212 JCTree tree = (JCTree) path.getLeaf();
jjg@672 213 Symbol sym = TreeInfo.symbolFor(tree);
jjg@672 214 if (sym == null && TreeInfo.isDeclaration(tree)) {
jjg@672 215 for (TreePath p = path; p != null; p = p.getParentPath()) {
jjg@672 216 JCTree t = (JCTree) p.getLeaf();
jjg@1127 217 if (t.hasTag(JCTree.Tag.CLASSDEF)) {
jjg@672 218 JCClassDecl ct = (JCClassDecl) t;
jjg@672 219 if (ct.sym != null) {
jjg@672 220 if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
jjg@672 221 attr.attribClass(ct.pos(), ct.sym);
jjg@672 222 sym = TreeInfo.symbolFor(tree);
jjg@672 223 }
jjg@672 224 break;
jjg@672 225 }
jjg@672 226 }
jjg@672 227 }
jjg@672 228 }
jjg@672 229 return sym;
duke@1 230 }
duke@1 231
duke@1 232 public TypeMirror getTypeMirror(TreePath path) {
duke@1 233 Tree t = path.getLeaf();
duke@1 234 return ((JCTree)t).type;
duke@1 235 }
duke@1 236
duke@1 237 public JavacScope getScope(TreePath path) {
duke@1 238 return new JavacScope(getAttrContext(path));
duke@1 239 }
duke@1 240
jjg@783 241 public String getDocComment(TreePath path) {
jjg@783 242 CompilationUnitTree t = path.getCompilationUnit();
jjg@783 243 if (t instanceof JCTree.JCCompilationUnit) {
jjg@783 244 JCCompilationUnit cu = (JCCompilationUnit) t;
jjg@783 245 if (cu.docComments != null) {
jjg@783 246 return cu.docComments.get(path.getLeaf());
jjg@783 247 }
jjg@783 248 }
jjg@783 249 return null;
jjg@783 250 }
jjg@783 251
duke@1 252 public boolean isAccessible(Scope scope, TypeElement type) {
duke@1 253 if (scope instanceof JavacScope && type instanceof ClassSymbol) {
duke@1 254 Env<AttrContext> env = ((JavacScope) scope).env;
mcimadamore@741 255 return resolve.isAccessible(env, (ClassSymbol)type, true);
duke@1 256 } else
duke@1 257 return false;
duke@1 258 }
duke@1 259
duke@1 260 public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
duke@1 261 if (scope instanceof JavacScope
duke@1 262 && member instanceof Symbol
duke@1 263 && type instanceof com.sun.tools.javac.code.Type) {
duke@1 264 Env<AttrContext> env = ((JavacScope) scope).env;
mcimadamore@741 265 return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
duke@1 266 } else
duke@1 267 return false;
duke@1 268 }
duke@1 269
duke@1 270 private Env<AttrContext> getAttrContext(TreePath path) {
duke@1 271 if (!(path.getLeaf() instanceof JCTree)) // implicit null-check
duke@1 272 throw new IllegalArgumentException();
duke@1 273
jjg@1090 274 // if we're being invoked from a Tree API client via parse/enter/analyze,
jjg@1090 275 // we need to make sure all the classes have been entered;
jjg@1090 276 // if we're being invoked from JSR 199 or JSR 269, then the classes
jjg@1090 277 // will already have been entered.
duke@1 278 if (javacTaskImpl != null) {
duke@1 279 try {
duke@1 280 javacTaskImpl.enter(null);
duke@1 281 } catch (IOException e) {
duke@1 282 throw new Error("unexpected error while entering symbols: " + e);
duke@1 283 }
duke@1 284 }
duke@1 285
duke@1 286
duke@1 287 JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
duke@1 288 Copier copier = new Copier(treeMaker.forToplevel(unit));
duke@1 289
duke@1 290 Env<AttrContext> env = null;
duke@1 291 JCMethodDecl method = null;
duke@1 292 JCVariableDecl field = null;
duke@1 293
duke@1 294 List<Tree> l = List.nil();
duke@1 295 TreePath p = path;
duke@1 296 while (p != null) {
duke@1 297 l = l.prepend(p.getLeaf());
duke@1 298 p = p.getParentPath();
duke@1 299 }
duke@1 300
duke@1 301 for ( ; l.nonEmpty(); l = l.tail) {
duke@1 302 Tree tree = l.head;
duke@1 303 switch (tree.getKind()) {
duke@1 304 case COMPILATION_UNIT:
duke@1 305 // System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
duke@1 306 env = enter.getTopLevelEnv((JCCompilationUnit)tree);
duke@1 307 break;
jjg@727 308 case ANNOTATION_TYPE:
duke@1 309 case CLASS:
jjg@727 310 case ENUM:
jjg@682 311 case INTERFACE:
duke@1 312 // System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
duke@1 313 env = enter.getClassEnv(((JCClassDecl)tree).sym);
duke@1 314 break;
duke@1 315 case METHOD:
duke@1 316 // System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
duke@1 317 method = (JCMethodDecl)tree;
duke@1 318 break;
duke@1 319 case VARIABLE:
duke@1 320 // System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
duke@1 321 field = (JCVariableDecl)tree;
duke@1 322 break;
duke@1 323 case BLOCK: {
duke@1 324 // System.err.println("BLOCK: ");
jjg@1090 325 if (method != null) {
jjg@1090 326 try {
jjg@1090 327 Assert.check(method.body == tree);
jjg@1090 328 method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
jjg@1090 329 env = memberEnter.getMethodEnv(method, env);
jjg@1090 330 env = attribStatToTree(method.body, env, copier.leafCopy);
jjg@1090 331 } finally {
jjg@1090 332 method.body = (JCBlock) tree;
jjg@1090 333 }
jjg@1090 334 } else {
jjg@1090 335 JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
jjg@1090 336 env = attribStatToTree(body, env, copier.leafCopy);
jjg@1090 337 }
duke@1 338 return env;
duke@1 339 }
duke@1 340 default:
duke@1 341 // System.err.println("DEFAULT: " + tree.getKind());
duke@1 342 if (field != null && field.getInitializer() == tree) {
duke@1 343 env = memberEnter.getInitEnv(field, env);
duke@1 344 JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf());
duke@1 345 env = attribExprToTree(expr, env, copier.leafCopy);
duke@1 346 return env;
duke@1 347 }
duke@1 348 }
duke@1 349 }
jjg@1090 350 return (field != null) ? memberEnter.getInitEnv(field, env) : env;
duke@1 351 }
duke@1 352
duke@1 353 private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
duke@1 354 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
duke@1 355 try {
duke@1 356 return attr.attribStatToTree(stat, env, tree);
duke@1 357 } finally {
duke@1 358 log.useSource(prev);
duke@1 359 }
duke@1 360 }
duke@1 361
duke@1 362 private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
duke@1 363 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
duke@1 364 try {
duke@1 365 return attr.attribExprToTree(expr, env, tree);
duke@1 366 } finally {
duke@1 367 log.useSource(prev);
duke@1 368 }
duke@1 369 }
duke@1 370
duke@1 371 /**
duke@1 372 * Makes a copy of a tree, noting the value resulting from copying a particular leaf.
duke@1 373 **/
duke@1 374 static class Copier extends TreeCopier<JCTree> {
duke@1 375 JCTree leafCopy = null;
duke@1 376
duke@1 377 Copier(TreeMaker M) {
duke@1 378 super(M);
duke@1 379 }
duke@1 380
jjg@696 381 @Override
duke@1 382 public <T extends JCTree> T copy(T t, JCTree leaf) {
duke@1 383 T t2 = super.copy(t, leaf);
duke@1 384 if (t == leaf)
duke@1 385 leafCopy = t2;
duke@1 386 return t2;
duke@1 387 }
duke@1 388 }
jjg@110 389
jjg@110 390 /**
jjg@110 391 * Gets the original type from the ErrorType object.
jjg@110 392 * @param errorType The errorType for which we want to get the original type.
jjg@110 393 * @returns TypeMirror corresponding to the original type, replaced by the ErrorType.
jjg@110 394 * noType (type.tag == NONE) is returned if there is no original type.
jjg@110 395 */
jjg@110 396 public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
jjg@110 397 if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
jjg@110 398 return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
jjg@110 399 }
jjg@110 400
jjg@110 401 return com.sun.tools.javac.code.Type.noType;
jjg@110 402 }
jjg@308 403
jjg@308 404 /**
jjg@308 405 * Prints a message of the specified kind at the location of the
jjg@308 406 * tree within the provided compilation unit
jjg@308 407 *
jjg@308 408 * @param kind the kind of message
jjg@308 409 * @param msg the message, or an empty string if none
jjg@308 410 * @param t the tree to use as a position hint
jjg@308 411 * @param root the compilation unit that contains tree
jjg@308 412 */
jjg@308 413 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
jjg@308 414 com.sun.source.tree.Tree t,
jjg@308 415 com.sun.source.tree.CompilationUnitTree root) {
jjg@308 416 JavaFileObject oldSource = null;
jjg@308 417 JavaFileObject newSource = null;
jjg@308 418 JCDiagnostic.DiagnosticPosition pos = null;
jjg@308 419
jjg@308 420 newSource = root.getSourceFile();
jjg@308 421 if (newSource != null) {
jjg@308 422 oldSource = log.useSource(newSource);
jjg@308 423 pos = ((JCTree) t).pos();
jjg@308 424 }
jjg@308 425
jjg@308 426 try {
jjg@308 427 switch (kind) {
jjg@308 428 case ERROR:
jjg@308 429 boolean prev = log.multipleErrors;
jjg@308 430 try {
jjg@308 431 log.error(pos, "proc.messager", msg.toString());
jjg@308 432 } finally {
jjg@308 433 log.multipleErrors = prev;
jjg@308 434 }
jjg@308 435 break;
jjg@308 436
jjg@308 437 case WARNING:
jjg@308 438 log.warning(pos, "proc.messager", msg.toString());
jjg@308 439 break;
jjg@308 440
jjg@308 441 case MANDATORY_WARNING:
jjg@308 442 log.mandatoryWarning(pos, "proc.messager", msg.toString());
jjg@308 443 break;
jjg@308 444
jjg@308 445 default:
jjg@308 446 log.note(pos, "proc.messager", msg.toString());
jjg@308 447 }
jjg@308 448 } finally {
jjg@308 449 if (oldSource != null)
jjg@308 450 log.useSource(oldSource);
jjg@308 451 }
jjg@308 452 }
jjg@988 453
jjg@988 454 @Override
jjg@988 455 public TypeMirror getLub(CatchTree tree) {
jjg@988 456 JCCatch ct = (JCCatch) tree;
jjg@988 457 JCVariableDecl v = ct.param;
jjg@988 458 if (v.type != null && v.type.getKind() == TypeKind.UNION) {
jjg@988 459 UnionClassType ut = (UnionClassType) v.type;
jjg@988 460 return ut.getLub();
jjg@988 461 } else {
jjg@988 462 return v.type;
jjg@988 463 }
jjg@988 464 }
duke@1 465 }

mercurial