langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java
author jlahoda
Wed, 27 Aug 2014 07:44:00 +0200
changeset 26266 2d24bda701dc
parent 26264 a09fedde76be
child 27224 228abfa87080
permissions -rw-r--r--
8056061: Mark implementations of public interfaces with an annotation Summary: Adding @DefinedBy annotation to mark methods that implement public API methods; annotating the methods; adding a coding rules analyzer to enforce all such methods are annotated. Reviewed-by: jjg, mcimadamore, jfranck Contributed-by: jan.lahoda@oracle.com, jonathan.gibbons@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
23122
02c931d49ad2 6411385: Trees.getPath does not work for constructors
jlahoda
parents: 22165
diff changeset
     2
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.api;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    29
import java.util.HashSet;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    30
import java.util.Set;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
    31
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.annotation.processing.ProcessingEnvironment;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.element.AnnotationMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.element.AnnotationValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.element.Element;
17549
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
    36
import javax.lang.model.element.ElementKind;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.element.ExecutableElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.lang.model.element.TypeElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.lang.model.type.DeclaredType;
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
    40
import javax.lang.model.type.TypeKind;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import javax.lang.model.type.TypeMirror;
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
    42
import javax.tools.Diagnostic;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import javax.tools.JavaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    46
import com.sun.source.doctree.DocCommentTree;
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
    47
import com.sun.source.doctree.DocTree;
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
    48
import com.sun.source.tree.CatchTree;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.source.tree.CompilationUnitTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import com.sun.source.tree.Scope;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.source.tree.Tree;
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
    52
import com.sun.source.util.DocSourcePositions;
17549
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
    53
import com.sun.source.util.DocTreePath;
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
    54
import com.sun.source.util.DocTreeScanner;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    55
import com.sun.source.util.DocTrees;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
    56
import com.sun.source.util.JavacTask;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import com.sun.source.util.TreePath;
6590
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
    58
import com.sun.tools.javac.code.Flags;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    59
import com.sun.tools.javac.code.Kinds;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
    60
import com.sun.tools.javac.code.Symbol;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
import com.sun.tools.javac.code.Symbol.ClassSymbol;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    62
import com.sun.tools.javac.code.Symbol.MethodSymbol;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    63
import com.sun.tools.javac.code.Symbol.PackageSymbol;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
import com.sun.tools.javac.code.Symbol.TypeSymbol;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    65
import com.sun.tools.javac.code.Symbol.VarSymbol;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    66
import com.sun.tools.javac.code.Type;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    67
import com.sun.tools.javac.code.Type.ArrayType;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    68
import com.sun.tools.javac.code.Type.ClassType;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    69
import com.sun.tools.javac.code.Type.ErrorType;
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
    70
import com.sun.tools.javac.code.Type.UnionClassType;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    71
import com.sun.tools.javac.code.Types;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    72
import com.sun.tools.javac.code.Types.TypeRelation;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
import com.sun.tools.javac.comp.Attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
import com.sun.tools.javac.comp.AttrContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
import com.sun.tools.javac.comp.Enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
import com.sun.tools.javac.comp.Env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
import com.sun.tools.javac.comp.MemberEnter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
import com.sun.tools.javac.comp.Resolve;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
import com.sun.tools.javac.model.JavacElements;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 13845
diff changeset
    80
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    81
import com.sun.tools.javac.tree.DCTree;
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
    82
import com.sun.tools.javac.tree.DCTree.DCBlockTag;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    83
import com.sun.tools.javac.tree.DCTree.DCDocComment;
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
    84
import com.sun.tools.javac.tree.DCTree.DCEndPosTree;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
    85
import com.sun.tools.javac.tree.DCTree.DCErroneous;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
    86
import com.sun.tools.javac.tree.DCTree.DCIdentifier;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
    87
import com.sun.tools.javac.tree.DCTree.DCParam;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
    88
import com.sun.tools.javac.tree.DCTree.DCReference;
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
    89
import com.sun.tools.javac.tree.DCTree.DCText;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12016
diff changeset
    90
import com.sun.tools.javac.tree.EndPosTable;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
    91
import com.sun.tools.javac.tree.JCTree;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
import com.sun.tools.javac.tree.TreeCopier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
import com.sun.tools.javac.tree.TreeInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
import com.sun.tools.javac.tree.TreeMaker;
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
    96
import com.sun.tools.javac.util.Abort;
10631
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
    97
import com.sun.tools.javac.util.Assert;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
import com.sun.tools.javac.util.Context;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
    99
