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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,957 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.api;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.util.HashSet;
    1.33 +import java.util.Set;
    1.34 +
    1.35 +import javax.annotation.processing.ProcessingEnvironment;
    1.36 +import javax.lang.model.element.AnnotationMirror;
    1.37 +import javax.lang.model.element.AnnotationValue;
    1.38 +import javax.lang.model.element.Element;
    1.39 +import javax.lang.model.element.ElementKind;
    1.40 +import javax.lang.model.element.ExecutableElement;
    1.41 +import javax.lang.model.element.TypeElement;
    1.42 +import javax.lang.model.type.DeclaredType;
    1.43 +import javax.lang.model.type.TypeKind;
    1.44 +import javax.lang.model.type.TypeMirror;
    1.45 +import javax.tools.Diagnostic;
    1.46 +import javax.tools.JavaCompiler;
    1.47 +import javax.tools.JavaFileObject;
    1.48 +
    1.49 +import com.sun.source.doctree.DocCommentTree;
    1.50 +import com.sun.source.doctree.DocTree;
    1.51 +import com.sun.source.tree.CatchTree;
    1.52 +import com.sun.source.tree.CompilationUnitTree;
    1.53 +import com.sun.source.tree.Scope;
    1.54 +import com.sun.source.tree.Tree;
    1.55 +import com.sun.source.util.DocSourcePositions;
    1.56 +import com.sun.source.util.DocTreePath;
    1.57 +import com.sun.source.util.DocTreeScanner;
    1.58 +import com.sun.source.util.DocTrees;
    1.59 +import com.sun.source.util.JavacTask;
    1.60 +import com.sun.source.util.TreePath;
    1.61 +import com.sun.tools.javac.code.Flags;
    1.62 +import com.sun.tools.javac.code.Kinds;
    1.63 +import com.sun.tools.javac.code.Symbol;
    1.64 +import com.sun.tools.javac.code.Symbol.ClassSymbol;
    1.65 +import com.sun.tools.javac.code.Symbol.MethodSymbol;
    1.66 +import com.sun.tools.javac.code.Symbol.PackageSymbol;
    1.67 +import com.sun.tools.javac.code.Symbol.TypeSymbol;
    1.68 +import com.sun.tools.javac.code.Symbol.VarSymbol;
    1.69 +import com.sun.tools.javac.code.Type;
    1.70 +import com.sun.tools.javac.code.Type.ArrayType;
    1.71 +import com.sun.tools.javac.code.Type.ClassType;
    1.72 +import com.sun.tools.javac.code.Type.ErrorType;
    1.73 +import com.sun.tools.javac.code.Type.UnionClassType;
    1.74 +import com.sun.tools.javac.code.Types;
    1.75 +import com.sun.tools.javac.code.Types.TypeRelation;
    1.76 +import com.sun.tools.javac.comp.Attr;
    1.77 +import com.sun.tools.javac.comp.AttrContext;
    1.78 +import com.sun.tools.javac.comp.Enter;
    1.79 +import com.sun.tools.javac.comp.Env;
    1.80 +import com.sun.tools.javac.comp.MemberEnter;
    1.81 +import com.sun.tools.javac.comp.Resolve;
    1.82 +import com.sun.tools.javac.model.JavacElements;
    1.83 +import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    1.84 +import com.sun.tools.javac.tree.DCTree;
    1.85 +import com.sun.tools.javac.tree.DCTree.DCBlockTag;
    1.86 +import com.sun.tools.javac.tree.DCTree.DCDocComment;
    1.87 +import com.sun.tools.javac.tree.DCTree.DCEndPosTree;
    1.88 +import com.sun.tools.javac.tree.DCTree.DCErroneous;
    1.89 +import com.sun.tools.javac.tree.DCTree.DCIdentifier;
    1.90 +import com.sun.tools.javac.tree.DCTree.DCParam;
    1.91 +import com.sun.tools.javac.tree.DCTree.DCReference;
    1.92 +import com.sun.tools.javac.tree.DCTree.DCText;
    1.93 +import com.sun.tools.javac.tree.EndPosTable;
    1.94 +import com.sun.tools.javac.tree.JCTree;
    1.95 +import com.sun.tools.javac.tree.JCTree.*;
    1.96 +import com.sun.tools.javac.tree.TreeCopier;
    1.97 +import com.sun.tools.javac.tree.TreeInfo;
    1.98 +import com.sun.tools.javac.tree.TreeMaker;
    1.99 +import com.sun.tools.javac.util.Abort;
   1.100 +import com.sun.tools.javac.util.Assert;
   1.101 +import com.sun.tools.javac.util.Context;
   1.102 +import com.sun.tools.javac.util.JCDiagnostic;
   1.103 +import com.sun.tools.javac.util.List;
   1.104 +import com.sun.tools.javac.util.ListBuffer;
   1.105 +import com.sun.tools.javac.util.Log;
   1.106 +import com.sun.tools.javac.util.Name;
   1.107 +import com.sun.tools.javac.util.Names;
   1.108 +import com.sun.tools.javac.util.Pair;
   1.109 +import com.sun.tools.javac.util.Position;
   1.110 +import static com.sun.tools.javac.code.TypeTag.*;
   1.111 +
   1.112 +/**
   1.113 + * Provides an implementation of Trees.
   1.114 + *
   1.115 + * <p><b>This is NOT part of any supported API.
   1.116 + * If you write code that depends on this, you do so at your own
   1.117 + * risk.  This code and its internal interfaces are subject to change
   1.118 + * or deletion without notice.</b></p>
   1.119 + *
   1.120 + * @author Peter von der Ah&eacute;
   1.121 + */
   1.122 +public class JavacTrees extends DocTrees {
   1.123 +
   1.124 +    // in a world of a single context per compilation, these would all be final
   1.125 +    private Resolve resolve;
   1.126 +    private Enter enter;
   1.127 +    private Log log;
   1.128 +    private MemberEnter memberEnter;
   1.129 +    private Attr attr;
   1.130 +    private TreeMaker treeMaker;
   1.131 +    private JavacElements elements;
   1.132 +    private JavacTaskImpl javacTaskImpl;
   1.133 +    private Names names;
   1.134 +    private Types types;
   1.135 +
   1.136 +    // called reflectively from Trees.instance(CompilationTask task)
   1.137 +    public static JavacTrees instance(JavaCompiler.CompilationTask task) {
   1.138 +        if (!(task instanceof BasicJavacTask))
   1.139 +            throw new IllegalArgumentException();
   1.140 +        return instance(((BasicJavacTask)task).getContext());
   1.141 +    }
   1.142 +
   1.143 +    // called reflectively from Trees.instance(ProcessingEnvironment env)
   1.144 +    public static JavacTrees instance(ProcessingEnvironment env) {
   1.145 +        if (!(env instanceof JavacProcessingEnvironment))
   1.146 +            throw new IllegalArgumentException();
   1.147 +        return instance(((JavacProcessingEnvironment)env).getContext());
   1.148 +    }
   1.149 +
   1.150 +    public static JavacTrees instance(Context context) {
   1.151 +        JavacTrees instance = context.get(JavacTrees.class);
   1.152 +        if (instance == null)
   1.153 +            instance = new JavacTrees(context);
   1.154 +        return instance;
   1.155 +    }
   1.156 +
   1.157 +    protected JavacTrees(Context context) {
   1.158 +        context.put(JavacTrees.class, this);
   1.159 +        init(context);
   1.160 +    }
   1.161 +
   1.162 +    public void updateContext(Context context) {
   1.163 +        init(context);
   1.164 +    }
   1.165 +
   1.166 +    private void init(Context context) {
   1.167 +        attr = Attr.instance(context);
   1.168 +        enter = Enter.instance(context);
   1.169 +        elements = JavacElements.instance(context);
   1.170 +        log = Log.instance(context);
   1.171 +        resolve = Resolve.instance(context);
   1.172 +        treeMaker = TreeMaker.instance(context);
   1.173 +        memberEnter = MemberEnter.instance(context);
   1.174 +        names = Names.instance(context);
   1.175 +        types = Types.instance(context);
   1.176 +
   1.177 +        JavacTask t = context.get(JavacTask.class);
   1.178 +        if (t instanceof JavacTaskImpl)
   1.179 +            javacTaskImpl = (JavacTaskImpl) t;
   1.180 +    }
   1.181 +
   1.182 +    public DocSourcePositions getSourcePositions() {
   1.183 +        return new DocSourcePositions() {
   1.184 +                public long getStartPosition(CompilationUnitTree file, Tree tree) {
   1.185 +                    return TreeInfo.getStartPos((JCTree) tree);
   1.186 +                }
   1.187 +
   1.188 +                public long getEndPosition(CompilationUnitTree file, Tree tree) {
   1.189 +                    EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
   1.190 +                    return TreeInfo.getEndPos((JCTree) tree, endPosTable);
   1.191 +                }
   1.192 +
   1.193 +                public long getStartPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
   1.194 +                    return ((DCTree) tree).getSourcePosition((DCDocComment) comment);
   1.195 +                }
   1.196 +                @SuppressWarnings("fallthrough")
   1.197 +                public long getEndPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
   1.198 +                    DCDocComment dcComment = (DCDocComment) comment;
   1.199 +                    if (tree instanceof DCEndPosTree) {
   1.200 +                        int endPos = ((DCEndPosTree) tree).getEndPos(dcComment);
   1.201 +
   1.202 +                        if (endPos != Position.NOPOS) {
   1.203 +                            return endPos;
   1.204 +                        }
   1.205 +                    }
   1.206 +                    int correction = 0;
   1.207 +                    switch (tree.getKind()) {
   1.208 +                        case TEXT:
   1.209 +                            DCText text = (DCText) tree;
   1.210 +
   1.211 +                            return dcComment.comment.getSourcePos(text.pos + text.text.length());
   1.212 +                        case ERRONEOUS:
   1.213 +                            DCErroneous err = (DCErroneous) tree;
   1.214 +
   1.215 +                            return dcComment.comment.getSourcePos(err.pos + err.body.length());
   1.216 +                        case IDENTIFIER:
   1.217 +                            DCIdentifier ident = (DCIdentifier) tree;
   1.218 +
   1.219 +                            return dcComment.comment.getSourcePos(ident.pos + (ident.name != names.error ? ident.name.length() : 0));
   1.220 +                        case PARAM:
   1.221 +                            DCParam param = (DCParam) tree;
   1.222 +
   1.223 +                            if (param.isTypeParameter && param.getDescription().isEmpty()) {
   1.224 +                                correction = 1;
   1.225 +                            }
   1.226 +                        case AUTHOR: case DEPRECATED: case RETURN: case SEE:
   1.227 +                        case SERIAL: case SERIAL_DATA: case SERIAL_FIELD: case SINCE:
   1.228 +                        case THROWS: case UNKNOWN_BLOCK_TAG: case VERSION: {
   1.229 +                            DocTree last = getLastChild(tree);
   1.230 +
   1.231 +                            if (last != null) {
   1.232 +                                return getEndPosition(file, comment, last) + correction;
   1.233 +                            }
   1.234 +
   1.235 +                            DCBlockTag block = (DCBlockTag) tree;
   1.236 +
   1.237 +                            return dcComment.comment.getSourcePos(block.pos + block.getTagName().length() + 1);
   1.238 +                        }
   1.239 +                        default:
   1.240 +                            DocTree last = getLastChild(tree);
   1.241 +
   1.242 +                            if (last != null) {
   1.243 +                                return getEndPosition(file, comment, last);
   1.244 +                            }
   1.245 +                            break;
   1.246 +                    }
   1.247 +
   1.248 +                    return Position.NOPOS;
   1.249 +                }
   1.250 +            };
   1.251 +    }
   1.252 +
   1.253 +    private DocTree getLastChild(DocTree tree) {
   1.254 +        final DocTree[] last = new DocTree[] {null};
   1.255 +
   1.256 +        tree.accept(new DocTreeScanner<Void, Void>() {
   1.257 +            @Override public Void scan(DocTree node, Void p) {
   1.258 +                if (node != null) last[0] = node;
   1.259 +                return null;
   1.260 +            }
   1.261 +        }, null);
   1.262 +
   1.263 +        return last[0];
   1.264 +    }
   1.265 +
   1.266 +    public JCClassDecl getTree(TypeElement element) {
   1.267 +        return (JCClassDecl) getTree((Element) element);
   1.268 +    }
   1.269 +
   1.270 +    public JCMethodDecl getTree(ExecutableElement method) {
   1.271 +        return (JCMethodDecl) getTree((Element) method);
   1.272 +    }
   1.273 +
   1.274 +    public JCTree getTree(Element element) {
   1.275 +        Symbol symbol = (Symbol) element;
   1.276 +        TypeSymbol enclosing = symbol.enclClass();
   1.277 +        Env<AttrContext> env = enter.getEnv(enclosing);
   1.278 +        if (env == null)
   1.279 +            return null;
   1.280 +        JCClassDecl classNode = env.enclClass;
   1.281 +        if (classNode != null) {
   1.282 +            if (TreeInfo.symbolFor(classNode) == element)
   1.283 +                return classNode;
   1.284 +            for (JCTree node : classNode.getMembers())
   1.285 +                if (TreeInfo.symbolFor(node) == element)
   1.286 +                    return node;
   1.287 +        }
   1.288 +        return null;
   1.289 +    }
   1.290 +
   1.291 +    public JCTree getTree(Element e, AnnotationMirror a) {
   1.292 +        return getTree(e, a, null);
   1.293 +    }
   1.294 +
   1.295 +    public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
   1.296 +        Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
   1.297 +        if (treeTopLevel == null)
   1.298 +            return null;
   1.299 +        return treeTopLevel.fst;
   1.300 +    }
   1.301 +
   1.302 +    public TreePath getPath(CompilationUnitTree unit, Tree node) {
   1.303 +        return TreePath.getPath(unit, node);
   1.304 +    }
   1.305 +
   1.306 +    public TreePath getPath(Element e) {
   1.307 +        return getPath(e, null, null);
   1.308 +    }
   1.309 +
   1.310 +    public TreePath getPath(Element e, AnnotationMirror a) {
   1.311 +        return getPath(e, a, null);
   1.312 +    }
   1.313 +
   1.314 +    public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
   1.315 +        final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
   1.316 +        if (treeTopLevel == null)
   1.317 +            return null;
   1.318 +        return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
   1.319 +    }
   1.320 +
   1.321 +    public Symbol getElement(TreePath path) {
   1.322 +        JCTree tree = (JCTree) path.getLeaf();
   1.323 +        Symbol sym = TreeInfo.symbolFor(tree);
   1.324 +        if (sym == null) {
   1.325 +            if (TreeInfo.isDeclaration(tree)) {
   1.326 +                for (TreePath p = path; p != null; p = p.getParentPath()) {
   1.327 +                    JCTree t = (JCTree) p.getLeaf();
   1.328 +                    if (t.hasTag(JCTree.Tag.CLASSDEF)) {
   1.329 +                        JCClassDecl ct = (JCClassDecl) t;
   1.330 +                        if (ct.sym != null) {
   1.331 +                            if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
   1.332 +                                attr.attribClass(ct.pos(), ct.sym);
   1.333 +                                sym = TreeInfo.symbolFor(tree);
   1.334 +                            }
   1.335 +                            break;
   1.336 +                        }
   1.337 +                    }
   1.338 +                }
   1.339 +            }
   1.340 +        }
   1.341 +        return sym;
   1.342 +    }
   1.343 +
   1.344 +    @Override
   1.345 +    public Element getElement(DocTreePath path) {
   1.346 +        DocTree forTree = path.getLeaf();
   1.347 +        if (forTree instanceof DCReference)
   1.348 +            return attributeDocReference(path.getTreePath(), ((DCReference) forTree));
   1.349 +        if (forTree instanceof DCIdentifier) {
   1.350 +            if (path.getParentPath().getLeaf() instanceof DCParam) {
   1.351 +                return attributeParamIdentifier(path.getTreePath(), (DCParam) path.getParentPath().getLeaf());
   1.352 +            }
   1.353 +        }
   1.354 +        return null;
   1.355 +    }
   1.356 +
   1.357 +    private Symbol attributeDocReference(TreePath path, DCReference ref) {
   1.358 +        Env<AttrContext> env = getAttrContext(path);
   1.359 +
   1.360 +        Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
   1.361 +                new Log.DeferredDiagnosticHandler(log);
   1.362 +        try {
   1.363 +            final TypeSymbol tsym;
   1.364 +            final Name memberName;
   1.365 +            if (ref.qualifierExpression == null) {
   1.366 +                tsym = env.enclClass.sym;
   1.367 +                memberName = ref.memberName;
   1.368 +            } else {
   1.369 +                // See if the qualifierExpression is a type or package name.
   1.370 +                // javac does not provide the exact method required, so
   1.371 +                // we first check if qualifierExpression identifies a type,
   1.372 +                // and if not, then we check to see if it identifies a package.
   1.373 +                Type t = attr.attribType(ref.qualifierExpression, env);
   1.374 +                if (t.isErroneous()) {
   1.375 +                    if (ref.memberName == null) {
   1.376 +                        // Attr/Resolve assume packages exist and create symbols as needed
   1.377 +                        // so use getPackageElement to restrict search to existing packages
   1.378 +                        PackageSymbol pck = elements.getPackageElement(ref.qualifierExpression.toString());
   1.379 +                        if (pck != null) {
   1.380 +                            return pck;
   1.381 +                        } else if (ref.qualifierExpression.hasTag(JCTree.Tag.IDENT)) {
   1.382 +                            // fixup:  allow "identifier" instead of "#identifier"
   1.383 +                            // for compatibility with javadoc
   1.384 +                            tsym = env.enclClass.sym;
   1.385 +                            memberName = ((JCIdent) ref.qualifierExpression).name;
   1.386 +                        } else
   1.387 +                            return null;
   1.388 +                    } else {
   1.389 +                        return null;
   1.390 +                    }
   1.391 +                } else {
   1.392 +                    tsym = t.tsym;
   1.393 +                    memberName = ref.memberName;
   1.394 +                }
   1.395 +            }
   1.396 +
   1.397 +            if (memberName == null)
   1.398 +                return tsym;
   1.399 +
   1.400 +            final List<Type> paramTypes;
   1.401 +            if (ref.paramTypes == null)
   1.402 +                paramTypes = null;
   1.403 +            else {
   1.404 +                ListBuffer<Type> lb = new ListBuffer<Type>();
   1.405 +                for (List<JCTree> l = ref.paramTypes; l.nonEmpty(); l = l.tail) {
   1.406 +                    JCTree tree = l.head;
   1.407 +                    Type t = attr.attribType(tree, env);
   1.408 +                    lb.add(t);
   1.409 +                }
   1.410 +                paramTypes = lb.toList();
   1.411 +            }
   1.412 +
   1.413 +            ClassSymbol sym = (ClassSymbol) types.cvarUpperBound(tsym.type).tsym;
   1.414 +
   1.415 +            Symbol msym = (memberName == sym.name)
   1.416 +                    ? findConstructor(sym, paramTypes)
   1.417 +                    : findMethod(sym, memberName, paramTypes);
   1.418 +            if (paramTypes != null) {
   1.419 +                // explicit (possibly empty) arg list given, so cannot be a field
   1.420 +                return msym;
   1.421 +            }
   1.422 +
   1.423 +            VarSymbol vsym = (ref.paramTypes != null) ? null : findField(sym, memberName);
   1.424 +            // prefer a field over a method with no parameters
   1.425 +            if (vsym != null &&
   1.426 +                    (msym == null ||
   1.427 +                        types.isSubtypeUnchecked(vsym.enclClass().asType(), msym.enclClass().asType()))) {
   1.428 +                return vsym;
   1.429 +            } else {
   1.430 +                return msym;
   1.431 +            }
   1.432 +        } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file
   1.433 +            return null;
   1.434 +        } finally {
   1.435 +            log.popDiagnosticHandler(deferredDiagnosticHandler);
   1.436 +        }
   1.437 +    }
   1.438 +
   1.439 +    private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
   1.440 +        Symbol javadocSymbol = getElement(path);
   1.441 +        if (javadocSymbol == null)
   1.442 +            return null;
   1.443 +        ElementKind kind = javadocSymbol.getKind();
   1.444 +        List<? extends Symbol> params = List.nil();
   1.445 +        if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
   1.446 +            MethodSymbol ee = (MethodSymbol) javadocSymbol;
   1.447 +            params = ptag.isTypeParameter()
   1.448 +                    ? ee.getTypeParameters()
   1.449 +                    : ee.getParameters();
   1.450 +        } else if (kind.isClass() || kind.isInterface()) {
   1.451 +            ClassSymbol te = (ClassSymbol) javadocSymbol;
   1.452 +            params = te.getTypeParameters();
   1.453 +        }
   1.454 +
   1.455 +        for (Symbol param : params) {
   1.456 +            if (param.getSimpleName() == ptag.getName().getName()) {
   1.457 +                return param;
   1.458 +            }
   1.459 +        }
   1.460 +        return null;
   1.461 +    }
   1.462 +
   1.463 +    /** @see com.sun.tools.javadoc.ClassDocImpl#findField */
   1.464 +    private VarSymbol findField(ClassSymbol tsym, Name fieldName) {
   1.465 +        return searchField(tsym, fieldName, new HashSet<ClassSymbol>());
   1.466 +    }
   1.467 +
   1.468 +    /** @see com.sun.tools.javadoc.ClassDocImpl#searchField */
   1.469 +    private VarSymbol searchField(ClassSymbol tsym, Name fieldName, Set<ClassSymbol> searched) {
   1.470 +        if (searched.contains(tsym)) {
   1.471 +            return null;
   1.472 +        }
   1.473 +        searched.add(tsym);
   1.474 +
   1.475 +        for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(fieldName);
   1.476 +                e.scope != null; e = e.next()) {
   1.477 +            if (e.sym.kind == Kinds.VAR) {
   1.478 +                return (VarSymbol)e.sym;
   1.479 +            }
   1.480 +        }
   1.481 +
   1.482 +        //### If we found a VarSymbol above, but which did not pass
   1.483 +        //### the modifier filter, we should return failure here!
   1.484 +
   1.485 +        ClassSymbol encl = tsym.owner.enclClass();
   1.486 +        if (encl != null) {
   1.487 +            VarSymbol vsym = searchField(encl, fieldName, searched);
   1.488 +            if (vsym != null) {
   1.489 +                return vsym;
   1.490 +            }
   1.491 +        }
   1.492 +
   1.493 +        // search superclass
   1.494 +        Type superclass = tsym.getSuperclass();
   1.495 +        if (superclass.tsym != null) {
   1.496 +            VarSymbol vsym = searchField((ClassSymbol) superclass.tsym, fieldName, searched);
   1.497 +            if (vsym != null) {
   1.498 +                return vsym;
   1.499 +            }
   1.500 +        }
   1.501 +
   1.502 +        // search interfaces
   1.503 +        List<Type> intfs = tsym.getInterfaces();
   1.504 +        for (List<Type> l = intfs; l.nonEmpty(); l = l.tail) {
   1.505 +            Type intf = l.head;
   1.506 +            if (intf.isErroneous()) continue;
   1.507 +            VarSymbol vsym = searchField((ClassSymbol) intf.tsym, fieldName, searched);
   1.508 +            if (vsym != null) {
   1.509 +                return vsym;
   1.510 +            }
   1.511 +        }
   1.512 +
   1.513 +        return null;
   1.514 +    }
   1.515 +
   1.516 +    /** @see com.sun.tools.javadoc.ClassDocImpl#findConstructor */
   1.517 +    MethodSymbol findConstructor(ClassSymbol tsym, List<Type> paramTypes) {
   1.518 +        for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(names.init);
   1.519 +                e.scope != null; e = e.next()) {
   1.520 +            if (e.sym.kind == Kinds.MTH) {
   1.521 +                if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) {
   1.522 +                    return (MethodSymbol) e.sym;
   1.523 +                }
   1.524 +            }
   1.525 +        }
   1.526 +        return null;
   1.527 +    }
   1.528 +
   1.529 +    /** @see com.sun.tools.javadoc.ClassDocImpl#findMethod */
   1.530 +    private MethodSymbol findMethod(ClassSymbol tsym, Name methodName, List<Type> paramTypes) {
   1.531 +        return searchMethod(tsym, methodName, paramTypes, new HashSet<ClassSymbol>());
   1.532 +    }
   1.533 +
   1.534 +    /** @see com.sun.tools.javadoc.ClassDocImpl#searchMethod */
   1.535 +    private MethodSymbol searchMethod(ClassSymbol tsym, Name methodName,
   1.536 +                                       List<Type> paramTypes, Set<ClassSymbol> searched) {
   1.537 +        //### Note that this search is not necessarily what the compiler would do!
   1.538 +
   1.539 +        // do not match constructors
   1.540 +        if (methodName == names.init)
   1.541 +            return null;
   1.542 +
   1.543 +        if (searched.contains(tsym))
   1.544 +            return null;
   1.545 +        searched.add(tsym);
   1.546 +
   1.547 +        // search current class
   1.548 +        com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(methodName);
   1.549 +
   1.550 +        //### Using modifier filter here isn't really correct,
   1.551 +        //### but emulates the old behavior.  Instead, we should
   1.552 +        //### apply the normal rules of visibility and inheritance.
   1.553 +
   1.554 +        if (paramTypes == null) {
   1.555 +            // If no parameters specified, we are allowed to return
   1.556 +            // any method with a matching name.  In practice, the old
   1.557 +            // code returned the first method, which is now the last!
   1.558 +            // In order to provide textually identical results, we
   1.559 +            // attempt to emulate the old behavior.
   1.560 +            MethodSymbol lastFound = null;
   1.561 +            for (; e.scope != null; e = e.next()) {
   1.562 +                if (e.sym.kind == Kinds.MTH) {
   1.563 +                    if (e.sym.name == methodName) {
   1.564 +                        lastFound = (MethodSymbol)e.sym;
   1.565 +                    }
   1.566 +                }
   1.567 +            }
   1.568 +            if (lastFound != null) {
   1.569 +                return lastFound;
   1.570 +            }
   1.571 +        } else {
   1.572 +            for (; e.scope != null; e = e.next()) {
   1.573 +                if (e.sym != null &&
   1.574 +                    e.sym.kind == Kinds.MTH) {
   1.575 +                    if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) {
   1.576 +                        return (MethodSymbol) e.sym;
   1.577 +                    }
   1.578 +                }
   1.579 +            }
   1.580 +        }
   1.581 +
   1.582 +        //### If we found a MethodSymbol above, but which did not pass
   1.583 +        //### the modifier filter, we should return failure here!
   1.584 +
   1.585 +        // search superclass
   1.586 +        Type superclass = tsym.getSuperclass();
   1.587 +        if (superclass.tsym != null) {
   1.588 +            MethodSymbol msym = searchMethod((ClassSymbol) superclass.tsym, methodName, paramTypes, searched);
   1.589 +            if (msym != null) {
   1.590 +                return msym;
   1.591 +            }
   1.592 +        }
   1.593 +
   1.594 +        // search interfaces
   1.595 +        List<Type> intfs = tsym.getInterfaces();
   1.596 +        for (List<Type> l = intfs; l.nonEmpty(); l = l.tail) {
   1.597 +            Type intf = l.head;
   1.598 +            if (intf.isErroneous()) continue;
   1.599 +            MethodSymbol msym = searchMethod((ClassSymbol) intf.tsym, methodName, paramTypes, searched);
   1.600 +            if (msym != null) {
   1.601 +                return msym;
   1.602 +            }
   1.603 +        }
   1.604 +
   1.605 +        // search enclosing class
   1.606 +        ClassSymbol encl = tsym.owner.enclClass();
   1.607 +        if (encl != null) {
   1.608 +            MethodSymbol msym = searchMethod(encl, methodName, paramTypes, searched);
   1.609 +            if (msym != null) {
   1.610 +                return msym;
   1.611 +            }
   1.612 +        }
   1.613 +
   1.614 +        return null;
   1.615 +    }
   1.616 +
   1.617 +    /** @see com.sun.tools.javadoc.ClassDocImpl */
   1.618 +    private boolean hasParameterTypes(MethodSymbol method, List<Type> paramTypes) {
   1.619 +        if (paramTypes == null)
   1.620 +            return true;
   1.621 +
   1.622 +        if (method.params().size() != paramTypes.size())
   1.623 +            return false;
   1.624 +
   1.625 +        List<Type> methodParamTypes = types.erasureRecursive(method.asType()).getParameterTypes();
   1.626 +
   1.627 +        return (Type.isErroneous(paramTypes))
   1.628 +            ? fuzzyMatch(paramTypes, methodParamTypes)
   1.629 +            : types.isSameTypes(paramTypes, methodParamTypes);
   1.630 +    }
   1.631 +
   1.632 +    boolean fuzzyMatch(List<Type> paramTypes, List<Type> methodParamTypes) {
   1.633 +        List<Type> l1 = paramTypes;
   1.634 +        List<Type> l2 = methodParamTypes;
   1.635 +        while (l1.nonEmpty()) {
   1.636 +            if (!fuzzyMatch(l1.head, l2.head))
   1.637 +                return false;
   1.638 +            l1 = l1.tail;
   1.639 +            l2 = l2.tail;
   1.640 +        }
   1.641 +        return true;
   1.642 +    }
   1.643 +
   1.644 +    boolean fuzzyMatch(Type paramType, Type methodParamType) {
   1.645 +        Boolean b = fuzzyMatcher.visit(paramType, methodParamType);
   1.646 +        return (b == Boolean.TRUE);
   1.647 +    }
   1.648 +
   1.649 +    TypeRelation fuzzyMatcher = new TypeRelation() {
   1.650 +        @Override
   1.651 +        public Boolean visitType(Type t, Type s) {
   1.652 +            if (t == s)
   1.653 +                return true;
   1.654 +
   1.655 +            if (s.isPartial())
   1.656 +                return visit(s, t);
   1.657 +
   1.658 +            switch (t.getTag()) {
   1.659 +            case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
   1.660 +            case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
   1.661 +                return t.hasTag(s.getTag());
   1.662 +            default:
   1.663 +                throw new AssertionError("fuzzyMatcher " + t.getTag());
   1.664 +            }
   1.665 +        }
   1.666 +
   1.667 +        @Override
   1.668 +        public Boolean visitArrayType(ArrayType t, Type s) {
   1.669 +            if (t == s)
   1.670 +                return true;
   1.671 +
   1.672 +            if (s.isPartial())
   1.673 +                return visit(s, t);
   1.674 +
   1.675 +            return s.hasTag(ARRAY)
   1.676 +                && visit(t.elemtype, types.elemtype(s));
   1.677 +        }
   1.678 +
   1.679 +        @Override
   1.680 +        public Boolean visitClassType(ClassType t, Type s) {
   1.681 +            if (t == s)
   1.682 +                return true;
   1.683 +
   1.684 +            if (s.isPartial())
   1.685 +                return visit(s, t);
   1.686 +
   1.687 +            return t.tsym == s.tsym;
   1.688 +        }
   1.689 +
   1.690 +        @Override
   1.691 +        public Boolean visitErrorType(ErrorType t, Type s) {
   1.692 +            return s.hasTag(CLASS)
   1.693 +                    && t.tsym.name == ((ClassType) s).tsym.name;
   1.694 +        }
   1.695 +    };
   1.696 +
   1.697 +    public TypeMirror getTypeMirror(TreePath path) {
   1.698 +        Tree t = path.getLeaf();
   1.699 +        return ((JCTree)t).type;
   1.700 +    }
   1.701 +
   1.702 +    public JavacScope getScope(TreePath path) {
   1.703 +        return new JavacScope(getAttrContext(path));
   1.704 +    }
   1.705 +
   1.706 +    public String getDocComment(TreePath path) {
   1.707 +        CompilationUnitTree t = path.getCompilationUnit();
   1.708 +        Tree leaf = path.getLeaf();
   1.709 +        if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
   1.710 +            JCCompilationUnit cu = (JCCompilationUnit) t;
   1.711 +            if (cu.docComments != null) {
   1.712 +                return cu.docComments.getCommentText((JCTree) leaf);
   1.713 +            }
   1.714 +        }
   1.715 +        return null;
   1.716 +    }
   1.717 +
   1.718 +    public DocCommentTree getDocCommentTree(TreePath path) {
   1.719 +        CompilationUnitTree t = path.getCompilationUnit();
   1.720 +        Tree leaf = path.getLeaf();
   1.721 +        if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
   1.722 +            JCCompilationUnit cu = (JCCompilationUnit) t;
   1.723 +            if (cu.docComments != null) {
   1.724 +                return cu.docComments.getCommentTree((JCTree) leaf);
   1.725 +            }
   1.726 +        }
   1.727 +        return null;
   1.728 +    }
   1.729 +
   1.730 +    public boolean isAccessible(Scope scope, TypeElement type) {
   1.731 +        if (scope instanceof JavacScope && type instanceof ClassSymbol) {
   1.732 +            Env<AttrContext> env = ((JavacScope) scope).env;
   1.733 +            return resolve.isAccessible(env, (ClassSymbol)type, true);
   1.734 +        } else
   1.735 +            return false;
   1.736 +    }
   1.737 +
   1.738 +    public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
   1.739 +        if (scope instanceof JavacScope
   1.740 +                && member instanceof Symbol
   1.741 +                && type instanceof com.sun.tools.javac.code.Type) {
   1.742 +            Env<AttrContext> env = ((JavacScope) scope).env;
   1.743 +            return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
   1.744 +        } else
   1.745 +            return false;
   1.746 +    }
   1.747 +
   1.748 +    private Env<AttrContext> getAttrContext(TreePath path) {
   1.749 +        if (!(path.getLeaf() instanceof JCTree))  // implicit null-check
   1.750 +            throw new IllegalArgumentException();
   1.751 +
   1.752 +        // if we're being invoked from a Tree API client via parse/enter/analyze,
   1.753 +        // we need to make sure all the classes have been entered;
   1.754 +        // if we're being invoked from JSR 199 or JSR 269, then the classes
   1.755 +        // will already have been entered.
   1.756 +        if (javacTaskImpl != null) {
   1.757 +            try {
   1.758 +                javacTaskImpl.enter(null);
   1.759 +            } catch (IOException e) {
   1.760 +                throw new Error("unexpected error while entering symbols: " + e);
   1.761 +            }
   1.762 +        }
   1.763 +
   1.764 +
   1.765 +        JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
   1.766 +        Copier copier = createCopier(treeMaker.forToplevel(unit));
   1.767 +
   1.768 +        Env<AttrContext> env = null;
   1.769 +        JCMethodDecl method = null;
   1.770 +        JCVariableDecl field = null;
   1.771 +
   1.772 +        List<Tree> l = List.nil();
   1.773 +        TreePath p = path;
   1.774 +        while (p != null) {
   1.775 +            l = l.prepend(p.getLeaf());
   1.776 +            p = p.getParentPath();
   1.777 +        }
   1.778 +
   1.779 +        for ( ; l.nonEmpty(); l = l.tail) {
   1.780 +            Tree tree = l.head;
   1.781 +            switch (tree.getKind()) {
   1.782 +                case COMPILATION_UNIT:
   1.783 +//                    System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
   1.784 +                    env = enter.getTopLevelEnv((JCCompilationUnit)tree);
   1.785 +                    break;
   1.786 +                case ANNOTATION_TYPE:
   1.787 +                case CLASS:
   1.788 +                case ENUM:
   1.789 +                case INTERFACE:
   1.790 +//                    System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
   1.791 +                    env = enter.getClassEnv(((JCClassDecl)tree).sym);
   1.792 +                    break;
   1.793 +                case METHOD:
   1.794 +//                    System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
   1.795 +                    method = (JCMethodDecl)tree;
   1.796 +                    env = memberEnter.getMethodEnv(method, env);
   1.797 +                    break;
   1.798 +                case VARIABLE:
   1.799 +//                    System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
   1.800 +                    field = (JCVariableDecl)tree;
   1.801 +                    break;
   1.802 +                case BLOCK: {
   1.803 +//                    System.err.println("BLOCK: ");
   1.804 +                    if (method != null) {
   1.805 +                        try {
   1.806 +                            Assert.check(method.body == tree);
   1.807 +                            method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
   1.808 +                            env = attribStatToTree(method.body, env, copier.leafCopy);
   1.809 +                        } finally {
   1.810 +                            method.body = (JCBlock) tree;
   1.811 +                        }
   1.812 +                    } else {
   1.813 +                        JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
   1.814 +                        env = attribStatToTree(body, env, copier.leafCopy);
   1.815 +                    }
   1.816 +                    return env;
   1.817 +                }
   1.818 +                default:
   1.819 +//                    System.err.println("DEFAULT: " + tree.getKind());
   1.820 +                    if (field != null && field.getInitializer() == tree) {
   1.821 +                        env = memberEnter.getInitEnv(field, env);
   1.822 +                        JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf());
   1.823 +                        env = attribExprToTree(expr, env, copier.leafCopy);
   1.824 +                        return env;
   1.825 +                    }
   1.826 +            }
   1.827 +        }
   1.828 +        return (field != null) ? memberEnter.getInitEnv(field, env) : env;
   1.829 +    }
   1.830 +
   1.831 +    private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
   1.832 +        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
   1.833 +        try {
   1.834 +            return attr.attribStatToTree(stat, env, tree);
   1.835 +        } finally {
   1.836 +            log.useSource(prev);
   1.837 +        }
   1.838 +    }
   1.839 +
   1.840 +    private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
   1.841 +        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
   1.842 +        try {
   1.843 +            return attr.attribExprToTree(expr, env, tree);
   1.844 +        } finally {
   1.845 +            log.useSource(prev);
   1.846 +        }
   1.847 +    }
   1.848 +
   1.849 +    /**
   1.850 +     * Makes a copy of a tree, noting the value resulting from copying a particular leaf.
   1.851 +     **/
   1.852 +    protected static class Copier extends TreeCopier<JCTree> {
   1.853 +        JCTree leafCopy = null;
   1.854 +
   1.855 +        protected Copier(TreeMaker M) {
   1.856 +            super(M);
   1.857 +        }
   1.858 +
   1.859 +        @Override
   1.860 +        public <T extends JCTree> T copy(T t, JCTree leaf) {
   1.861 +            T t2 = super.copy(t, leaf);
   1.862 +            if (t == leaf)
   1.863 +                leafCopy = t2;
   1.864 +            return t2;
   1.865 +        }
   1.866 +    }
   1.867 +
   1.868 +    protected Copier createCopier(TreeMaker maker) {
   1.869 +        return new Copier(maker);
   1.870 +    }
   1.871 +
   1.872 +    /**
   1.873 +     * Gets the original type from the ErrorType object.
   1.874 +     * @param errorType The errorType for which we want to get the original type.
   1.875 +     * @returns TypeMirror corresponding to the original type, replaced by the ErrorType.
   1.876 +     *          noType (type.tag == NONE) is returned if there is no original type.
   1.877 +     */
   1.878 +    public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
   1.879 +        if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
   1.880 +            return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
   1.881 +        }
   1.882 +
   1.883 +        return com.sun.tools.javac.code.Type.noType;
   1.884 +    }
   1.885 +
   1.886 +    /**
   1.887 +     * Prints a message of the specified kind at the location of the
   1.888 +     * tree within the provided compilation unit
   1.889 +     *
   1.890 +     * @param kind the kind of message
   1.891 +     * @param msg  the message, or an empty string if none
   1.892 +     * @param t    the tree to use as a position hint
   1.893 +     * @param root the compilation unit that contains tree
   1.894 +     */
   1.895 +    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
   1.896 +            com.sun.source.tree.Tree t,
   1.897 +            com.sun.source.tree.CompilationUnitTree root) {
   1.898 +        printMessage(kind, msg, ((JCTree) t).pos(), root);
   1.899 +    }
   1.900 +
   1.901 +    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
   1.902 +            com.sun.source.doctree.DocTree t,
   1.903 +            com.sun.source.doctree.DocCommentTree c,
   1.904 +            com.sun.source.tree.CompilationUnitTree root) {
   1.905 +        printMessage(kind, msg, ((DCTree) t).pos((DCDocComment) c), root);
   1.906 +    }
   1.907 +
   1.908 +    private void printMessage(Diagnostic.Kind kind, CharSequence msg,
   1.909 +            JCDiagnostic.DiagnosticPosition pos,
   1.910 +            com.sun.source.tree.CompilationUnitTree root) {
   1.911 +        JavaFileObject oldSource = null;
   1.912 +        JavaFileObject newSource = null;
   1.913 +
   1.914 +        newSource = root.getSourceFile();
   1.915 +        if (newSource == null) {
   1.916 +            pos = null;
   1.917 +        } else {
   1.918 +            oldSource = log.useSource(newSource);
   1.919 +        }
   1.920 +
   1.921 +        try {
   1.922 +            switch (kind) {
   1.923 +            case ERROR:
   1.924 +                boolean prev = log.multipleErrors;
   1.925 +                try {
   1.926 +                    log.error(pos, "proc.messager", msg.toString());
   1.927 +                } finally {
   1.928 +                    log.multipleErrors = prev;
   1.929 +                }
   1.930 +                break;
   1.931 +
   1.932 +            case WARNING:
   1.933 +                log.warning(pos, "proc.messager", msg.toString());
   1.934 +                break;
   1.935 +
   1.936 +            case MANDATORY_WARNING:
   1.937 +                log.mandatoryWarning(pos, "proc.messager", msg.toString());
   1.938 +                break;
   1.939 +
   1.940 +            default:
   1.941 +                log.note(pos, "proc.messager", msg.toString());
   1.942 +            }
   1.943 +        } finally {
   1.944 +            if (oldSource != null)
   1.945 +                log.useSource(oldSource);
   1.946 +        }
   1.947 +    }
   1.948 +
   1.949 +    @Override
   1.950 +    public TypeMirror getLub(CatchTree tree) {
   1.951 +        JCCatch ct = (JCCatch) tree;
   1.952 +        JCVariableDecl v = ct.param;
   1.953 +        if (v.type != null && v.type.getKind() == TypeKind.UNION) {
   1.954 +            UnionClassType ut = (UnionClassType) v.type;
   1.955 +            return ut.getLub();
   1.956 +        } else {
   1.957 +            return v.type;
   1.958 +        }
   1.959 +    }
   1.960 +}

mercurial