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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 1327
fabfd2710057
child 1409
33abf479f202
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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;
jjg@1357 63 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
jjg@1280 64 import com.sun.tools.javac.tree.EndPosTable;
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
ksrini@1327 120 protected 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@1280 243 Tree leaf = path.getLeaf();
jjg@1280 244 if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
jjg@783 245 JCCompilationUnit cu = (JCCompilationUnit) t;
jjg@783 246 if (cu.docComments != null) {
jjg@1280 247 return cu.docComments.getCommentText((JCTree) leaf);
jjg@783 248 }
jjg@783 249 }
jjg@783 250 return null;
jjg@783 251 }
jjg@783 252
duke@1 253 public boolean isAccessible(Scope scope, TypeElement type) {
duke@1 254 if (scope instanceof JavacScope && type instanceof ClassSymbol) {
duke@1 255 Env<AttrContext> env = ((JavacScope) scope).env;
mcimadamore@741 256 return resolve.isAccessible(env, (ClassSymbol)type, true);
duke@1 257 } else
duke@1 258 return false;
duke@1 259 }
duke@1 260
duke@1 261 public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
duke@1 262 if (scope instanceof JavacScope
duke@1 263 && member instanceof Symbol
duke@1 264 && type instanceof com.sun.tools.javac.code.Type) {
duke@1 265 Env<AttrContext> env = ((JavacScope) scope).env;
mcimadamore@741 266 return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
duke@1 267 } else
duke@1 268 return false;
duke@1 269 }
duke@1 270
duke@1 271 private Env<AttrContext> getAttrContext(TreePath path) {
duke@1 272 if (!(path.getLeaf() instanceof JCTree)) // implicit null-check
duke@1 273 throw new IllegalArgumentException();
duke@1 274
jjg@1090 275 // if we're being invoked from a Tree API client via parse/enter/analyze,
jjg@1090 276 // we need to make sure all the classes have been entered;
jjg@1090 277 // if we're being invoked from JSR 199 or JSR 269, then the classes
jjg@1090 278 // will already have been entered.
duke@1 279 if (javacTaskImpl != null) {
duke@1 280 try {
duke@1 281 javacTaskImpl.enter(null);
duke@1 282 } catch (IOException e) {
duke@1 283 throw new Error("unexpected error while entering symbols: " + e);
duke@1 284 }
duke@1 285 }
duke@1 286
duke@1 287
duke@1 288 JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
ksrini@1327 289 Copier copier = createCopier(treeMaker.forToplevel(unit));
duke@1 290
duke@1 291 Env<AttrContext> env = null;
duke@1 292 JCMethodDecl method = null;
duke@1 293 JCVariableDecl field = null;
duke@1 294
duke@1 295 List<Tree> l = List.nil();
duke@1 296 TreePath p = path;
duke@1 297 while (p != null) {
duke@1 298 l = l.prepend(p.getLeaf());
duke@1 299 p = p.getParentPath();
duke@1 300 }
duke@1 301
duke@1 302 for ( ; l.nonEmpty(); l = l.tail) {
duke@1 303 Tree tree = l.head;
duke@1 304 switch (tree.getKind()) {
duke@1 305 case COMPILATION_UNIT:
duke@1 306 // System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
duke@1 307 env = enter.getTopLevelEnv((JCCompilationUnit)tree);
duke@1 308 break;
jjg@727 309 case ANNOTATION_TYPE:
duke@1 310 case CLASS:
jjg@727 311 case ENUM:
jjg@682 312 case INTERFACE:
duke@1 313 // System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
duke@1 314 env = enter.getClassEnv(((JCClassDecl)tree).sym);
duke@1 315 break;
duke@1 316 case METHOD:
duke@1 317 // System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
duke@1 318 method = (JCMethodDecl)tree;
duke@1 319 break;
duke@1 320 case VARIABLE:
duke@1 321 // System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
duke@1 322 field = (JCVariableDecl)tree;
duke@1 323 break;
duke@1 324 case BLOCK: {
duke@1 325 // System.err.println("BLOCK: ");
jjg@1090 326 if (method != null) {
jjg@1090 327 try {
jjg@1090 328 Assert.check(method.body == tree);
jjg@1090 329 method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
jjg@1090 330 env = memberEnter.getMethodEnv(method, env);
jjg@1090 331 env = attribStatToTree(method.body, env, copier.leafCopy);
jjg@1090 332 } finally {
jjg@1090 333 method.body = (JCBlock) tree;
jjg@1090 334 }
jjg@1090 335 } else {
jjg@1090 336 JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
jjg@1090 337 env = attribStatToTree(body, env, copier.leafCopy);
jjg@1090 338 }
duke@1 339 return env;
duke@1 340 }
duke@1 341 default:
duke@1 342 // System.err.println("DEFAULT: " + tree.getKind());
duke@1 343 if (field != null && field.getInitializer() == tree) {
duke@1 344 env = memberEnter.getInitEnv(field, env);
duke@1 345 JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf());
duke@1 346 env = attribExprToTree(expr, env, copier.leafCopy);
duke@1 347 return env;
duke@1 348 }
duke@1 349 }
duke@1 350 }
jjg@1090 351 return (field != null) ? memberEnter.getInitEnv(field, env) : env;
duke@1 352 }
duke@1 353
duke@1 354 private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
duke@1 355 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
duke@1 356 try {
duke@1 357 return attr.attribStatToTree(stat, env, tree);
duke@1 358 } finally {
duke@1 359 log.useSource(prev);
duke@1 360 }
duke@1 361 }
duke@1 362
duke@1 363 private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
duke@1 364 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
duke@1 365 try {
duke@1 366 return attr.attribExprToTree(expr, env, tree);
duke@1 367 } finally {
duke@1 368 log.useSource(prev);
duke@1 369 }
duke@1 370 }
duke@1 371
duke@1 372 /**
duke@1 373 * Makes a copy of a tree, noting the value resulting from copying a particular leaf.
duke@1 374 **/
ksrini@1327 375 protected static class Copier extends TreeCopier<JCTree> {
duke@1 376 JCTree leafCopy = null;
duke@1 377
ksrini@1327 378 protected Copier(TreeMaker M) {
duke@1 379 super(M);
duke@1 380 }
duke@1 381
jjg@696 382 @Override
duke@1 383 public <T extends JCTree> T copy(T t, JCTree leaf) {
duke@1 384 T t2 = super.copy(t, leaf);
duke@1 385 if (t == leaf)
duke@1 386 leafCopy = t2;
duke@1 387 return t2;
duke@1 388 }
duke@1 389 }
jjg@110 390
ksrini@1327 391 protected Copier createCopier(TreeMaker maker) {
ksrini@1327 392 return new Copier(maker);
ksrini@1327 393 }
ksrini@1327 394
jjg@110 395 /**
jjg@110 396 * Gets the original type from the ErrorType object.
jjg@110 397 * @param errorType The errorType for which we want to get the original type.
jjg@110 398 * @returns TypeMirror corresponding to the original type, replaced by the ErrorType.
jjg@110 399 * noType (type.tag == NONE) is returned if there is no original type.
jjg@110 400 */
jjg@110 401 public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
jjg@110 402 if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
jjg@110 403 return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
jjg@110 404 }
jjg@110 405
jjg@110 406 return com.sun.tools.javac.code.Type.noType;
jjg@110 407 }
jjg@308 408
jjg@308 409 /**
jjg@308 410 * Prints a message of the specified kind at the location of the
jjg@308 411 * tree within the provided compilation unit
jjg@308 412 *
jjg@308 413 * @param kind the kind of message
jjg@308 414 * @param msg the message, or an empty string if none
jjg@308 415 * @param t the tree to use as a position hint
jjg@308 416 * @param root the compilation unit that contains tree
jjg@308 417 */
jjg@308 418 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
jjg@308 419 com.sun.source.tree.Tree t,
jjg@308 420 com.sun.source.tree.CompilationUnitTree root) {
jjg@308 421 JavaFileObject oldSource = null;
jjg@308 422 JavaFileObject newSource = null;
jjg@308 423 JCDiagnostic.DiagnosticPosition pos = null;
jjg@308 424
jjg@308 425 newSource = root.getSourceFile();
jjg@308 426 if (newSource != null) {
jjg@308 427 oldSource = log.useSource(newSource);
jjg@308 428 pos = ((JCTree) t).pos();
jjg@308 429 }
jjg@308 430
jjg@308 431 try {
jjg@308 432 switch (kind) {
jjg@308 433 case ERROR:
jjg@308 434 boolean prev = log.multipleErrors;
jjg@308 435 try {
jjg@308 436 log.error(pos, "proc.messager", msg.toString());
jjg@308 437 } finally {
jjg@308 438 log.multipleErrors = prev;
jjg@308 439 }
jjg@308 440 break;
jjg@308 441
jjg@308 442 case WARNING:
jjg@308 443 log.warning(pos, "proc.messager", msg.toString());
jjg@308 444 break;
jjg@308 445
jjg@308 446 case MANDATORY_WARNING:
jjg@308 447 log.mandatoryWarning(pos, "proc.messager", msg.toString());
jjg@308 448 break;
jjg@308 449
jjg@308 450 default:
jjg@308 451 log.note(pos, "proc.messager", msg.toString());
jjg@308 452 }
jjg@308 453 } finally {
jjg@308 454 if (oldSource != null)
jjg@308 455 log.useSource(oldSource);
jjg@308 456 }
jjg@308 457 }
jjg@988 458
jjg@988 459 @Override
jjg@988 460 public TypeMirror getLub(CatchTree tree) {
jjg@988 461 JCCatch ct = (JCCatch) tree;
jjg@988 462 JCVariableDecl v = ct.param;
jjg@988 463 if (v.type != null && v.type.getKind() == TypeKind.UNION) {
jjg@988 464 UnionClassType ut = (UnionClassType) v.type;
jjg@988 465 return ut.getLub();
jjg@988 466 } else {
jjg@988 467 return v.type;
jjg@988 468 }
jjg@988 469 }
duke@1 470 }

mercurial