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

Mon, 17 Dec 2012 07:47:05 -0800

author
jjg
date
Mon, 17 Dec 2012 07:47:05 -0800
changeset 1455
75ab654b5cd5
parent 1416
c0f0c41cafa0
child 1704
ed918a442b83
permissions
-rw-r--r--

8004832: Add new doclint package
Reviewed-by: mcimadamore

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

mercurial