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