import com.sun.tools.javac.util.DefinedBy;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   100
import com.sun.tools.javac.util.DefinedBy.Api;
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   101
import com.sun.tools.javac.util.JCDiagnostic;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
import com.sun.tools.javac.util.List;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   103
import com.sun.tools.javac.util.ListBuffer;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
import com.sun.tools.javac.util.Log;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   105
import com.sun.tools.javac.util.Name;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   106
import com.sun.tools.javac.util.Names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
import com.sun.tools.javac.util.Pair;
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   108
import com.sun.tools.javac.util.Position;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   109
import static com.sun.tools.javac.code.TypeTag.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
 * Provides an implementation of Trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
   114
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
 * @author Peter von der Ah&eacute;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
 */
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   121
public class JavacTrees extends DocTrees {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
6717
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   123
    // in a world of a single context per compilation, these would all be final
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   124
    private Resolve resolve;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   125
    private Enter enter;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   126
    private Log log;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   127
    private MemberEnter memberEnter;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   128
    private Attr attr;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   129
    private TreeMaker treeMaker;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   130
    private JavacElements elements;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   131
    private JavacTaskImpl javacTaskImpl;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   132
    private Names names;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   133
    private Types types;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   135
    // called reflectively from Trees.instance(CompilationTask task)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    public static JavacTrees instance(JavaCompiler.CompilationTask task) {
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14541
diff changeset
   137
        if (!(task instanceof BasicJavacTask))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            throw new IllegalArgumentException();
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14541
diff changeset
   139
        return instance(((BasicJavacTask)task).getContext());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   142
    // called reflectively from Trees.instance(ProcessingEnvironment env)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    public static JavacTrees instance(ProcessingEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        if (!(env instanceof JavacProcessingEnvironment))
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        return instance(((JavacProcessingEnvironment)env).getContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    public static JavacTrees instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        JavacTrees instance = context.get(JavacTrees.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            instance = new JavacTrees(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
13845
bbb35ad7a9c1 7192073: (javac) minor refactoring of tree maker to help IDEs
ksrini
parents: 13077
diff changeset
   156
    protected JavacTrees(Context context) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        context.put(JavacTrees.class, this);
6717
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   158
        init(context);
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   159
    }
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   160
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   161
    public void updateContext(Context context) {
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   162
        init(context);
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   163
    }
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   164
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   165
    private void init(Context context) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        elements = JavacElements.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        resolve = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        treeMaker = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        memberEnter = MemberEnter.instance(context);
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   173
        names = Names.instance(context);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   174
        types = Types.instance(context);
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   175
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   176
        JavacTask t = context.get(JavacTask.class);
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   177
        if (t instanceof JavacTaskImpl)
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   178
            javacTaskImpl = (JavacTaskImpl) t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   181
    @DefinedBy(Api.COMPILER_TREE)
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   182
    public DocSourcePositions getSourcePositions() {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   183
        return new DocSourcePositions() {
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   184
                @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                public long getStartPosition(CompilationUnitTree file, Tree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                    return TreeInfo.getStartPos((JCTree) tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   189
                @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                public long getEndPosition(CompilationUnitTree file, Tree tree) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   191
                    EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   192
                    return TreeInfo.getEndPos((JCTree) tree, endPosTable);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                }
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   194
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   195
                @DefinedBy(Api.COMPILER_TREE)
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   196
                public long getStartPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   197
                    return ((DCTree) tree).getSourcePosition((DCDocComment) comment);
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   198
                }
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   199
                @SuppressWarnings("fallthrough") @DefinedBy(Api.COMPILER_TREE)
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   200
                public long getEndPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   201
                    DCDocComment dcComment = (DCDocComment) comment;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   202
                    if (tree instanceof DCEndPosTree) {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   203
                        int endPos = ((DCEndPosTree) tree).getEndPos(dcComment);
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   204
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   205
                        if (endPos != Position.NOPOS) {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   206
                            return endPos;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   207
                        }
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   208
                    }
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   209
                    int correction = 0;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   210
                    switch (tree.getKind()) {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   211
                        case TEXT:
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   212
                            DCText text = (DCText) tree;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   213
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   214
                            return dcComment.comment.getSourcePos(text.pos + text.text.length());
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   215
                        case ERRONEOUS:
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   216
                            DCErroneous err = (DCErroneous) tree;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   217
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   218
                            return dcComment.comment.getSourcePos(err.pos + err.body.length());
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   219
                        case IDENTIFIER:
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   220
                            DCIdentifier ident = (DCIdentifier) tree;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   221
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   222
                            return dcComment.comment.getSourcePos(ident.pos + (ident.name != names.error ? ident.name.length() : 0));
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   223
                        case PARAM:
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   224
                            DCParam param = (DCParam) tree;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   225
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   226
                            if (param.isTypeParameter && param.getDescription().isEmpty()) {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   227
                                correction = 1;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   228
                            }
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   229
                        case AUTHOR: case DEPRECATED: case RETURN: case SEE:
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   230
                        case SERIAL: case SERIAL_DATA: case SERIAL_FIELD: case SINCE:
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   231
                        case THROWS: case UNKNOWN_BLOCK_TAG: case VERSION: {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   232
                            DocTree last = getLastChild(tree);
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   233
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   234
                            if (last != null) {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   235
                                return getEndPosition(file, comment, last) + correction;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   236
                            }
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   237
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   238
                            DCBlockTag block = (DCBlockTag) tree;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   239
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   240
                            return dcComment.comment.getSourcePos(block.pos + block.getTagName().length() + 1);
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   241
                        }
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   242
                        default:
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   243
                            DocTree last = getLastChild(tree);
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   244
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   245
                            if (last != null) {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   246
                                return getEndPosition(file, comment, last);
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   247
                            }
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   248
                            break;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   249
                    }
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   250
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   251
                    return Position.NOPOS;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   252
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            };
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   256
    private DocTree getLastChild(DocTree tree) {
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   257
        final DocTree[] last = new DocTree[] {null};
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   258
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   259
        tree.accept(new DocTreeScanner<Void, Void>() {
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   260
            @Override @DefinedBy(Api.COMPILER_TREE)
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   261
 public Void scan(DocTree node, Void p) {
17273
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   262
                if (node != null) last[0] = node;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   263
                return null;
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   264
            }
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   265
        }, null);
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   266
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   267
        return last[0];
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   268
    }
d7ec6654aac9 8008174: DocTree API should provide start and end positions for tree nodes
jlahoda
parents: 14952
diff changeset
   269
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   270
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    public JCClassDecl getTree(TypeElement element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        return (JCClassDecl) getTree((Element) element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   275
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    public JCMethodDecl getTree(ExecutableElement method) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
        return (JCMethodDecl) getTree((Element) method);
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   280
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    public JCTree getTree(Element element) {
23122
02c931d49ad2 6411385: Trees.getPath does not work for constructors
jlahoda
parents: 22165
diff changeset
   282
        return getTree(element, null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   285
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    public JCTree getTree(Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        return getTree(e, a, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   290
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
    public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        if (treeTopLevel == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        return treeTopLevel.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   298
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    public TreePath getPath(CompilationUnitTree unit, Tree node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        return TreePath.getPath(unit, node);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   303
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    public TreePath getPath(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        return getPath(e, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   308
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
    public TreePath getPath(Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        return getPath(e, a, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   313
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
    public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
        final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        if (treeTopLevel == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   321
    @DefinedBy(Api.COMPILER_TREE)
17549
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   322
    public Symbol getElement(TreePath path) {
6590
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   323
        JCTree tree = (JCTree) path.getLeaf();
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   324
        Symbol sym = TreeInfo.symbolFor(tree);
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   325
        if (sym == null) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   326
            if (TreeInfo.isDeclaration(tree)) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   327
                for (TreePath p = path; p != null; p = p.getParentPath()) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   328
                    JCTree t = (JCTree) p.getLeaf();
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   329
                    if (t.hasTag(JCTree.Tag.CLASSDEF)) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   330
                        JCClassDecl ct = (JCClassDecl) t;
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   331
                        if (ct.sym != null) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   332
                            if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   333
                                attr.attribClass(ct.pos(), ct.sym);
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   334
                                sym = TreeInfo.symbolFor(tree);
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   335
                            }
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   336
                            break;
6590
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   337
                        }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   338
                    }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   339
                }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   340
            }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   341
        }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   342
        return sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   345
    @Override @DefinedBy(Api.COMPILER_TREE)
17549
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   346
    public Element getElement(DocTreePath path) {
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   347
        DocTree forTree = path.getLeaf();
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   348
        if (forTree instanceof DCReference)
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   349
            return attributeDocReference(path.getTreePath(), ((DCReference) forTree));
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   350
        if (forTree instanceof DCIdentifier) {
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   351
            if (path.getParentPath().getLeaf() instanceof DCParam) {
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   352
                return attributeParamIdentifier(path.getTreePath(), (DCParam) path.getParentPath().getLeaf());
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   353
            }
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   354
        }
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   355
        return null;
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   356
    }
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   357
17549
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   358
    private Symbol attributeDocReference(TreePath path, DCReference ref) {
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   359
        Env<AttrContext> env = getAttrContext(path);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   360
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   361
        Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   362
                new Log.DeferredDiagnosticHandler(log);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   363
        try {
19125
db8cab0ecbd9 8020556: doclint does not check type variables for @throws
jjg
parents: 18646
diff changeset
   364
            final TypeSymbol tsym;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   365
            final Name memberName;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   366
            if (ref.qualifierExpression == null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   367
                tsym = env.enclClass.sym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   368
                memberName = ref.memberName;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   369
            } else {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   370
                // See if the qualifierExpression is a type or package name.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   371
                // javac does not provide the exact method required, so
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   372
                // we first check if qualifierExpression identifies a type,
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   373
                // and if not, then we check to see if it identifies a package.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   374
                Type t = attr.attribType(ref.qualifierExpression, env);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   375
                if (t.isErroneous()) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   376
                    if (ref.memberName == null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   377
                        // Attr/Resolve assume packages exist and create symbols as needed
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   378
                        // so use getPackageElement to restrict search to existing packages
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   379
                        PackageSymbol pck = elements.getPackageElement(ref.qualifierExpression.toString());
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   380
                        if (pck != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   381
                            return pck;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   382
                        } else if (ref.qualifierExpression.hasTag(JCTree.Tag.IDENT)) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   383
                            // fixup:  allow "identifier" instead of "#identifier"
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   384
                            // for compatibility with javadoc
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   385
                            tsym = env.enclClass.sym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   386
                            memberName = ((JCIdent) ref.qualifierExpression).name;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   387
                        } else
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   388
                            return null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   389
                    } else {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   390
                        return null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   391
                    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   392
                } else {
19125
db8cab0ecbd9 8020556: doclint does not check type variables for @throws
jjg
parents: 18646
diff changeset
   393
                    tsym = t.tsym;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   394
                    memberName = ref.memberName;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   395
                }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   396
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   397
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   398
            if (memberName == null)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   399
                return tsym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   400
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   401
            final List<Type> paramTypes;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   402
            if (ref.paramTypes == null)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   403
                paramTypes = null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   404
            else {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19125
diff changeset
   405
                ListBuffer<Type> lb = new ListBuffer<>();
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   406
                for (List<JCTree> l = ref.paramTypes; l.nonEmpty(); l = l.tail) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   407
                    JCTree tree = l.head;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   408
                    Type t = attr.attribType(tree, env);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   409
                    lb.add(t);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   410
                }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   411
                paramTypes = lb.toList();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   412
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   413
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 23122
diff changeset
   414
            ClassSymbol sym = (ClassSymbol) types.cvarUpperBound(tsym.type).tsym;
19125
db8cab0ecbd9 8020556: doclint does not check type variables for @throws
jjg
parents: 18646
diff changeset
   415
db8cab0ecbd9 8020556: doclint does not check type variables for @throws
jjg
parents: 18646
diff changeset
   416
            Symbol msym = (memberName == sym.name)
db8cab0ecbd9 8020556: doclint does not check type variables for @throws
jjg
parents: 18646
diff changeset
   417
                    ? findConstructor(sym, paramTypes)
db8cab0ecbd9 8020556: doclint does not check type variables for @throws
jjg
parents: 18646
diff changeset
   418
                    : findMethod(sym, memberName, paramTypes);
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   419
            if (paramTypes != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   420
                // explicit (possibly empty) arg list given, so cannot be a field
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   421
                return msym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   422
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   423
19125
db8cab0ecbd9 8020556: doclint does not check type variables for @throws
jjg
parents: 18646
diff changeset
   424
            VarSymbol vsym = (ref.paramTypes != null) ? null : findField(sym, memberName);
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   425
            // prefer a field over a method with no parameters
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   426
            if (vsym != null &&
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   427
                    (msym == null ||
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   428
                        types.isSubtypeUnchecked(vsym.enclClass().asType(), msym.enclClass().asType()))) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   429
                return vsym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   430
            } else {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   431
                return msym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   432
            }
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   433
        } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14548
diff changeset
   434
            return null;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   435
        } finally {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   436
            log.popDiagnosticHandler(deferredDiagnosticHandler);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   437
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   438
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   439
17549
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   440
    private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   441
        Symbol javadocSymbol = getElement(path);
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   442
        if (javadocSymbol == null)
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   443
            return null;
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   444
        ElementKind kind = javadocSymbol.getKind();
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   445
        List<? extends Symbol> params = List.nil();
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   446
        if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   447
            MethodSymbol ee = (MethodSymbol) javadocSymbol;
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   448
            params = ptag.isTypeParameter()
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   449
                    ? ee.getTypeParameters()
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   450
                    : ee.getParameters();
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   451
        } else if (kind.isClass() || kind.isInterface()) {
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   452
            ClassSymbol te = (ClassSymbol) javadocSymbol;
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   453
            params = te.getTypeParameters();
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   454
        }
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   455
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   456
        for (Symbol param : params) {
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   457
            if (param.getSimpleName() == ptag.getName().getName()) {
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   458
                return param;
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   459
            }
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   460
        }
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   461
        return null;
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   462
    }
65ea6f3b72d9 8009724: Enhance the DocTree API with DocTreePath
jlahoda
parents: 17273
diff changeset
   463
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   464
    /** @see com.sun.tools.javadoc.ClassDocImpl#findField */
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   465
    private VarSymbol findField(ClassSymbol tsym, Name fieldName) {
22165
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   466
        return searchField(tsym, fieldName, new HashSet<ClassSymbol>());
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   467
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   468
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   469
    /** @see com.sun.tools.javadoc.ClassDocImpl#searchField */
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   470
    private VarSymbol searchField(ClassSymbol tsym, Name fieldName, Set<ClassSymbol> searched) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   471
        if (searched.contains(tsym)) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   472
            return null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   473
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   474
        searched.add(tsym);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   475
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   476
        for (Symbol sym : tsym.members().getSymbolsByName(fieldName)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   477
            if (sym.kind == Kinds.VAR) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   478
                return (VarSymbol)sym;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   479
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   480
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   481
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   482
        //### If we found a VarSymbol above, but which did not pass
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   483
        //### the modifier filter, we should return failure here!
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   484
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   485
        ClassSymbol encl = tsym.owner.enclClass();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   486
        if (encl != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   487
            VarSymbol vsym = searchField(encl, fieldName, searched);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   488
            if (vsym != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   489
                return vsym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   490
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   491
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   492
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   493
        // search superclass
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   494
        Type superclass = tsym.getSuperclass();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   495
        if (superclass.tsym != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   496
            VarSymbol vsym = searchField((ClassSymbol) superclass.tsym, fieldName, searched);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   497
            if (vsym != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   498
                return vsym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   499
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   500
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   501
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   502
        // search interfaces
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   503
        List<Type> intfs = tsym.getInterfaces();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   504
        for (List<Type> l = intfs; l.nonEmpty(); l = l.tail) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   505
            Type intf = l.head;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   506
            if (intf.isErroneous()) continue;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   507
            VarSymbol vsym = searchField((ClassSymbol) intf.tsym, fieldName, searched);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   508
            if (vsym != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   509
                return vsym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   510
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   511
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   512
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   513
        return null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   514
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   515
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   516
    /** @see com.sun.tools.javadoc.ClassDocImpl#findConstructor */
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   517
    MethodSymbol findConstructor(ClassSymbol tsym, List<Type> paramTypes) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   518
        for (Symbol sym : tsym.members().getSymbolsByName(names.init)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   519
            if (sym.kind == Kinds.MTH) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   520
                if (hasParameterTypes((MethodSymbol) sym, paramTypes)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   521
                    return (MethodSymbol) sym;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   522
                }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   523
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   524
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   525
        return null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   526
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   527
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   528
    /** @see com.sun.tools.javadoc.ClassDocImpl#findMethod */
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   529
    private MethodSymbol findMethod(ClassSymbol tsym, Name methodName, List<Type> paramTypes) {
22165
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   530
        return searchMethod(tsym, methodName, paramTypes, new HashSet<ClassSymbol>());
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   531
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   532
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   533
    /** @see com.sun.tools.javadoc.ClassDocImpl#searchMethod */
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   534
    private MethodSymbol searchMethod(ClassSymbol tsym, Name methodName,
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   535
                                       List<Type> paramTypes, Set<ClassSymbol> searched) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   536
        //### Note that this search is not necessarily what the compiler would do!
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   537
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   538
        // do not match constructors
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   539
        if (methodName == names.init)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   540
            return null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   542
        if (searched.contains(tsym))
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   543
            return null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   544
        searched.add(tsym);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   545
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   546
        // search current class
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   547
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   548
        //### Using modifier filter here isn't really correct,
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   549
        //### but emulates the old behavior.  Instead, we should
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   550
        //### apply the normal rules of visibility and inheritance.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   551
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   552
        if (paramTypes == null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   553
            // If no parameters specified, we are allowed to return
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   554
            // any method with a matching name.  In practice, the old
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   555
            // code returned the first method, which is now the last!
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   556
            // In order to provide textually identical results, we
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   557
            // attempt to emulate the old behavior.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   558
            MethodSymbol lastFound = null;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   559
            for (Symbol sym : tsym.members().getSymbolsByName(methodName)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   560
                if (sym.kind == Kinds.MTH) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   561
                    if (sym.name == methodName) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   562
                        lastFound = (MethodSymbol)sym;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   563
                    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   564
                }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   565
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   566
            if (lastFound != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   567
                return lastFound;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   568
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   569
        } else {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   570
            for (Symbol sym : tsym.members().getSymbolsByName(methodName)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   571
                if (sym != null &&
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   572
                    sym.kind == Kinds.MTH) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   573
                    if (hasParameterTypes((MethodSymbol) sym, paramTypes)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   574
                        return (MethodSymbol) sym;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   575
                    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   576
                }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   577
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   578
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   579
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   580
        //### If we found a MethodSymbol above, but which did not pass
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   581
        //### the modifier filter, we should return failure here!
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   582
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   583
        // search superclass
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   584
        Type superclass = tsym.getSuperclass();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   585
        if (superclass.tsym != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   586
            MethodSymbol msym = searchMethod((ClassSymbol) superclass.tsym, methodName, paramTypes, searched);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   587
            if (msym != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   588
                return msym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   589
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   590
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   591
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   592
        // search interfaces
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   593
        List<Type> intfs = tsym.getInterfaces();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   594
        for (List<Type> l = intfs; l.nonEmpty(); l = l.tail) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   595
            Type intf = l.head;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   596
            if (intf.isErroneous()) continue;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   597
            MethodSymbol msym = searchMethod((ClassSymbol) intf.tsym, methodName, paramTypes, searched);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   598
            if (msym != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   599
                return msym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   600
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   601
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   602
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   603
        // search enclosing class
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   604
        ClassSymbol encl = tsym.owner.enclClass();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   605
        if (encl != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   606
            MethodSymbol msym = searchMethod(encl, methodName, paramTypes, searched);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   607
            if (msym != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   608
                return msym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   609
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   610
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   611
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   612
        return null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   613
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   614
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   615
    /** @see com.sun.tools.javadoc.ClassDocImpl */
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   616
    private boolean hasParameterTypes(MethodSymbol method, List<Type> paramTypes) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   617
        if (paramTypes == null)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   618
            return true;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   619
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   620
        if (method.params().size() != paramTypes.size())
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   621
            return false;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   622
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   623
        List<Type> methodParamTypes = types.erasureRecursive(method.asType()).getParameterTypes();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   624
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   625
        return (Type.isErroneous(paramTypes))
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   626
            ? fuzzyMatch(paramTypes, methodParamTypes)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   627
            : types.isSameTypes(paramTypes, methodParamTypes);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   628
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   629
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   630
    boolean fuzzyMatch(List<Type> paramTypes, List<Type> methodParamTypes) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   631
        List<Type> l1 = paramTypes;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   632
        List<Type> l2 = methodParamTypes;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   633
        while (l1.nonEmpty()) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   634
            if (!fuzzyMatch(l1.head, l2.head))
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   635
                return false;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   636
            l1 = l1.tail;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   637
            l2 = l2.tail;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   638
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   639
        return true;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   640
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   641
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   642
    boolean fuzzyMatch(Type paramType, Type methodParamType) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   643
        Boolean b = fuzzyMatcher.visit(paramType, methodParamType);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   644
        return (b == Boolean.TRUE);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   645
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   646
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   647
    TypeRelation fuzzyMatcher = new TypeRelation() {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   648
        @Override
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   649
        public Boolean visitType(Type t, Type s) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   650
            if (t == s)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   651
                return true;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   652
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   653
            if (s.isPartial())
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   654
                return visit(s, t);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   655
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   656
            switch (t.getTag()) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   657
            case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   658
            case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 17557
diff changeset
   659
                return t.hasTag(s.getTag());
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   660
            default:
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   661
                throw new AssertionError("fuzzyMatcher " + t.getTag());
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   662
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   663
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   664
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   665
        @Override
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   666
        public Boolean visitArrayType(ArrayType t, Type s) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   667
            if (t == s)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   668
                return true;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   669
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   670
            if (s.isPartial())
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   671
                return visit(s, t);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   672
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 17557
diff changeset
   673
            return s.hasTag(ARRAY)
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   674
                && visit(t.elemtype, types.elemtype(s));
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   675
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   676
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   677
        @Override
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   678
        public Boolean visitClassType(ClassType t, Type s) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   679
            if (t == s)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   680
                return true;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   681
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   682
            if (s.isPartial())
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   683
                return visit(s, t);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   684
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   685
            return t.tsym == s.tsym;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   686
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   687
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   688
        @Override
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   689
        public Boolean visitErrorType(ErrorType t, Type s) {
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 17557
diff changeset
   690
            return s.hasTag(CLASS)
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   691
                    && t.tsym.name == ((ClassType) s).tsym.name;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   692
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   693
    };
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   694
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   695
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
    public TypeMirror getTypeMirror(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
        Tree t = path.getLeaf();
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
        return ((JCTree)t).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   701
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
    public JavacScope getScope(TreePath path) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25287
diff changeset
   703
        return JavacScope.create(getAttrContext(path));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   706
    @DefinedBy(Api.COMPILER_TREE)
7631
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   707
    public String getDocComment(TreePath path) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   708
        CompilationUnitTree t = path.getCompilationUnit();
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12016
diff changeset
   709
        Tree leaf = path.getLeaf();
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12016
diff changeset
   710
        if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
7631
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   711
            JCCompilationUnit cu = (JCCompilationUnit) t;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   712
            if (cu.docComments != null) {
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12016
diff changeset
   713
                return cu.docComments.getCommentText((JCTree) leaf);
7631
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   714
            }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   715
        }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   716
        return null;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   717
    }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   718
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   719
    @DefinedBy(Api.COMPILER_TREE)
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   720
    public DocCommentTree getDocCommentTree(TreePath path) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   721
        CompilationUnitTree t = path.getCompilationUnit();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   722
        Tree leaf = path.getLeaf();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   723
        if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   724
            JCCompilationUnit cu = (JCCompilationUnit) t;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   725
            if (cu.docComments != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   726
                return cu.docComments.getCommentTree((JCTree) leaf);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   727
            }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   728
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   729
        return null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   730
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   731
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   732
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
    public boolean isAccessible(Scope scope, TypeElement type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
        if (scope instanceof JavacScope && type instanceof ClassSymbol) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
            Env<AttrContext> env = ((JavacScope) scope).env;
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 7077
diff changeset
   736
            return resolve.isAccessible(env, (ClassSymbol)type, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   741
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
    public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
        if (scope instanceof JavacScope
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
                && member instanceof Symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
                && type instanceof com.sun.tools.javac.code.Type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
            Env<AttrContext> env = ((JavacScope) scope).env;
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 7077
diff changeset
   747
            return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
    private Env<AttrContext> getAttrContext(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
        if (!(path.getLeaf() instanceof JCTree))  // implicit null-check
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
10631
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   756
        // if we're being invoked from a Tree API client via parse/enter/analyze,
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   757
        // we need to make sure all the classes have been entered;
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   758
        // if we're being invoked from JSR 199 or JSR 269, then the classes
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   759
        // will already have been entered.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
        if (javacTaskImpl != null) {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   761
            javacTaskImpl.enter(null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
        JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
13845
bbb35ad7a9c1 7192073: (javac) minor refactoring of tree maker to help IDEs
ksrini
parents: 13077
diff changeset
   766
        Copier copier = createCopier(treeMaker.forToplevel(unit));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
        Env<AttrContext> env = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
        JCMethodDecl method = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
        JCVariableDecl field = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
        List<Tree> l = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
        TreePath p = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
        while (p != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
            l = l.prepend(p.getLeaf());
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
            p = p.getParentPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
        for ( ; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
            Tree tree = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
            switch (tree.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
                case COMPILATION_UNIT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
//                    System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
                    env = enter.getTopLevelEnv((JCCompilationUnit)tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
                    break;
7077
55689b828dc5 6993304: JavacTrees.getAttrContext not updated to Tree.Kind.{ANNOTATION_TYPE,ENUM,INTERFACE}
jjg
parents: 6717
diff changeset
   786
                case ANNOTATION_TYPE:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
                case CLASS:
7077
55689b828dc5 6993304: JavacTrees.getAttrContext not updated to Tree.Kind.{ANNOTATION_TYPE,ENUM,INTERFACE}
jjg
parents: 6717
diff changeset
   788
                case ENUM:
6600
b3bb16faccc2 6985181: Annotations lost from classfile
jjg
parents: 6590
diff changeset
   789
                case INTERFACE:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
//                    System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
                    env = enter.getClassEnv(((JCClassDecl)tree).sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
                case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
//                    System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
                    method = (JCMethodDecl)tree;
19125
db8cab0ecbd9 8020556: doclint does not check type variables for @throws
jjg
parents: 18646
diff changeset
   796
                    env = memberEnter.getMethodEnv(method, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
                case VARIABLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
//                    System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
                    field = (JCVariableDecl)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
                case BLOCK: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
//                    System.err.println("BLOCK: ");
10631
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   804
                    if (method != null) {
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   805
                        try {
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   806
                            Assert.check(method.body == tree);
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   807
                            method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   808
                            env = attribStatToTree(method.body, env, copier.leafCopy);
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   809
                        } finally {
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   810
                            method.body = (JCBlock) tree;
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   811
                        }
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   812
                    } else {
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   813
                        JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   814
                        env = attribStatToTree(body, env, copier.leafCopy);
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   815
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
                    return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
//                    System.err.println("DEFAULT: " + tree.getKind());
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
                    if (field != null && field.getInitializer() == tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
                        env = memberEnter.getInitEnv(field, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
                        JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf());
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
                        env = attribExprToTree(expr, env, copier.leafCopy);
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
                        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
        }
10631
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   828
        return (field != null) ? memberEnter.getInitEnv(field, env) : env;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
    private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
            return attr.attribStatToTree(stat, env, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
    private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
            return attr.attribExprToTree(expr, env, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
     * Makes a copy of a tree, noting the value resulting from copying a particular leaf.
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
     **/
13845
bbb35ad7a9c1 7192073: (javac) minor refactoring of tree maker to help IDEs
ksrini
parents: 13077
diff changeset
   852
    protected static class Copier extends TreeCopier<JCTree> {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
        JCTree leafCopy = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
13845
bbb35ad7a9c1 7192073: (javac) minor refactoring of tree maker to help IDEs
ksrini
parents: 13077
diff changeset
   855
        protected Copier(TreeMaker M) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
            super(M);
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
6717
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   859
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
        public <T extends JCTree> T copy(T t, JCTree leaf) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
            T t2 = super.copy(t, leaf);
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
            if (t == leaf)
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
                leafCopy = t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
            return t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
    }
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   867
13845
bbb35ad7a9c1 7192073: (javac) minor refactoring of tree maker to help IDEs
ksrini
parents: 13077
diff changeset
   868
    protected Copier createCopier(TreeMaker maker) {
bbb35ad7a9c1 7192073: (javac) minor refactoring of tree maker to help IDEs
ksrini
parents: 13077
diff changeset
   869
        return new Copier(maker);
bbb35ad7a9c1 7192073: (javac) minor refactoring of tree maker to help IDEs
ksrini
parents: 13077
diff changeset
   870
    }
bbb35ad7a9c1 7192073: (javac) minor refactoring of tree maker to help IDEs
ksrini
parents: 13077
diff changeset
   871
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   872
    /**
25287
d2440361b323 8046614: Fill in missing doc comments
jjg
parents: 24612
diff changeset
   873
     * Returns the original type from the ErrorType object.
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   874
     * @param errorType The errorType for which we want to get the original type.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   875
     * @returns TypeMirror corresponding to the original type, replaced by the ErrorType.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   876
     *          noType (type.tag == NONE) is returned if there is no original type.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   877
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   878
    @DefinedBy(Api.COMPILER_TREE)
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   879
    public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   880
        if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   881
            return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   882
        }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   883
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   884
        return com.sun.tools.javac.code.Type.noType;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   885
    }
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   886
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   887
    /**
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   888
     * Prints a message of the specified kind at the location of the
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   889
     * tree within the provided compilation unit
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   890
     *
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   891
     * @param kind the kind of message
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   892
     * @param msg  the message, or an empty string if none
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   893
     * @param t    the tree to use as a position hint
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   894
     * @param root the compilation unit that contains tree
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   895
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   896
    @DefinedBy(Api.COMPILER_TREE)
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   897
    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   898
            com.sun.source.tree.Tree t,
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   899
            com.sun.source.tree.CompilationUnitTree root) {
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   900
        printMessage(kind, msg, ((JCTree) t).pos(), root);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   901
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   902
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   903
    @DefinedBy(Api.COMPILER_TREE)
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   904
    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   905
            com.sun.source.doctree.DocTree t,
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   906
            com.sun.source.doctree.DocCommentTree c,
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   907
            com.sun.source.tree.CompilationUnitTree root) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   908
        printMessage(kind, msg, ((DCTree) t).pos((DCDocComment) c), root);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   909
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   910
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   911
    private void printMessage(Diagnostic.Kind kind, CharSequence msg,
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   912
            JCDiagnostic.DiagnosticPosition pos,
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   913
            com.sun.source.tree.CompilationUnitTree root) {
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   914
        JavaFileObject oldSource = null;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   915
        JavaFileObject newSource = null;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   916
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   917
        newSource = root.getSourceFile();
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   918
        if (newSource == null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   919
            pos = null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14258
diff changeset
   920
        } else {
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   921
            oldSource = log.useSource(newSource);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   922
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   923
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   924
        try {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   925
            switch (kind) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   926
            case ERROR:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   927
                boolean prev = log.multipleErrors;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   928
                try {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   929
                    log.error(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   930
                } finally {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   931
                    log.multipleErrors = prev;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   932
                }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   933
                break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   934
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   935
            case WARNING:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   936
                log.warning(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   937
                break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   938
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   939
            case MANDATORY_WARNING:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   940
                log.mandatoryWarning(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   941
                break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   942
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   943
            default:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   944
                log.note(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   945
            }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   946
        } finally {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   947
            if (oldSource != null)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   948
                log.useSource(oldSource);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   949
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   950
    }
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   951
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   952
    @Override @DefinedBy(Api.COMPILER_TREE)
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   953
    public TypeMirror getLub(CatchTree tree) {
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   954
        JCCatch ct = (JCCatch) tree;
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   955
        JCVariableDecl v = ct.param;
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   956
        if (v.type != null && v.type.getKind() == TypeKind.UNION) {
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   957
            UnionClassType ut = (UnionClassType) v.type;
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   958
            return ut.getLub();
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   959
        } else {
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   960
            return v.type;
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   961
        }
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   962
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
}