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

Wed, 14 Sep 2011 12:14:30 -0700

author
jjg
date
Wed, 14 Sep 2011 12:14:30 -0700
changeset 1090
1807fc3fd33c
parent 988
7ae6c0fd479b
child 1127
ca49d50318dc
permissions
-rw-r--r--

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

mercurial