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

Thu, 09 Dec 2010 08:48:08 -0800

author
jjg
date
Thu, 09 Dec 2010 08:48:08 -0800
changeset 783
90914ac50868
parent 741
58ceeff50af8
child 988
7ae6c0fd479b
permissions
-rw-r--r--

6985202: no access to doc comments from Tree API
Reviewed-by: mcimadamore

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

mercurial