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

Mon, 19 Nov 2012 11:38:49 -0800

author
jjg
date
Mon, 19 Nov 2012 11:38:49 -0800
changeset 1416
c0f0c41cafa0
parent 1409
33abf479f202
child 1455
75ab654b5cd5
permissions
-rw-r--r--

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

mercurial