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

Thu, 28 Apr 2011 15:05:36 -0700

author
jjg
date
Thu, 28 Apr 2011 15:05:36 -0700
changeset 988
7ae6c0fd479b
parent 783
90914ac50868
child 1090
1807fc3fd33c
permissions
-rw-r--r--

7029150: Project Coin: present union types from the tree API through to javax.lang.model
Reviewed-by: mcimadamore

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

mercurial