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

Wed, 17 Apr 2013 15:54:24 +0200

author
jlahoda
date
Wed, 17 Apr 2013 15:54:24 +0200
changeset 1704
ed918a442b83
parent 1455
75ab654b5cd5
child 1726
a7ff36d06fa2
permissions
-rw-r--r--

8008174: DocTree API should provide start and end positions for tree nodes
Summary: Adding DocSourcePositions to allow access to DocTree starting/ending position
Reviewed-by: jjg, darcy
Contributed-by: Ralph Benjamin Ruijs <ralphbenjamin@netbeans.org>, Jan Lahoda <jlahoda@netbeans.org>

duke@1 1 /*
jlahoda@1704 2 * Copyright (c) 2005, 2013, 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@1409 29 import java.util.HashSet;
jjg@1409 30 import java.util.Set;
jjg@1210 31
duke@1 32 import javax.annotation.processing.ProcessingEnvironment;
duke@1 33 import javax.lang.model.element.AnnotationMirror;
duke@1 34 import javax.lang.model.element.AnnotationValue;
duke@1 35 import javax.lang.model.element.Element;
duke@1 36 import javax.lang.model.element.ExecutableElement;
duke@1 37 import javax.lang.model.element.TypeElement;
duke@1 38 import javax.lang.model.type.DeclaredType;
jjg@988 39 import javax.lang.model.type.TypeKind;
duke@1 40 import javax.lang.model.type.TypeMirror;
jjg@308 41 import javax.tools.Diagnostic;
duke@1 42 import javax.tools.JavaCompiler;
duke@1 43 import javax.tools.JavaFileObject;
duke@1 44
jjg@1409 45 import com.sun.source.doctree.DocCommentTree;
jlahoda@1704 46 import com.sun.source.doctree.DocTree;
jjg@1409 47 import com.sun.source.doctree.ReferenceTree;
jjg@988 48 import com.sun.source.tree.CatchTree;
duke@1 49 import com.sun.source.tree.CompilationUnitTree;
duke@1 50 import com.sun.source.tree.Scope;
duke@1 51 import com.sun.source.tree.Tree;
jlahoda@1704 52 import com.sun.source.util.DocSourcePositions;
jlahoda@1704 53 import com.sun.source.util.DocTreeScanner;
jjg@1409 54 import com.sun.source.util.DocTrees;
jjg@1210 55 import com.sun.source.util.JavacTask;
duke@1 56 import com.sun.source.util.TreePath;
jjg@672 57 import com.sun.tools.javac.code.Flags;
jjg@1409 58 import com.sun.tools.javac.code.Kinds;
jjg@1210 59 import com.sun.tools.javac.code.Symbol;
duke@1 60 import com.sun.tools.javac.code.Symbol.ClassSymbol;
jjg@1409 61 import com.sun.tools.javac.code.Symbol.MethodSymbol;
jjg@1409 62 import com.sun.tools.javac.code.Symbol.PackageSymbol;
duke@1 63 import com.sun.tools.javac.code.Symbol.TypeSymbol;
jjg@1409 64 import com.sun.tools.javac.code.Symbol.VarSymbol;
jjg@1409 65 import com.sun.tools.javac.code.Type;
jjg@1409 66 import com.sun.tools.javac.code.Type.ArrayType;
jjg@1409 67 import com.sun.tools.javac.code.Type.ClassType;
jjg@1409 68 import com.sun.tools.javac.code.Type.ErrorType;
jjg@988 69 import com.sun.tools.javac.code.Type.UnionClassType;
jjg@1409 70 import com.sun.tools.javac.code.Types;
jjg@1409 71 import com.sun.tools.javac.code.Types.TypeRelation;
duke@1 72 import com.sun.tools.javac.comp.Attr;
duke@1 73 import com.sun.tools.javac.comp.AttrContext;
duke@1 74 import com.sun.tools.javac.comp.Enter;
duke@1 75 import com.sun.tools.javac.comp.Env;
duke@1 76 import com.sun.tools.javac.comp.MemberEnter;
duke@1 77 import com.sun.tools.javac.comp.Resolve;
duke@1 78 import com.sun.tools.javac.model.JavacElements;
jjg@1357 79 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
jjg@1409 80 import com.sun.tools.javac.tree.DCTree;
jlahoda@1704 81 import com.sun.tools.javac.tree.DCTree.DCBlockTag;
jjg@1409 82 import com.sun.tools.javac.tree.DCTree.DCDocComment;
jlahoda@1704 83 import com.sun.tools.javac.tree.DCTree.DCEndPosTree;
jlahoda@1704 84 import com.sun.tools.javac.tree.DCTree.DCErroneous;
jlahoda@1704 85 import com.sun.tools.javac.tree.DCTree.DCIdentifier;
jlahoda@1704 86 import com.sun.tools.javac.tree.DCTree.DCParam;
jjg@1409 87 import com.sun.tools.javac.tree.DCTree.DCReference;
jlahoda@1704 88 import com.sun.tools.javac.tree.DCTree.DCText;
jjg@1280 89 import com.sun.tools.javac.tree.EndPosTable;
jjg@1210 90 import com.sun.tools.javac.tree.JCTree;
duke@1 91 import com.sun.tools.javac.tree.JCTree.*;
duke@1 92 import com.sun.tools.javac.tree.TreeCopier;
duke@1 93 import com.sun.tools.javac.tree.TreeInfo;
duke@1 94 import com.sun.tools.javac.tree.TreeMaker;
jjg@1455 95 import com.sun.tools.javac.util.Abort;
jjg@1090 96 import com.sun.tools.javac.util.Assert;
duke@1 97 import com.sun.tools.javac.util.Context;
jjg@308 98 import com.sun.tools.javac.util.JCDiagnostic;
duke@1 99 import com.sun.tools.javac.util.List;
jjg@1409 100 import com.sun.tools.javac.util.ListBuffer;
duke@1 101 import com.sun.tools.javac.util.Log;
jjg@1409 102 import com.sun.tools.javac.util.Name;
jjg@1409 103 import com.sun.tools.javac.util.Names;
duke@1 104 import com.sun.tools.javac.util.Pair;
jlahoda@1704 105 import com.sun.tools.javac.util.Position;
jjg@1409 106 import static com.sun.tools.javac.code.TypeTag.*;
duke@1 107
duke@1 108 /**
duke@1 109 * Provides an implementation of Trees.
duke@1 110 *
jjg@581 111 * <p><b>This is NOT part of any supported API.
duke@1 112 * If you write code that depends on this, you do so at your own
duke@1 113 * risk. This code and its internal interfaces are subject to change
duke@1 114 * or deletion without notice.</b></p>
duke@1 115 *
duke@1 116 * @author Peter von der Ah&eacute;
duke@1 117 */
jjg@1409 118 public class JavacTrees extends DocTrees {
duke@1 119
jjg@696 120 // in a world of a single context per compilation, these would all be final
jjg@696 121 private Resolve resolve;
jjg@696 122 private Enter enter;
jjg@696 123 private Log log;
jjg@696 124 private MemberEnter memberEnter;
jjg@696 125 private Attr attr;
jjg@696 126 private TreeMaker treeMaker;
jjg@696 127 private JavacElements elements;
jjg@696 128 private JavacTaskImpl javacTaskImpl;
jjg@1409 129 private Names names;
jjg@1409 130 private Types types;
duke@1 131
jjg@1210 132 // called reflectively from Trees.instance(CompilationTask task)
duke@1 133 public static JavacTrees instance(JavaCompiler.CompilationTask task) {
jjg@1416 134 if (!(task instanceof BasicJavacTask))
duke@1 135 throw new IllegalArgumentException();
jjg@1416 136 return instance(((BasicJavacTask)task).getContext());
duke@1 137 }
duke@1 138
jjg@1210 139 // called reflectively from Trees.instance(ProcessingEnvironment env)
duke@1 140 public static JavacTrees instance(ProcessingEnvironment env) {
duke@1 141 if (!(env instanceof JavacProcessingEnvironment))
duke@1 142 throw new IllegalArgumentException();
duke@1 143 return instance(((JavacProcessingEnvironment)env).getContext());
duke@1 144 }
duke@1 145
duke@1 146 public static JavacTrees instance(Context context) {
duke@1 147 JavacTrees instance = context.get(JavacTrees.class);
duke@1 148 if (instance == null)
duke@1 149 instance = new JavacTrees(context);
duke@1 150 return instance;
duke@1 151 }
duke@1 152
ksrini@1327 153 protected JavacTrees(Context context) {
duke@1 154 context.put(JavacTrees.class, this);
jjg@696 155 init(context);
jjg@696 156 }
jjg@696 157
jjg@696 158 public void updateContext(Context context) {
jjg@696 159 init(context);
jjg@696 160 }
jjg@696 161
jjg@696 162 private void init(Context context) {
duke@1 163 attr = Attr.instance(context);
duke@1 164 enter = Enter.instance(context);
duke@1 165 elements = JavacElements.instance(context);
duke@1 166 log = Log.instance(context);
duke@1 167 resolve = Resolve.instance(context);
duke@1 168 treeMaker = TreeMaker.instance(context);
duke@1 169 memberEnter = MemberEnter.instance(context);
jjg@1409 170 names = Names.instance(context);
jjg@1409 171 types = Types.instance(context);
jjg@1210 172
jjg@1210 173 JavacTask t = context.get(JavacTask.class);
jjg@1210 174 if (t instanceof JavacTaskImpl)
jjg@1210 175 javacTaskImpl = (JavacTaskImpl) t;
duke@1 176 }
duke@1 177
jlahoda@1704 178 public DocSourcePositions getSourcePositions() {
jlahoda@1704 179 return new DocSourcePositions() {
duke@1 180 public long getStartPosition(CompilationUnitTree file, Tree tree) {
duke@1 181 return TreeInfo.getStartPos((JCTree) tree);
duke@1 182 }
duke@1 183
duke@1 184 public long getEndPosition(CompilationUnitTree file, Tree tree) {
ksrini@1138 185 EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
ksrini@1138 186 return TreeInfo.getEndPos((JCTree) tree, endPosTable);
duke@1 187 }
jlahoda@1704 188
jlahoda@1704 189 public long getStartPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
jlahoda@1704 190 return ((DCTree) tree).getSourcePosition((DCDocComment) comment);
jlahoda@1704 191 }
jlahoda@1704 192 @SuppressWarnings("fallthrough")
jlahoda@1704 193 public long getEndPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
jlahoda@1704 194 DCDocComment dcComment = (DCDocComment) comment;
jlahoda@1704 195 if (tree instanceof DCEndPosTree) {
jlahoda@1704 196 int endPos = ((DCEndPosTree) tree).getEndPos(dcComment);
jlahoda@1704 197
jlahoda@1704 198 if (endPos != Position.NOPOS) {
jlahoda@1704 199 return endPos;
jlahoda@1704 200 }
jlahoda@1704 201 }
jlahoda@1704 202 int correction = 0;
jlahoda@1704 203 switch (tree.getKind()) {
jlahoda@1704 204 case TEXT:
jlahoda@1704 205 DCText text = (DCText) tree;
jlahoda@1704 206
jlahoda@1704 207 return dcComment.comment.getSourcePos(text.pos + text.text.length());
jlahoda@1704 208 case ERRONEOUS:
jlahoda@1704 209 DCErroneous err = (DCErroneous) tree;
jlahoda@1704 210
jlahoda@1704 211 return dcComment.comment.getSourcePos(err.pos + err.body.length());
jlahoda@1704 212 case IDENTIFIER:
jlahoda@1704 213 DCIdentifier ident = (DCIdentifier) tree;
jlahoda@1704 214
jlahoda@1704 215 return dcComment.comment.getSourcePos(ident.pos + (ident.name != names.error ? ident.name.length() : 0));
jlahoda@1704 216 case PARAM:
jlahoda@1704 217 DCParam param = (DCParam) tree;
jlahoda@1704 218
jlahoda@1704 219 if (param.isTypeParameter && param.getDescription().isEmpty()) {
jlahoda@1704 220 correction = 1;
jlahoda@1704 221 }
jlahoda@1704 222 case AUTHOR: case DEPRECATED: case RETURN: case SEE:
jlahoda@1704 223 case SERIAL: case SERIAL_DATA: case SERIAL_FIELD: case SINCE:
jlahoda@1704 224 case THROWS: case UNKNOWN_BLOCK_TAG: case VERSION: {
jlahoda@1704 225 DocTree last = getLastChild(tree);
jlahoda@1704 226
jlahoda@1704 227 if (last != null) {
jlahoda@1704 228 return getEndPosition(file, comment, last) + correction;
jlahoda@1704 229 }
jlahoda@1704 230
jlahoda@1704 231 DCBlockTag block = (DCBlockTag) tree;
jlahoda@1704 232
jlahoda@1704 233 return dcComment.comment.getSourcePos(block.pos + block.getTagName().length() + 1);
jlahoda@1704 234 }
jlahoda@1704 235 default:
jlahoda@1704 236 DocTree last = getLastChild(tree);
jlahoda@1704 237
jlahoda@1704 238 if (last != null) {
jlahoda@1704 239 return getEndPosition(file, comment, last);
jlahoda@1704 240 }
jlahoda@1704 241 break;
jlahoda@1704 242 }
jlahoda@1704 243
jlahoda@1704 244 return Position.NOPOS;
jlahoda@1704 245 }
duke@1 246 };
duke@1 247 }
duke@1 248
jlahoda@1704 249 private DocTree getLastChild(DocTree tree) {
jlahoda@1704 250 final DocTree[] last = new DocTree[] {null};
jlahoda@1704 251
jlahoda@1704 252 tree.accept(new DocTreeScanner<Void, Void>() {
jlahoda@1704 253 @Override public Void scan(DocTree node, Void p) {
jlahoda@1704 254 if (node != null) last[0] = node;
jlahoda@1704 255 return null;
jlahoda@1704 256 }
jlahoda@1704 257 }, null);
jlahoda@1704 258
jlahoda@1704 259 return last[0];
jlahoda@1704 260 }
jlahoda@1704 261
duke@1 262 public JCClassDecl getTree(TypeElement element) {
duke@1 263 return (JCClassDecl) getTree((Element) element);
duke@1 264 }
duke@1 265
duke@1 266 public JCMethodDecl getTree(ExecutableElement method) {
duke@1 267 return (JCMethodDecl) getTree((Element) method);
duke@1 268 }
duke@1 269
duke@1 270 public JCTree getTree(Element element) {
duke@1 271 Symbol symbol = (Symbol) element;
duke@1 272 TypeSymbol enclosing = symbol.enclClass();
duke@1 273 Env<AttrContext> env = enter.getEnv(enclosing);
duke@1 274 if (env == null)
duke@1 275 return null;
duke@1 276 JCClassDecl classNode = env.enclClass;
duke@1 277 if (classNode != null) {
duke@1 278 if (TreeInfo.symbolFor(classNode) == element)
duke@1 279 return classNode;
duke@1 280 for (JCTree node : classNode.getMembers())
duke@1 281 if (TreeInfo.symbolFor(node) == element)
duke@1 282 return node;
duke@1 283 }
duke@1 284 return null;
duke@1 285 }
duke@1 286
duke@1 287 public JCTree getTree(Element e, AnnotationMirror a) {
duke@1 288 return getTree(e, a, null);
duke@1 289 }
duke@1 290
duke@1 291 public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
duke@1 292 Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
duke@1 293 if (treeTopLevel == null)
duke@1 294 return null;
duke@1 295 return treeTopLevel.fst;
duke@1 296 }
duke@1 297
duke@1 298 public TreePath getPath(CompilationUnitTree unit, Tree node) {
duke@1 299 return TreePath.getPath(unit, node);
duke@1 300 }
duke@1 301
duke@1 302 public TreePath getPath(Element e) {
duke@1 303 return getPath(e, null, null);
duke@1 304 }
duke@1 305
duke@1 306 public TreePath getPath(Element e, AnnotationMirror a) {
duke@1 307 return getPath(e, a, null);
duke@1 308 }
duke@1 309
duke@1 310 public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
duke@1 311 final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
duke@1 312 if (treeTopLevel == null)
duke@1 313 return null;
duke@1 314 return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
duke@1 315 }
duke@1 316
duke@1 317 public Element getElement(TreePath path) {
jjg@672 318 JCTree tree = (JCTree) path.getLeaf();
jjg@672 319 Symbol sym = TreeInfo.symbolFor(tree);
jjg@1455 320 if (sym == null) {
jjg@1455 321 if (TreeInfo.isDeclaration(tree)) {
jjg@1455 322 for (TreePath p = path; p != null; p = p.getParentPath()) {
jjg@1455 323 JCTree t = (JCTree) p.getLeaf();
jjg@1455 324 if (t.hasTag(JCTree.Tag.CLASSDEF)) {
jjg@1455 325 JCClassDecl ct = (JCClassDecl) t;
jjg@1455 326 if (ct.sym != null) {
jjg@1455 327 if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
jjg@1455 328 attr.attribClass(ct.pos(), ct.sym);
jjg@1455 329 sym = TreeInfo.symbolFor(tree);
jjg@1455 330 }
jjg@1455 331 break;
jjg@672 332 }
jjg@672 333 }
jjg@672 334 }
jjg@1455 335 } else if (tree.hasTag(Tag.TOPLEVEL)) {
jjg@1455 336 JCCompilationUnit cu = (JCCompilationUnit) tree;
jjg@1455 337 if (cu.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE)) {
jjg@1455 338 sym = cu.packge;
jjg@1455 339 }
jjg@672 340 }
jjg@672 341 }
jjg@672 342 return sym;
duke@1 343 }
duke@1 344
jjg@1409 345 @Override
jjg@1409 346 public Element getElement(TreePath path, ReferenceTree reference) {
jjg@1409 347 if (!(reference instanceof DCReference))
jjg@1409 348 return null;
jjg@1409 349 DCReference ref = (DCReference) reference;
jjg@1409 350
jjg@1409 351 Env<AttrContext> env = getAttrContext(path);
jjg@1409 352
jjg@1409 353 Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
jjg@1409 354 new Log.DeferredDiagnosticHandler(log);
jjg@1409 355 try {
jjg@1409 356 final ClassSymbol tsym;
jjg@1409 357 final Name memberName;
jjg@1409 358 if (ref.qualifierExpression == null) {
jjg@1409 359 tsym = env.enclClass.sym;
jjg@1409 360 memberName = ref.memberName;
jjg@1409 361 } else {
jjg@1409 362 // See if the qualifierExpression is a type or package name.
jjg@1409 363 // javac does not provide the exact method required, so
jjg@1409 364 // we first check if qualifierExpression identifies a type,
jjg@1409 365 // and if not, then we check to see if it identifies a package.
jjg@1409 366 Type t = attr.attribType(ref.qualifierExpression, env);
jjg@1409 367 if (t.isErroneous()) {
jjg@1409 368 if (ref.memberName == null) {
jjg@1409 369 // Attr/Resolve assume packages exist and create symbols as needed
jjg@1409 370 // so use getPackageElement to restrict search to existing packages
jjg@1409 371 PackageSymbol pck = elements.getPackageElement(ref.qualifierExpression.toString());
jjg@1409 372 if (pck != null) {
jjg@1409 373 return pck;
jjg@1409 374 } else if (ref.qualifierExpression.hasTag(JCTree.Tag.IDENT)) {
jjg@1409 375 // fixup: allow "identifier" instead of "#identifier"
jjg@1409 376 // for compatibility with javadoc
jjg@1409 377 tsym = env.enclClass.sym;
jjg@1409 378 memberName = ((JCIdent) ref.qualifierExpression).name;
jjg@1409 379 } else
jjg@1409 380 return null;
jjg@1409 381 } else {
jjg@1409 382 return null;
jjg@1409 383 }
jjg@1409 384 } else {
jjg@1409 385 tsym = (ClassSymbol) t.tsym;
jjg@1409 386 memberName = ref.memberName;
jjg@1409 387 }
jjg@1409 388 }
jjg@1409 389
jjg@1409 390 if (memberName == null)
jjg@1409 391 return tsym;
jjg@1409 392
jjg@1409 393 final List<Type> paramTypes;
jjg@1409 394 if (ref.paramTypes == null)
jjg@1409 395 paramTypes = null;
jjg@1409 396 else {
jjg@1409 397 ListBuffer<Type> lb = new ListBuffer<Type>();
jjg@1409 398 for (List<JCTree> l = ref.paramTypes; l.nonEmpty(); l = l.tail) {
jjg@1409 399 JCTree tree = l.head;
jjg@1409 400 Type t = attr.attribType(tree, env);
jjg@1409 401 lb.add(t);
jjg@1409 402 }
jjg@1409 403 paramTypes = lb.toList();
jjg@1409 404 }
jjg@1409 405
jjg@1409 406 Symbol msym = (memberName == tsym.name)
jjg@1409 407 ? findConstructor(tsym, paramTypes)
jjg@1409 408 : findMethod(tsym, memberName, paramTypes);
jjg@1409 409 if (paramTypes != null) {
jjg@1409 410 // explicit (possibly empty) arg list given, so cannot be a field
jjg@1409 411 return msym;
jjg@1409 412 }
jjg@1409 413
jjg@1409 414 VarSymbol vsym = (ref.paramTypes != null) ? null : findField(tsym, memberName);
jjg@1409 415 // prefer a field over a method with no parameters
jjg@1409 416 if (vsym != null &&
jjg@1409 417 (msym == null ||
jjg@1409 418 types.isSubtypeUnchecked(vsym.enclClass().asType(), msym.enclClass().asType()))) {
jjg@1409 419 return vsym;
jjg@1409 420 } else {
jjg@1409 421 return msym;
jjg@1409 422 }
jjg@1455 423 } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file
jjg@1455 424 return null;
jjg@1409 425 } finally {
jjg@1409 426 log.popDiagnosticHandler(deferredDiagnosticHandler);
jjg@1409 427 }
jjg@1409 428 }
jjg@1409 429
jjg@1409 430 /** @see com.sun.tools.javadoc.ClassDocImpl#findField */
jjg@1409 431 private VarSymbol findField(ClassSymbol tsym, Name fieldName) {
jjg@1409 432 return searchField(tsym, fieldName, new HashSet<ClassSymbol>());
jjg@1409 433 }
jjg@1409 434
jjg@1409 435 /** @see com.sun.tools.javadoc.ClassDocImpl#searchField */
jjg@1409 436 private VarSymbol searchField(ClassSymbol tsym, Name fieldName, Set<ClassSymbol> searched) {
jjg@1409 437 if (searched.contains(tsym)) {
jjg@1409 438 return null;
jjg@1409 439 }
jjg@1409 440 searched.add(tsym);
jjg@1409 441
jjg@1409 442 for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(fieldName);
jjg@1409 443 e.scope != null; e = e.next()) {
jjg@1409 444 if (e.sym.kind == Kinds.VAR) {
jjg@1409 445 return (VarSymbol)e.sym;
jjg@1409 446 }
jjg@1409 447 }
jjg@1409 448
jjg@1409 449 //### If we found a VarSymbol above, but which did not pass
jjg@1409 450 //### the modifier filter, we should return failure here!
jjg@1409 451
jjg@1409 452 ClassSymbol encl = tsym.owner.enclClass();
jjg@1409 453 if (encl != null) {
jjg@1409 454 VarSymbol vsym = searchField(encl, fieldName, searched);
jjg@1409 455 if (vsym != null) {
jjg@1409 456 return vsym;
jjg@1409 457 }
jjg@1409 458 }
jjg@1409 459
jjg@1409 460 // search superclass
jjg@1409 461 Type superclass = tsym.getSuperclass();
jjg@1409 462 if (superclass.tsym != null) {
jjg@1409 463 VarSymbol vsym = searchField((ClassSymbol) superclass.tsym, fieldName, searched);
jjg@1409 464 if (vsym != null) {
jjg@1409 465 return vsym;
jjg@1409 466 }
jjg@1409 467 }
jjg@1409 468
jjg@1409 469 // search interfaces
jjg@1409 470 List<Type> intfs = tsym.getInterfaces();
jjg@1409 471 for (List<Type> l = intfs; l.nonEmpty(); l = l.tail) {
jjg@1409 472 Type intf = l.head;
jjg@1409 473 if (intf.isErroneous()) continue;
jjg@1409 474 VarSymbol vsym = searchField((ClassSymbol) intf.tsym, fieldName, searched);
jjg@1409 475 if (vsym != null) {
jjg@1409 476 return vsym;
jjg@1409 477 }
jjg@1409 478 }
jjg@1409 479
jjg@1409 480 return null;
jjg@1409 481 }
jjg@1409 482
jjg@1409 483 /** @see com.sun.tools.javadoc.ClassDocImpl#findConstructor */
jjg@1409 484 MethodSymbol findConstructor(ClassSymbol tsym, List<Type> paramTypes) {
jjg@1409 485 for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(names.init);
jjg@1409 486 e.scope != null; e = e.next()) {
jjg@1409 487 if (e.sym.kind == Kinds.MTH) {
jjg@1409 488 if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) {
jjg@1409 489 return (MethodSymbol) e.sym;
jjg@1409 490 }
jjg@1409 491 }
jjg@1409 492 }
jjg@1409 493 return null;
jjg@1409 494 }
jjg@1409 495
jjg@1409 496 /** @see com.sun.tools.javadoc.ClassDocImpl#findMethod */
jjg@1409 497 private MethodSymbol findMethod(ClassSymbol tsym, Name methodName, List<Type> paramTypes) {
jjg@1409 498 return searchMethod(tsym, methodName, paramTypes, new HashSet<ClassSymbol>());
jjg@1409 499 }
jjg@1409 500
jjg@1409 501 /** @see com.sun.tools.javadoc.ClassDocImpl#searchMethod */
jjg@1409 502 private MethodSymbol searchMethod(ClassSymbol tsym, Name methodName,
jjg@1409 503 List<Type> paramTypes, Set<ClassSymbol> searched) {
jjg@1409 504 //### Note that this search is not necessarily what the compiler would do!
jjg@1409 505
jjg@1409 506 // do not match constructors
jjg@1409 507 if (methodName == names.init)
jjg@1409 508 return null;
jjg@1409 509
jjg@1409 510 if (searched.contains(tsym))
jjg@1409 511 return null;
jjg@1409 512 searched.add(tsym);
jjg@1409 513
jjg@1409 514 // search current class
jjg@1409 515 com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(methodName);
jjg@1409 516
jjg@1409 517 //### Using modifier filter here isn't really correct,
jjg@1409 518 //### but emulates the old behavior. Instead, we should
jjg@1409 519 //### apply the normal rules of visibility and inheritance.
jjg@1409 520
jjg@1409 521 if (paramTypes == null) {
jjg@1409 522 // If no parameters specified, we are allowed to return
jjg@1409 523 // any method with a matching name. In practice, the old
jjg@1409 524 // code returned the first method, which is now the last!
jjg@1409 525 // In order to provide textually identical results, we
jjg@1409 526 // attempt to emulate the old behavior.
jjg@1409 527 MethodSymbol lastFound = null;
jjg@1409 528 for (; e.scope != null; e = e.next()) {
jjg@1409 529 if (e.sym.kind == Kinds.MTH) {
jjg@1409 530 if (e.sym.name == methodName) {
jjg@1409 531 lastFound = (MethodSymbol)e.sym;
jjg@1409 532 }
jjg@1409 533 }
jjg@1409 534 }
jjg@1409 535 if (lastFound != null) {
jjg@1409 536 return lastFound;
jjg@1409 537 }
jjg@1409 538 } else {
jjg@1409 539 for (; e.scope != null; e = e.next()) {
jjg@1409 540 if (e.sym != null &&
jjg@1409 541 e.sym.kind == Kinds.MTH) {
jjg@1409 542 if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) {
jjg@1409 543 return (MethodSymbol) e.sym;
jjg@1409 544 }
jjg@1409 545 }
jjg@1409 546 }
jjg@1409 547 }
jjg@1409 548
jjg@1409 549 //### If we found a MethodSymbol above, but which did not pass
jjg@1409 550 //### the modifier filter, we should return failure here!
jjg@1409 551
jjg@1409 552 // search superclass
jjg@1409 553 Type superclass = tsym.getSuperclass();
jjg@1409 554 if (superclass.tsym != null) {
jjg@1409 555 MethodSymbol msym = searchMethod((ClassSymbol) superclass.tsym, methodName, paramTypes, searched);
jjg@1409 556 if (msym != null) {
jjg@1409 557 return msym;
jjg@1409 558 }
jjg@1409 559 }
jjg@1409 560
jjg@1409 561 // search interfaces
jjg@1409 562 List<Type> intfs = tsym.getInterfaces();
jjg@1409 563 for (List<Type> l = intfs; l.nonEmpty(); l = l.tail) {
jjg@1409 564 Type intf = l.head;
jjg@1409 565 if (intf.isErroneous()) continue;
jjg@1409 566 MethodSymbol msym = searchMethod((ClassSymbol) intf.tsym, methodName, paramTypes, searched);
jjg@1409 567 if (msym != null) {
jjg@1409 568 return msym;
jjg@1409 569 }
jjg@1409 570 }
jjg@1409 571
jjg@1409 572 // search enclosing class
jjg@1409 573 ClassSymbol encl = tsym.owner.enclClass();
jjg@1409 574 if (encl != null) {
jjg@1409 575 MethodSymbol msym = searchMethod(encl, methodName, paramTypes, searched);
jjg@1409 576 if (msym != null) {
jjg@1409 577 return msym;
jjg@1409 578 }
jjg@1409 579 }
jjg@1409 580
jjg@1409 581 return null;
jjg@1409 582 }
jjg@1409 583
jjg@1409 584 /** @see com.sun.tools.javadoc.ClassDocImpl */
jjg@1409 585 private boolean hasParameterTypes(MethodSymbol method, List<Type> paramTypes) {
jjg@1409 586 if (paramTypes == null)
jjg@1409 587 return true;
jjg@1409 588
jjg@1409 589 if (method.params().size() != paramTypes.size())
jjg@1409 590 return false;
jjg@1409 591
jjg@1409 592 List<Type> methodParamTypes = types.erasureRecursive(method.asType()).getParameterTypes();
jjg@1409 593
jjg@1409 594 return (Type.isErroneous(paramTypes))
jjg@1409 595 ? fuzzyMatch(paramTypes, methodParamTypes)
jjg@1409 596 : types.isSameTypes(paramTypes, methodParamTypes);
jjg@1409 597 }
jjg@1409 598
jjg@1409 599 boolean fuzzyMatch(List<Type> paramTypes, List<Type> methodParamTypes) {
jjg@1409 600 List<Type> l1 = paramTypes;
jjg@1409 601 List<Type> l2 = methodParamTypes;
jjg@1409 602 while (l1.nonEmpty()) {
jjg@1409 603 if (!fuzzyMatch(l1.head, l2.head))
jjg@1409 604 return false;
jjg@1409 605 l1 = l1.tail;
jjg@1409 606 l2 = l2.tail;
jjg@1409 607 }
jjg@1409 608 return true;
jjg@1409 609 }
jjg@1409 610
jjg@1409 611 boolean fuzzyMatch(Type paramType, Type methodParamType) {
jjg@1409 612 Boolean b = fuzzyMatcher.visit(paramType, methodParamType);
jjg@1409 613 return (b == Boolean.TRUE);
jjg@1409 614 }
jjg@1409 615
jjg@1409 616 TypeRelation fuzzyMatcher = new TypeRelation() {
jjg@1409 617 @Override
jjg@1409 618 public Boolean visitType(Type t, Type s) {
jjg@1409 619 if (t == s)
jjg@1409 620 return true;
jjg@1409 621
jjg@1409 622 if (s.isPartial())
jjg@1409 623 return visit(s, t);
jjg@1409 624
jjg@1409 625 switch (t.getTag()) {
jjg@1409 626 case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
jjg@1409 627 case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
jjg@1409 628 return t.getTag() == s.getTag();
jjg@1409 629
jjg@1409 630 default:
jjg@1409 631 throw new AssertionError("fuzzyMatcher " + t.getTag());
jjg@1409 632 }
jjg@1409 633 }
jjg@1409 634
jjg@1409 635 @Override
jjg@1409 636 public Boolean visitArrayType(ArrayType t, Type s) {
jjg@1409 637 if (t == s)
jjg@1409 638 return true;
jjg@1409 639
jjg@1409 640 if (s.isPartial())
jjg@1409 641 return visit(s, t);
jjg@1409 642
jjg@1409 643 return s.getTag() == ARRAY
jjg@1409 644 && visit(t.elemtype, types.elemtype(s));
jjg@1409 645 }
jjg@1409 646
jjg@1409 647 @Override
jjg@1409 648 public Boolean visitClassType(ClassType t, Type s) {
jjg@1409 649 if (t == s)
jjg@1409 650 return true;
jjg@1409 651
jjg@1409 652 if (s.isPartial())
jjg@1409 653 return visit(s, t);
jjg@1409 654
jjg@1409 655 return t.tsym == s.tsym;
jjg@1409 656 }
jjg@1409 657
jjg@1409 658 @Override
jjg@1409 659 public Boolean visitErrorType(ErrorType t, Type s) {
jjg@1409 660 return s.getTag() == CLASS
jjg@1409 661 && t.tsym.name == ((ClassType) s).tsym.name;
jjg@1409 662 }
jjg@1409 663 };
jjg@1409 664
duke@1 665 public TypeMirror getTypeMirror(TreePath path) {
duke@1 666 Tree t = path.getLeaf();
duke@1 667 return ((JCTree)t).type;
duke@1 668 }
duke@1 669
duke@1 670 public JavacScope getScope(TreePath path) {
duke@1 671 return new JavacScope(getAttrContext(path));
duke@1 672 }
duke@1 673
jjg@783 674 public String getDocComment(TreePath path) {
jjg@783 675 CompilationUnitTree t = path.getCompilationUnit();
jjg@1280 676 Tree leaf = path.getLeaf();
jjg@1280 677 if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
jjg@783 678 JCCompilationUnit cu = (JCCompilationUnit) t;
jjg@783 679 if (cu.docComments != null) {
jjg@1280 680 return cu.docComments.getCommentText((JCTree) leaf);
jjg@783 681 }
jjg@783 682 }
jjg@783 683 return null;
jjg@783 684 }
jjg@783 685
jjg@1409 686 public DocCommentTree getDocCommentTree(TreePath path) {
jjg@1409 687 CompilationUnitTree t = path.getCompilationUnit();
jjg@1409 688 Tree leaf = path.getLeaf();
jjg@1409 689 if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
jjg@1409 690 JCCompilationUnit cu = (JCCompilationUnit) t;
jjg@1409 691 if (cu.docComments != null) {
jjg@1409 692 return cu.docComments.getCommentTree((JCTree) leaf);
jjg@1409 693 }
jjg@1409 694 }
jjg@1409 695 return null;
jjg@1409 696 }
jjg@1409 697
duke@1 698 public boolean isAccessible(Scope scope, TypeElement type) {
duke@1 699 if (scope instanceof JavacScope && type instanceof ClassSymbol) {
duke@1 700 Env<AttrContext> env = ((JavacScope) scope).env;
mcimadamore@741 701 return resolve.isAccessible(env, (ClassSymbol)type, true);
duke@1 702 } else
duke@1 703 return false;
duke@1 704 }
duke@1 705
duke@1 706 public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
duke@1 707 if (scope instanceof JavacScope
duke@1 708 && member instanceof Symbol
duke@1 709 && type instanceof com.sun.tools.javac.code.Type) {
duke@1 710 Env<AttrContext> env = ((JavacScope) scope).env;
mcimadamore@741 711 return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
duke@1 712 } else
duke@1 713 return false;
duke@1 714 }
duke@1 715
duke@1 716 private Env<AttrContext> getAttrContext(TreePath path) {
duke@1 717 if (!(path.getLeaf() instanceof JCTree)) // implicit null-check
duke@1 718 throw new IllegalArgumentException();
duke@1 719
jjg@1090 720 // if we're being invoked from a Tree API client via parse/enter/analyze,
jjg@1090 721 // we need to make sure all the classes have been entered;
jjg@1090 722 // if we're being invoked from JSR 199 or JSR 269, then the classes
jjg@1090 723 // will already have been entered.
duke@1 724 if (javacTaskImpl != null) {
duke@1 725 try {
duke@1 726 javacTaskImpl.enter(null);
duke@1 727 } catch (IOException e) {
duke@1 728 throw new Error("unexpected error while entering symbols: " + e);
duke@1 729 }
duke@1 730 }
duke@1 731
duke@1 732
duke@1 733 JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
ksrini@1327 734 Copier copier = createCopier(treeMaker.forToplevel(unit));
duke@1 735
duke@1 736 Env<AttrContext> env = null;
duke@1 737 JCMethodDecl method = null;
duke@1 738 JCVariableDecl field = null;
duke@1 739
duke@1 740 List<Tree> l = List.nil();
duke@1 741 TreePath p = path;
duke@1 742 while (p != null) {
duke@1 743 l = l.prepend(p.getLeaf());
duke@1 744 p = p.getParentPath();
duke@1 745 }
duke@1 746
duke@1 747 for ( ; l.nonEmpty(); l = l.tail) {
duke@1 748 Tree tree = l.head;
duke@1 749 switch (tree.getKind()) {
duke@1 750 case COMPILATION_UNIT:
duke@1 751 // System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
duke@1 752 env = enter.getTopLevelEnv((JCCompilationUnit)tree);
duke@1 753 break;
jjg@727 754 case ANNOTATION_TYPE:
duke@1 755 case CLASS:
jjg@727 756 case ENUM:
jjg@682 757 case INTERFACE:
duke@1 758 // System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
duke@1 759 env = enter.getClassEnv(((JCClassDecl)tree).sym);
duke@1 760 break;
duke@1 761 case METHOD:
duke@1 762 // System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
duke@1 763 method = (JCMethodDecl)tree;
duke@1 764 break;
duke@1 765 case VARIABLE:
duke@1 766 // System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
duke@1 767 field = (JCVariableDecl)tree;
duke@1 768 break;
duke@1 769 case BLOCK: {
duke@1 770 // System.err.println("BLOCK: ");
jjg@1090 771 if (method != null) {
jjg@1090 772 try {
jjg@1090 773 Assert.check(method.body == tree);
jjg@1090 774 method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
jjg@1090 775 env = memberEnter.getMethodEnv(method, env);
jjg@1090 776 env = attribStatToTree(method.body, env, copier.leafCopy);
jjg@1090 777 } finally {
jjg@1090 778 method.body = (JCBlock) tree;
jjg@1090 779 }
jjg@1090 780 } else {
jjg@1090 781 JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
jjg@1090 782 env = attribStatToTree(body, env, copier.leafCopy);
jjg@1090 783 }
duke@1 784 return env;
duke@1 785 }
duke@1 786 default:
duke@1 787 // System.err.println("DEFAULT: " + tree.getKind());
duke@1 788 if (field != null && field.getInitializer() == tree) {
duke@1 789 env = memberEnter.getInitEnv(field, env);
duke@1 790 JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf());
duke@1 791 env = attribExprToTree(expr, env, copier.leafCopy);
duke@1 792 return env;
duke@1 793 }
duke@1 794 }
duke@1 795 }
jjg@1090 796 return (field != null) ? memberEnter.getInitEnv(field, env) : env;
duke@1 797 }
duke@1 798
duke@1 799 private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
duke@1 800 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
duke@1 801 try {
duke@1 802 return attr.attribStatToTree(stat, env, tree);
duke@1 803 } finally {
duke@1 804 log.useSource(prev);
duke@1 805 }
duke@1 806 }
duke@1 807
duke@1 808 private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
duke@1 809 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
duke@1 810 try {
duke@1 811 return attr.attribExprToTree(expr, env, tree);
duke@1 812 } finally {
duke@1 813 log.useSource(prev);
duke@1 814 }
duke@1 815 }
duke@1 816
duke@1 817 /**
duke@1 818 * Makes a copy of a tree, noting the value resulting from copying a particular leaf.
duke@1 819 **/
ksrini@1327 820 protected static class Copier extends TreeCopier<JCTree> {
duke@1 821 JCTree leafCopy = null;
duke@1 822
ksrini@1327 823 protected Copier(TreeMaker M) {
duke@1 824 super(M);
duke@1 825 }
duke@1 826
jjg@696 827 @Override
duke@1 828 public <T extends JCTree> T copy(T t, JCTree leaf) {
duke@1 829 T t2 = super.copy(t, leaf);
duke@1 830 if (t == leaf)
duke@1 831 leafCopy = t2;
duke@1 832 return t2;
duke@1 833 }
duke@1 834 }
jjg@110 835
ksrini@1327 836 protected Copier createCopier(TreeMaker maker) {
ksrini@1327 837 return new Copier(maker);
ksrini@1327 838 }
ksrini@1327 839
jjg@110 840 /**
jjg@110 841 * Gets the original type from the ErrorType object.
jjg@110 842 * @param errorType The errorType for which we want to get the original type.
jjg@110 843 * @returns TypeMirror corresponding to the original type, replaced by the ErrorType.
jjg@110 844 * noType (type.tag == NONE) is returned if there is no original type.
jjg@110 845 */
jjg@110 846 public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
jjg@110 847 if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
jjg@110 848 return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
jjg@110 849 }
jjg@110 850
jjg@110 851 return com.sun.tools.javac.code.Type.noType;
jjg@110 852 }
jjg@308 853
jjg@308 854 /**
jjg@308 855 * Prints a message of the specified kind at the location of the
jjg@308 856 * tree within the provided compilation unit
jjg@308 857 *
jjg@308 858 * @param kind the kind of message
jjg@308 859 * @param msg the message, or an empty string if none
jjg@308 860 * @param t the tree to use as a position hint
jjg@308 861 * @param root the compilation unit that contains tree
jjg@308 862 */
jjg@308 863 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
jjg@308 864 com.sun.source.tree.Tree t,
jjg@308 865 com.sun.source.tree.CompilationUnitTree root) {
jjg@1409 866 printMessage(kind, msg, ((JCTree) t).pos(), root);
jjg@1409 867 }
jjg@1409 868
jjg@1409 869 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
jjg@1409 870 com.sun.source.doctree.DocTree t,
jjg@1409 871 com.sun.source.doctree.DocCommentTree c,
jjg@1409 872 com.sun.source.tree.CompilationUnitTree root) {
jjg@1409 873 printMessage(kind, msg, ((DCTree) t).pos((DCDocComment) c), root);
jjg@1409 874 }
jjg@1409 875
jjg@1409 876 private void printMessage(Diagnostic.Kind kind, CharSequence msg,
jjg@1409 877 JCDiagnostic.DiagnosticPosition pos,
jjg@1409 878 com.sun.source.tree.CompilationUnitTree root) {
jjg@308 879 JavaFileObject oldSource = null;
jjg@308 880 JavaFileObject newSource = null;
jjg@308 881
jjg@308 882 newSource = root.getSourceFile();
jjg@1409 883 if (newSource == null) {
jjg@1409 884 pos = null;
jjg@1409 885 } else {
jjg@308 886 oldSource = log.useSource(newSource);
jjg@308 887 }
jjg@308 888
jjg@308 889 try {
jjg@308 890 switch (kind) {
jjg@308 891 case ERROR:
jjg@308 892 boolean prev = log.multipleErrors;
jjg@308 893 try {
jjg@308 894 log.error(pos, "proc.messager", msg.toString());
jjg@308 895 } finally {
jjg@308 896 log.multipleErrors = prev;
jjg@308 897 }
jjg@308 898 break;
jjg@308 899
jjg@308 900 case WARNING:
jjg@308 901 log.warning(pos, "proc.messager", msg.toString());
jjg@308 902 break;
jjg@308 903
jjg@308 904 case MANDATORY_WARNING:
jjg@308 905 log.mandatoryWarning(pos, "proc.messager", msg.toString());
jjg@308 906 break;
jjg@308 907
jjg@308 908 default:
jjg@308 909 log.note(pos, "proc.messager", msg.toString());
jjg@308 910 }
jjg@308 911 } finally {
jjg@308 912 if (oldSource != null)
jjg@308 913 log.useSource(oldSource);
jjg@308 914 }
jjg@308 915 }
jjg@988 916
jjg@988 917 @Override
jjg@988 918 public TypeMirror getLub(CatchTree tree) {
jjg@988 919 JCCatch ct = (JCCatch) tree;
jjg@988 920 JCVariableDecl v = ct.param;
jjg@988 921 if (v.type != null && v.type.getKind() == TypeKind.UNION) {
jjg@988 922 UnionClassType ut = (UnionClassType) v.type;
jjg@988 923 return ut.getLub();
jjg@988 924 } else {
jjg@988 925 return v.type;
jjg@988 926 }
jjg@988 927 }
duke@1 928 }

mercurial