duke@1: /*
jjg@1210: * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.api;
duke@1:
duke@1: import java.io.IOException;
jjg@1409: import java.util.HashSet;
jjg@1409: import java.util.Set;
jjg@1210:
duke@1: import javax.annotation.processing.ProcessingEnvironment;
duke@1: import javax.lang.model.element.AnnotationMirror;
duke@1: import javax.lang.model.element.AnnotationValue;
duke@1: import javax.lang.model.element.Element;
duke@1: import javax.lang.model.element.ExecutableElement;
duke@1: import javax.lang.model.element.TypeElement;
duke@1: import javax.lang.model.type.DeclaredType;
jjg@988: import javax.lang.model.type.TypeKind;
duke@1: import javax.lang.model.type.TypeMirror;
jjg@308: import javax.tools.Diagnostic;
duke@1: import javax.tools.JavaCompiler;
duke@1: import javax.tools.JavaFileObject;
duke@1:
jjg@1409: import com.sun.source.doctree.DocCommentTree;
jjg@1409: import com.sun.source.doctree.ReferenceTree;
jjg@988: import com.sun.source.tree.CatchTree;
duke@1: import com.sun.source.tree.CompilationUnitTree;
duke@1: import com.sun.source.tree.Scope;
duke@1: import com.sun.source.tree.Tree;
jjg@1409: import com.sun.source.util.DocTrees;
jjg@1210: import com.sun.source.util.JavacTask;
duke@1: import com.sun.source.util.SourcePositions;
duke@1: import com.sun.source.util.TreePath;
jjg@672: import com.sun.tools.javac.code.Flags;
jjg@1409: import com.sun.tools.javac.code.Kinds;
jjg@1210: import com.sun.tools.javac.code.Symbol;
duke@1: import com.sun.tools.javac.code.Symbol.ClassSymbol;
jjg@1409: import com.sun.tools.javac.code.Symbol.MethodSymbol;
jjg@1409: import com.sun.tools.javac.code.Symbol.PackageSymbol;
duke@1: import com.sun.tools.javac.code.Symbol.TypeSymbol;
jjg@1409: import com.sun.tools.javac.code.Symbol.VarSymbol;
jjg@1409: import com.sun.tools.javac.code.Type;
jjg@1409: import com.sun.tools.javac.code.Type.ArrayType;
jjg@1409: import com.sun.tools.javac.code.Type.ClassType;
jjg@1409: import com.sun.tools.javac.code.Type.ErrorType;
jjg@988: import com.sun.tools.javac.code.Type.UnionClassType;
jjg@1409: import com.sun.tools.javac.code.Types;
jjg@1409: import com.sun.tools.javac.code.Types.TypeRelation;
duke@1: import com.sun.tools.javac.comp.Attr;
duke@1: import com.sun.tools.javac.comp.AttrContext;
duke@1: import com.sun.tools.javac.comp.Enter;
duke@1: import com.sun.tools.javac.comp.Env;
duke@1: import com.sun.tools.javac.comp.MemberEnter;
duke@1: import com.sun.tools.javac.comp.Resolve;
duke@1: import com.sun.tools.javac.model.JavacElements;
jjg@1357: import com.sun.tools.javac.processing.JavacProcessingEnvironment;
jjg@1409: import com.sun.tools.javac.tree.DCTree;
jjg@1409: import com.sun.tools.javac.tree.DCTree.DCDocComment;
jjg@1409: import com.sun.tools.javac.tree.DCTree.DCReference;
jjg@1280: import com.sun.tools.javac.tree.EndPosTable;
jjg@1210: import com.sun.tools.javac.tree.JCTree;
duke@1: import com.sun.tools.javac.tree.JCTree.*;
duke@1: import com.sun.tools.javac.tree.TreeCopier;
duke@1: import com.sun.tools.javac.tree.TreeInfo;
duke@1: import com.sun.tools.javac.tree.TreeMaker;
jjg@1455: import com.sun.tools.javac.util.Abort;
jjg@1090: import com.sun.tools.javac.util.Assert;
duke@1: import com.sun.tools.javac.util.Context;
jjg@308: import com.sun.tools.javac.util.JCDiagnostic;
duke@1: import com.sun.tools.javac.util.List;
jjg@1409: import com.sun.tools.javac.util.ListBuffer;
duke@1: import com.sun.tools.javac.util.Log;
jjg@1409: import com.sun.tools.javac.util.Name;
jjg@1409: import com.sun.tools.javac.util.Names;
duke@1: import com.sun.tools.javac.util.Pair;
jjg@1409: import static com.sun.tools.javac.code.TypeTag.*;
duke@1:
duke@1: /**
duke@1: * Provides an implementation of Trees.
duke@1: *
jjg@581: *
This is NOT part of any supported API.
duke@1: * If you write code that depends on this, you do so at your own
duke@1: * risk. This code and its internal interfaces are subject to change
duke@1: * or deletion without notice.
duke@1: *
duke@1: * @author Peter von der Ahé
duke@1: */
jjg@1409: public class JavacTrees extends DocTrees {
duke@1:
jjg@696: // in a world of a single context per compilation, these would all be final
jjg@696: private Resolve resolve;
jjg@696: private Enter enter;
jjg@696: private Log log;
jjg@696: private MemberEnter memberEnter;
jjg@696: private Attr attr;
jjg@696: private TreeMaker treeMaker;
jjg@696: private JavacElements elements;
jjg@696: private JavacTaskImpl javacTaskImpl;
jjg@1409: private Names names;
jjg@1409: private Types types;
duke@1:
jjg@1210: // called reflectively from Trees.instance(CompilationTask task)
duke@1: public static JavacTrees instance(JavaCompiler.CompilationTask task) {
jjg@1416: if (!(task instanceof BasicJavacTask))
duke@1: throw new IllegalArgumentException();
jjg@1416: return instance(((BasicJavacTask)task).getContext());
duke@1: }
duke@1:
jjg@1210: // called reflectively from Trees.instance(ProcessingEnvironment env)
duke@1: public static JavacTrees instance(ProcessingEnvironment env) {
duke@1: if (!(env instanceof JavacProcessingEnvironment))
duke@1: throw new IllegalArgumentException();
duke@1: return instance(((JavacProcessingEnvironment)env).getContext());
duke@1: }
duke@1:
duke@1: public static JavacTrees instance(Context context) {
duke@1: JavacTrees instance = context.get(JavacTrees.class);
duke@1: if (instance == null)
duke@1: instance = new JavacTrees(context);
duke@1: return instance;
duke@1: }
duke@1:
ksrini@1327: protected JavacTrees(Context context) {
duke@1: context.put(JavacTrees.class, this);
jjg@696: init(context);
jjg@696: }
jjg@696:
jjg@696: public void updateContext(Context context) {
jjg@696: init(context);
jjg@696: }
jjg@696:
jjg@696: private void init(Context context) {
duke@1: attr = Attr.instance(context);
duke@1: enter = Enter.instance(context);
duke@1: elements = JavacElements.instance(context);
duke@1: log = Log.instance(context);
duke@1: resolve = Resolve.instance(context);
duke@1: treeMaker = TreeMaker.instance(context);
duke@1: memberEnter = MemberEnter.instance(context);
jjg@1409: names = Names.instance(context);
jjg@1409: types = Types.instance(context);
jjg@1210:
jjg@1210: JavacTask t = context.get(JavacTask.class);
jjg@1210: if (t instanceof JavacTaskImpl)
jjg@1210: javacTaskImpl = (JavacTaskImpl) t;
duke@1: }
duke@1:
duke@1: public SourcePositions getSourcePositions() {
duke@1: return new SourcePositions() {
duke@1: public long getStartPosition(CompilationUnitTree file, Tree tree) {
duke@1: return TreeInfo.getStartPos((JCTree) tree);
duke@1: }
duke@1:
duke@1: public long getEndPosition(CompilationUnitTree file, Tree tree) {
ksrini@1138: EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
ksrini@1138: return TreeInfo.getEndPos((JCTree) tree, endPosTable);
duke@1: }
duke@1: };
duke@1: }
duke@1:
duke@1: public JCClassDecl getTree(TypeElement element) {
duke@1: return (JCClassDecl) getTree((Element) element);
duke@1: }
duke@1:
duke@1: public JCMethodDecl getTree(ExecutableElement method) {
duke@1: return (JCMethodDecl) getTree((Element) method);
duke@1: }
duke@1:
duke@1: public JCTree getTree(Element element) {
duke@1: Symbol symbol = (Symbol) element;
duke@1: TypeSymbol enclosing = symbol.enclClass();
duke@1: Env env = enter.getEnv(enclosing);
duke@1: if (env == null)
duke@1: return null;
duke@1: JCClassDecl classNode = env.enclClass;
duke@1: if (classNode != null) {
duke@1: if (TreeInfo.symbolFor(classNode) == element)
duke@1: return classNode;
duke@1: for (JCTree node : classNode.getMembers())
duke@1: if (TreeInfo.symbolFor(node) == element)
duke@1: return node;
duke@1: }
duke@1: return null;
duke@1: }
duke@1:
duke@1: public JCTree getTree(Element e, AnnotationMirror a) {
duke@1: return getTree(e, a, null);
duke@1: }
duke@1:
duke@1: public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
duke@1: Pair treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
duke@1: if (treeTopLevel == null)
duke@1: return null;
duke@1: return treeTopLevel.fst;
duke@1: }
duke@1:
duke@1: public TreePath getPath(CompilationUnitTree unit, Tree node) {
duke@1: return TreePath.getPath(unit, node);
duke@1: }
duke@1:
duke@1: public TreePath getPath(Element e) {
duke@1: return getPath(e, null, null);
duke@1: }
duke@1:
duke@1: public TreePath getPath(Element e, AnnotationMirror a) {
duke@1: return getPath(e, a, null);
duke@1: }
duke@1:
duke@1: public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
duke@1: final Pair treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
duke@1: if (treeTopLevel == null)
duke@1: return null;
duke@1: return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
duke@1: }
duke@1:
duke@1: public Element getElement(TreePath path) {
jjg@672: JCTree tree = (JCTree) path.getLeaf();
jjg@672: Symbol sym = TreeInfo.symbolFor(tree);
jjg@1455: if (sym == null) {
jjg@1455: if (TreeInfo.isDeclaration(tree)) {
jjg@1455: for (TreePath p = path; p != null; p = p.getParentPath()) {
jjg@1455: JCTree t = (JCTree) p.getLeaf();
jjg@1455: if (t.hasTag(JCTree.Tag.CLASSDEF)) {
jjg@1455: JCClassDecl ct = (JCClassDecl) t;
jjg@1455: if (ct.sym != null) {
jjg@1455: if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
jjg@1455: attr.attribClass(ct.pos(), ct.sym);
jjg@1455: sym = TreeInfo.symbolFor(tree);
jjg@1455: }
jjg@1455: break;
jjg@672: }
jjg@672: }
jjg@672: }
jjg@1455: } else if (tree.hasTag(Tag.TOPLEVEL)) {
jjg@1455: JCCompilationUnit cu = (JCCompilationUnit) tree;
jjg@1455: if (cu.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE)) {
jjg@1455: sym = cu.packge;
jjg@1455: }
jjg@672: }
jjg@672: }
jjg@672: return sym;
duke@1: }
duke@1:
jjg@1409: @Override
jjg@1409: public Element getElement(TreePath path, ReferenceTree reference) {
jjg@1409: if (!(reference instanceof DCReference))
jjg@1409: return null;
jjg@1409: DCReference ref = (DCReference) reference;
jjg@1409:
jjg@1409: Env env = getAttrContext(path);
jjg@1409:
jjg@1409: Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
jjg@1409: new Log.DeferredDiagnosticHandler(log);
jjg@1409: try {
jjg@1409: final ClassSymbol tsym;
jjg@1409: final Name memberName;
jjg@1409: if (ref.qualifierExpression == null) {
jjg@1409: tsym = env.enclClass.sym;
jjg@1409: memberName = ref.memberName;
jjg@1409: } else {
jjg@1409: // See if the qualifierExpression is a type or package name.
jjg@1409: // javac does not provide the exact method required, so
jjg@1409: // we first check if qualifierExpression identifies a type,
jjg@1409: // and if not, then we check to see if it identifies a package.
jjg@1409: Type t = attr.attribType(ref.qualifierExpression, env);
jjg@1409: if (t.isErroneous()) {
jjg@1409: if (ref.memberName == null) {
jjg@1409: // Attr/Resolve assume packages exist and create symbols as needed
jjg@1409: // so use getPackageElement to restrict search to existing packages
jjg@1409: PackageSymbol pck = elements.getPackageElement(ref.qualifierExpression.toString());
jjg@1409: if (pck != null) {
jjg@1409: return pck;
jjg@1409: } else if (ref.qualifierExpression.hasTag(JCTree.Tag.IDENT)) {
jjg@1409: // fixup: allow "identifier" instead of "#identifier"
jjg@1409: // for compatibility with javadoc
jjg@1409: tsym = env.enclClass.sym;
jjg@1409: memberName = ((JCIdent) ref.qualifierExpression).name;
jjg@1409: } else
jjg@1409: return null;
jjg@1409: } else {
jjg@1409: return null;
jjg@1409: }
jjg@1409: } else {
jjg@1409: tsym = (ClassSymbol) t.tsym;
jjg@1409: memberName = ref.memberName;
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: if (memberName == null)
jjg@1409: return tsym;
jjg@1409:
jjg@1409: final List paramTypes;
jjg@1409: if (ref.paramTypes == null)
jjg@1409: paramTypes = null;
jjg@1409: else {
jjg@1409: ListBuffer lb = new ListBuffer();
jjg@1409: for (List l = ref.paramTypes; l.nonEmpty(); l = l.tail) {
jjg@1409: JCTree tree = l.head;
jjg@1409: Type t = attr.attribType(tree, env);
jjg@1409: lb.add(t);
jjg@1409: }
jjg@1409: paramTypes = lb.toList();
jjg@1409: }
jjg@1409:
jjg@1409: Symbol msym = (memberName == tsym.name)
jjg@1409: ? findConstructor(tsym, paramTypes)
jjg@1409: : findMethod(tsym, memberName, paramTypes);
jjg@1409: if (paramTypes != null) {
jjg@1409: // explicit (possibly empty) arg list given, so cannot be a field
jjg@1409: return msym;
jjg@1409: }
jjg@1409:
jjg@1409: VarSymbol vsym = (ref.paramTypes != null) ? null : findField(tsym, memberName);
jjg@1409: // prefer a field over a method with no parameters
jjg@1409: if (vsym != null &&
jjg@1409: (msym == null ||
jjg@1409: types.isSubtypeUnchecked(vsym.enclClass().asType(), msym.enclClass().asType()))) {
jjg@1409: return vsym;
jjg@1409: } else {
jjg@1409: return msym;
jjg@1409: }
jjg@1455: } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file
jjg@1455: return null;
jjg@1409: } finally {
jjg@1409: log.popDiagnosticHandler(deferredDiagnosticHandler);
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: /** @see com.sun.tools.javadoc.ClassDocImpl#findField */
jjg@1409: private VarSymbol findField(ClassSymbol tsym, Name fieldName) {
jjg@1409: return searchField(tsym, fieldName, new HashSet());
jjg@1409: }
jjg@1409:
jjg@1409: /** @see com.sun.tools.javadoc.ClassDocImpl#searchField */
jjg@1409: private VarSymbol searchField(ClassSymbol tsym, Name fieldName, Set searched) {
jjg@1409: if (searched.contains(tsym)) {
jjg@1409: return null;
jjg@1409: }
jjg@1409: searched.add(tsym);
jjg@1409:
jjg@1409: for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(fieldName);
jjg@1409: e.scope != null; e = e.next()) {
jjg@1409: if (e.sym.kind == Kinds.VAR) {
jjg@1409: return (VarSymbol)e.sym;
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: //### If we found a VarSymbol above, but which did not pass
jjg@1409: //### the modifier filter, we should return failure here!
jjg@1409:
jjg@1409: ClassSymbol encl = tsym.owner.enclClass();
jjg@1409: if (encl != null) {
jjg@1409: VarSymbol vsym = searchField(encl, fieldName, searched);
jjg@1409: if (vsym != null) {
jjg@1409: return vsym;
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: // search superclass
jjg@1409: Type superclass = tsym.getSuperclass();
jjg@1409: if (superclass.tsym != null) {
jjg@1409: VarSymbol vsym = searchField((ClassSymbol) superclass.tsym, fieldName, searched);
jjg@1409: if (vsym != null) {
jjg@1409: return vsym;
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: // search interfaces
jjg@1409: List intfs = tsym.getInterfaces();
jjg@1409: for (List l = intfs; l.nonEmpty(); l = l.tail) {
jjg@1409: Type intf = l.head;
jjg@1409: if (intf.isErroneous()) continue;
jjg@1409: VarSymbol vsym = searchField((ClassSymbol) intf.tsym, fieldName, searched);
jjg@1409: if (vsym != null) {
jjg@1409: return vsym;
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: /** @see com.sun.tools.javadoc.ClassDocImpl#findConstructor */
jjg@1409: MethodSymbol findConstructor(ClassSymbol tsym, List paramTypes) {
jjg@1409: for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(names.init);
jjg@1409: e.scope != null; e = e.next()) {
jjg@1409: if (e.sym.kind == Kinds.MTH) {
jjg@1409: if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) {
jjg@1409: return (MethodSymbol) e.sym;
jjg@1409: }
jjg@1409: }
jjg@1409: }
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: /** @see com.sun.tools.javadoc.ClassDocImpl#findMethod */
jjg@1409: private MethodSymbol findMethod(ClassSymbol tsym, Name methodName, List paramTypes) {
jjg@1409: return searchMethod(tsym, methodName, paramTypes, new HashSet());
jjg@1409: }
jjg@1409:
jjg@1409: /** @see com.sun.tools.javadoc.ClassDocImpl#searchMethod */
jjg@1409: private MethodSymbol searchMethod(ClassSymbol tsym, Name methodName,
jjg@1409: List paramTypes, Set searched) {
jjg@1409: //### Note that this search is not necessarily what the compiler would do!
jjg@1409:
jjg@1409: // do not match constructors
jjg@1409: if (methodName == names.init)
jjg@1409: return null;
jjg@1409:
jjg@1409: if (searched.contains(tsym))
jjg@1409: return null;
jjg@1409: searched.add(tsym);
jjg@1409:
jjg@1409: // search current class
jjg@1409: com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(methodName);
jjg@1409:
jjg@1409: //### Using modifier filter here isn't really correct,
jjg@1409: //### but emulates the old behavior. Instead, we should
jjg@1409: //### apply the normal rules of visibility and inheritance.
jjg@1409:
jjg@1409: if (paramTypes == null) {
jjg@1409: // If no parameters specified, we are allowed to return
jjg@1409: // any method with a matching name. In practice, the old
jjg@1409: // code returned the first method, which is now the last!
jjg@1409: // In order to provide textually identical results, we
jjg@1409: // attempt to emulate the old behavior.
jjg@1409: MethodSymbol lastFound = null;
jjg@1409: for (; e.scope != null; e = e.next()) {
jjg@1409: if (e.sym.kind == Kinds.MTH) {
jjg@1409: if (e.sym.name == methodName) {
jjg@1409: lastFound = (MethodSymbol)e.sym;
jjg@1409: }
jjg@1409: }
jjg@1409: }
jjg@1409: if (lastFound != null) {
jjg@1409: return lastFound;
jjg@1409: }
jjg@1409: } else {
jjg@1409: for (; e.scope != null; e = e.next()) {
jjg@1409: if (e.sym != null &&
jjg@1409: e.sym.kind == Kinds.MTH) {
jjg@1409: if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) {
jjg@1409: return (MethodSymbol) e.sym;
jjg@1409: }
jjg@1409: }
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: //### If we found a MethodSymbol above, but which did not pass
jjg@1409: //### the modifier filter, we should return failure here!
jjg@1409:
jjg@1409: // search superclass
jjg@1409: Type superclass = tsym.getSuperclass();
jjg@1409: if (superclass.tsym != null) {
jjg@1409: MethodSymbol msym = searchMethod((ClassSymbol) superclass.tsym, methodName, paramTypes, searched);
jjg@1409: if (msym != null) {
jjg@1409: return msym;
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: // search interfaces
jjg@1409: List intfs = tsym.getInterfaces();
jjg@1409: for (List l = intfs; l.nonEmpty(); l = l.tail) {
jjg@1409: Type intf = l.head;
jjg@1409: if (intf.isErroneous()) continue;
jjg@1409: MethodSymbol msym = searchMethod((ClassSymbol) intf.tsym, methodName, paramTypes, searched);
jjg@1409: if (msym != null) {
jjg@1409: return msym;
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: // search enclosing class
jjg@1409: ClassSymbol encl = tsym.owner.enclClass();
jjg@1409: if (encl != null) {
jjg@1409: MethodSymbol msym = searchMethod(encl, methodName, paramTypes, searched);
jjg@1409: if (msym != null) {
jjg@1409: return msym;
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: return null;
jjg@1409: }
jjg@1409:
jjg@1409: /** @see com.sun.tools.javadoc.ClassDocImpl */
jjg@1409: private boolean hasParameterTypes(MethodSymbol method, List paramTypes) {
jjg@1409: if (paramTypes == null)
jjg@1409: return true;
jjg@1409:
jjg@1409: if (method.params().size() != paramTypes.size())
jjg@1409: return false;
jjg@1409:
jjg@1409: List methodParamTypes = types.erasureRecursive(method.asType()).getParameterTypes();
jjg@1409:
jjg@1409: return (Type.isErroneous(paramTypes))
jjg@1409: ? fuzzyMatch(paramTypes, methodParamTypes)
jjg@1409: : types.isSameTypes(paramTypes, methodParamTypes);
jjg@1409: }
jjg@1409:
jjg@1409: boolean fuzzyMatch(List paramTypes, List methodParamTypes) {
jjg@1409: List l1 = paramTypes;
jjg@1409: List l2 = methodParamTypes;
jjg@1409: while (l1.nonEmpty()) {
jjg@1409: if (!fuzzyMatch(l1.head, l2.head))
jjg@1409: return false;
jjg@1409: l1 = l1.tail;
jjg@1409: l2 = l2.tail;
jjg@1409: }
jjg@1409: return true;
jjg@1409: }
jjg@1409:
jjg@1409: boolean fuzzyMatch(Type paramType, Type methodParamType) {
jjg@1409: Boolean b = fuzzyMatcher.visit(paramType, methodParamType);
jjg@1409: return (b == Boolean.TRUE);
jjg@1409: }
jjg@1409:
jjg@1409: TypeRelation fuzzyMatcher = new TypeRelation() {
jjg@1409: @Override
jjg@1409: public Boolean visitType(Type t, Type s) {
jjg@1409: if (t == s)
jjg@1409: return true;
jjg@1409:
jjg@1409: if (s.isPartial())
jjg@1409: return visit(s, t);
jjg@1409:
jjg@1409: switch (t.getTag()) {
jjg@1409: case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
jjg@1409: case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
jjg@1409: return t.getTag() == s.getTag();
jjg@1409:
jjg@1409: default:
jjg@1409: throw new AssertionError("fuzzyMatcher " + t.getTag());
jjg@1409: }
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public Boolean visitArrayType(ArrayType t, Type s) {
jjg@1409: if (t == s)
jjg@1409: return true;
jjg@1409:
jjg@1409: if (s.isPartial())
jjg@1409: return visit(s, t);
jjg@1409:
jjg@1409: return s.getTag() == ARRAY
jjg@1409: && visit(t.elemtype, types.elemtype(s));
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public Boolean visitClassType(ClassType t, Type s) {
jjg@1409: if (t == s)
jjg@1409: return true;
jjg@1409:
jjg@1409: if (s.isPartial())
jjg@1409: return visit(s, t);
jjg@1409:
jjg@1409: return t.tsym == s.tsym;
jjg@1409: }
jjg@1409:
jjg@1409: @Override
jjg@1409: public Boolean visitErrorType(ErrorType t, Type s) {
jjg@1409: return s.getTag() == CLASS
jjg@1409: && t.tsym.name == ((ClassType) s).tsym.name;
jjg@1409: }
jjg@1409: };
jjg@1409:
duke@1: public TypeMirror getTypeMirror(TreePath path) {
duke@1: Tree t = path.getLeaf();
duke@1: return ((JCTree)t).type;
duke@1: }
duke@1:
duke@1: public JavacScope getScope(TreePath path) {
duke@1: return new JavacScope(getAttrContext(path));
duke@1: }
duke@1:
jjg@783: public String getDocComment(TreePath path) {
jjg@783: CompilationUnitTree t = path.getCompilationUnit();
jjg@1280: Tree leaf = path.getLeaf();
jjg@1280: if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
jjg@783: JCCompilationUnit cu = (JCCompilationUnit) t;
jjg@783: if (cu.docComments != null) {
jjg@1280: return cu.docComments.getCommentText((JCTree) leaf);
jjg@783: }
jjg@783: }
jjg@783: return null;
jjg@783: }
jjg@783:
jjg@1409: public DocCommentTree getDocCommentTree(TreePath path) {
jjg@1409: CompilationUnitTree t = path.getCompilationUnit();
jjg@1409: Tree leaf = path.getLeaf();
jjg@1409: if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
jjg@1409: JCCompilationUnit cu = (JCCompilationUnit) t;
jjg@1409: if (cu.docComments != null) {
jjg@1409: return cu.docComments.getCommentTree((JCTree) leaf);
jjg@1409: }
jjg@1409: }
jjg@1409: return null;
jjg@1409: }
jjg@1409:
duke@1: public boolean isAccessible(Scope scope, TypeElement type) {
duke@1: if (scope instanceof JavacScope && type instanceof ClassSymbol) {
duke@1: Env env = ((JavacScope) scope).env;
mcimadamore@741: return resolve.isAccessible(env, (ClassSymbol)type, true);
duke@1: } else
duke@1: return false;
duke@1: }
duke@1:
duke@1: public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
duke@1: if (scope instanceof JavacScope
duke@1: && member instanceof Symbol
duke@1: && type instanceof com.sun.tools.javac.code.Type) {
duke@1: Env env = ((JavacScope) scope).env;
mcimadamore@741: return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
duke@1: } else
duke@1: return false;
duke@1: }
duke@1:
duke@1: private Env getAttrContext(TreePath path) {
duke@1: if (!(path.getLeaf() instanceof JCTree)) // implicit null-check
duke@1: throw new IllegalArgumentException();
duke@1:
jjg@1090: // if we're being invoked from a Tree API client via parse/enter/analyze,
jjg@1090: // we need to make sure all the classes have been entered;
jjg@1090: // if we're being invoked from JSR 199 or JSR 269, then the classes
jjg@1090: // will already have been entered.
duke@1: if (javacTaskImpl != null) {
duke@1: try {
duke@1: javacTaskImpl.enter(null);
duke@1: } catch (IOException e) {
duke@1: throw new Error("unexpected error while entering symbols: " + e);
duke@1: }
duke@1: }
duke@1:
duke@1:
duke@1: JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
ksrini@1327: Copier copier = createCopier(treeMaker.forToplevel(unit));
duke@1:
duke@1: Env env = null;
duke@1: JCMethodDecl method = null;
duke@1: JCVariableDecl field = null;
duke@1:
duke@1: List l = List.nil();
duke@1: TreePath p = path;
duke@1: while (p != null) {
duke@1: l = l.prepend(p.getLeaf());
duke@1: p = p.getParentPath();
duke@1: }
duke@1:
duke@1: for ( ; l.nonEmpty(); l = l.tail) {
duke@1: Tree tree = l.head;
duke@1: switch (tree.getKind()) {
duke@1: case COMPILATION_UNIT:
duke@1: // System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
duke@1: env = enter.getTopLevelEnv((JCCompilationUnit)tree);
duke@1: break;
jjg@727: case ANNOTATION_TYPE:
duke@1: case CLASS:
jjg@727: case ENUM:
jjg@682: case INTERFACE:
duke@1: // System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
duke@1: env = enter.getClassEnv(((JCClassDecl)tree).sym);
duke@1: break;
duke@1: case METHOD:
duke@1: // System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
duke@1: method = (JCMethodDecl)tree;
duke@1: break;
duke@1: case VARIABLE:
duke@1: // System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
duke@1: field = (JCVariableDecl)tree;
duke@1: break;
duke@1: case BLOCK: {
duke@1: // System.err.println("BLOCK: ");
jjg@1090: if (method != null) {
jjg@1090: try {
jjg@1090: Assert.check(method.body == tree);
jjg@1090: method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
jjg@1090: env = memberEnter.getMethodEnv(method, env);
jjg@1090: env = attribStatToTree(method.body, env, copier.leafCopy);
jjg@1090: } finally {
jjg@1090: method.body = (JCBlock) tree;
jjg@1090: }
jjg@1090: } else {
jjg@1090: JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
jjg@1090: env = attribStatToTree(body, env, copier.leafCopy);
jjg@1090: }
duke@1: return env;
duke@1: }
duke@1: default:
duke@1: // System.err.println("DEFAULT: " + tree.getKind());
duke@1: if (field != null && field.getInitializer() == tree) {
duke@1: env = memberEnter.getInitEnv(field, env);
duke@1: JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf());
duke@1: env = attribExprToTree(expr, env, copier.leafCopy);
duke@1: return env;
duke@1: }
duke@1: }
duke@1: }
jjg@1090: return (field != null) ? memberEnter.getInitEnv(field, env) : env;
duke@1: }
duke@1:
duke@1: private Env attribStatToTree(JCTree stat, Envenv, JCTree tree) {
duke@1: JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
duke@1: try {
duke@1: return attr.attribStatToTree(stat, env, tree);
duke@1: } finally {
duke@1: log.useSource(prev);
duke@1: }
duke@1: }
duke@1:
duke@1: private Env attribExprToTree(JCExpression expr, Envenv, JCTree tree) {
duke@1: JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
duke@1: try {
duke@1: return attr.attribExprToTree(expr, env, tree);
duke@1: } finally {
duke@1: log.useSource(prev);
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Makes a copy of a tree, noting the value resulting from copying a particular leaf.
duke@1: **/
ksrini@1327: protected static class Copier extends TreeCopier {
duke@1: JCTree leafCopy = null;
duke@1:
ksrini@1327: protected Copier(TreeMaker M) {
duke@1: super(M);
duke@1: }
duke@1:
jjg@696: @Override
duke@1: public T copy(T t, JCTree leaf) {
duke@1: T t2 = super.copy(t, leaf);
duke@1: if (t == leaf)
duke@1: leafCopy = t2;
duke@1: return t2;
duke@1: }
duke@1: }
jjg@110:
ksrini@1327: protected Copier createCopier(TreeMaker maker) {
ksrini@1327: return new Copier(maker);
ksrini@1327: }
ksrini@1327:
jjg@110: /**
jjg@110: * Gets the original type from the ErrorType object.
jjg@110: * @param errorType The errorType for which we want to get the original type.
jjg@110: * @returns TypeMirror corresponding to the original type, replaced by the ErrorType.
jjg@110: * noType (type.tag == NONE) is returned if there is no original type.
jjg@110: */
jjg@110: public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
jjg@110: if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
jjg@110: return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
jjg@110: }
jjg@110:
jjg@110: return com.sun.tools.javac.code.Type.noType;
jjg@110: }
jjg@308:
jjg@308: /**
jjg@308: * Prints a message of the specified kind at the location of the
jjg@308: * tree within the provided compilation unit
jjg@308: *
jjg@308: * @param kind the kind of message
jjg@308: * @param msg the message, or an empty string if none
jjg@308: * @param t the tree to use as a position hint
jjg@308: * @param root the compilation unit that contains tree
jjg@308: */
jjg@308: public void printMessage(Diagnostic.Kind kind, CharSequence msg,
jjg@308: com.sun.source.tree.Tree t,
jjg@308: com.sun.source.tree.CompilationUnitTree root) {
jjg@1409: printMessage(kind, msg, ((JCTree) t).pos(), root);
jjg@1409: }
jjg@1409:
jjg@1409: public void printMessage(Diagnostic.Kind kind, CharSequence msg,
jjg@1409: com.sun.source.doctree.DocTree t,
jjg@1409: com.sun.source.doctree.DocCommentTree c,
jjg@1409: com.sun.source.tree.CompilationUnitTree root) {
jjg@1409: printMessage(kind, msg, ((DCTree) t).pos((DCDocComment) c), root);
jjg@1409: }
jjg@1409:
jjg@1409: private void printMessage(Diagnostic.Kind kind, CharSequence msg,
jjg@1409: JCDiagnostic.DiagnosticPosition pos,
jjg@1409: com.sun.source.tree.CompilationUnitTree root) {
jjg@308: JavaFileObject oldSource = null;
jjg@308: JavaFileObject newSource = null;
jjg@308:
jjg@308: newSource = root.getSourceFile();
jjg@1409: if (newSource == null) {
jjg@1409: pos = null;
jjg@1409: } else {
jjg@308: oldSource = log.useSource(newSource);
jjg@308: }
jjg@308:
jjg@308: try {
jjg@308: switch (kind) {
jjg@308: case ERROR:
jjg@308: boolean prev = log.multipleErrors;
jjg@308: try {
jjg@308: log.error(pos, "proc.messager", msg.toString());
jjg@308: } finally {
jjg@308: log.multipleErrors = prev;
jjg@308: }
jjg@308: break;
jjg@308:
jjg@308: case WARNING:
jjg@308: log.warning(pos, "proc.messager", msg.toString());
jjg@308: break;
jjg@308:
jjg@308: case MANDATORY_WARNING:
jjg@308: log.mandatoryWarning(pos, "proc.messager", msg.toString());
jjg@308: break;
jjg@308:
jjg@308: default:
jjg@308: log.note(pos, "proc.messager", msg.toString());
jjg@308: }
jjg@308: } finally {
jjg@308: if (oldSource != null)
jjg@308: log.useSource(oldSource);
jjg@308: }
jjg@308: }
jjg@988:
jjg@988: @Override
jjg@988: public TypeMirror getLub(CatchTree tree) {
jjg@988: JCCatch ct = (JCCatch) tree;
jjg@988: JCVariableDecl v = ct.param;
jjg@988: if (v.type != null && v.type.getKind() == TypeKind.UNION) {
jjg@988: UnionClassType ut = (UnionClassType) v.type;
jjg@988: return ut.getLub();
jjg@988: } else {
jjg@988: return v.type;
jjg@988: }
jjg@988: }
duke@1: }