langtools/src/jdk.compiler/share/classes/jdk/internal/shellsupport/doc/JavadocHelper.java
author mcimadamore
Fri, 16 Dec 2016 15:27:34 +0000
changeset 42827 36468b5fa7f4
parent 41865 3ef02797070d
child 43773 8d8593871575
permissions -rw-r--r--
8181370: Convert anonymous inner classes into lambdas/method references Reviewed-by: jjg, rfield, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     1
/*
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     4
 *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    10
 *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    15
 * accompanied this code).
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    16
 *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    20
 *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    23
 * questions.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    24
 */
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    25
package jdk.internal.shellsupport.doc;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    26
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    27
import java.io.IOException;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    28
import java.net.URI;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    29
import java.net.URISyntaxException;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    30
import java.nio.file.Path;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    31
import java.util.ArrayList;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    32
import java.util.Arrays;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    33
import java.util.Collection;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    34
import java.util.Comparator;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    35
import java.util.HashMap;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    36
import java.util.HashSet;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    37
import java.util.IdentityHashMap;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    38
import java.util.Iterator;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    39
import java.util.List;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    40
import java.util.Map;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    41
import java.util.Map.Entry;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    42
import java.util.Objects;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    43
import java.util.Set;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    44
import java.util.Stack;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    45
import java.util.TreeMap;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    46
import java.util.stream.Collectors;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    47
import java.util.stream.Stream;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    48
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    49
import javax.lang.model.element.Element;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    50
import javax.lang.model.element.ElementKind;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    51
import javax.lang.model.element.ExecutableElement;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    52
import javax.lang.model.element.TypeElement;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    53
import javax.lang.model.element.VariableElement;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    54
import javax.lang.model.type.DeclaredType;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    55
import javax.lang.model.type.TypeKind;
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 41865
diff changeset
    56
import javax.lang.model.type.TypeMirror;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    57
import javax.lang.model.util.ElementFilter;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    58
import javax.tools.JavaCompiler;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    59
import javax.tools.JavaFileManager;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    60
import javax.tools.JavaFileObject;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    61
import javax.tools.SimpleJavaFileObject;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    62
import javax.tools.StandardJavaFileManager;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    63
import javax.tools.StandardLocation;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    64
import javax.tools.ToolProvider;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    65
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    66
import com.sun.source.doctree.DocCommentTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    67
import com.sun.source.doctree.DocTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    68
import com.sun.source.doctree.InheritDocTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    69
import com.sun.source.doctree.ParamTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    70
import com.sun.source.doctree.ReturnTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    71
import com.sun.source.doctree.ThrowsTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    72
import com.sun.source.tree.ClassTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    73
import com.sun.source.tree.CompilationUnitTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    74
import com.sun.source.tree.MethodTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    75
import com.sun.source.tree.VariableTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    76
import com.sun.source.util.DocTreePath;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    77
import com.sun.source.util.DocTreeScanner;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    78
import com.sun.source.util.DocTrees;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    79
import com.sun.source.util.JavacTask;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    80
import com.sun.source.util.TreePath;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    81
import com.sun.source.util.TreePathScanner;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    82
import com.sun.source.util.Trees;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    83
import com.sun.tools.javac.api.JavacTaskImpl;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    84
import com.sun.tools.javac.util.DefinedBy;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    85
import com.sun.tools.javac.util.DefinedBy.Api;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    86
import com.sun.tools.javac.util.Pair;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    87
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    88
/**Helper to find javadoc and resolve @inheritDoc.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    89
 */
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    90
public abstract class JavadocHelper implements AutoCloseable {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    91
    private static final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    92
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    93
    /**Create the helper.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    94
     *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    95
     * @param mainTask JavacTask from which the further Elements originate
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    96
     * @param sourceLocations paths where source files should be searched
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    97
     * @return a JavadocHelper
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    98
     */
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
    99
    public static JavadocHelper create(JavacTask mainTask, Collection<? extends Path> sourceLocations) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   100
        StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   101
        try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   102
            fm.setLocationFromPaths(StandardLocation.SOURCE_PATH, sourceLocations);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   103
            return new OnDemandJavadocHelper(mainTask, fm);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   104
        } catch (IOException ex) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   105
            try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   106
                fm.close();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   107
            } catch (IOException closeEx) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   108
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   109
            return new JavadocHelper() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   110
                @Override
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   111
                public String getResolvedDocComment(Element forElement) throws IOException {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   112
                    return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   113
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   114
                @Override
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   115
                public Element getSourceElement(Element forElement) throws IOException {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   116
                    return forElement;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   117
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   118
                @Override
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   119
                public void close() throws IOException {}
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   120
            };
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   121
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   122
    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   123
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   124
    /**Returns javadoc for the given element, if it can be found, or null otherwise. The javadoc
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   125
     * will have @inheritDoc resolved.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   126
     *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   127
     * @param forElement element for which the javadoc should be searched
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   128
     * @return javadoc if found, null otherwise
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   129
     * @throws IOException if something goes wrong in the search
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   130
     */
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   131
    public abstract String getResolvedDocComment(Element forElement) throws IOException;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   132
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   133
    /**Returns an element representing the same given program element, but the returned element will
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   134
     * be resolved from source, if it can be found. Returns the original element if the source for
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   135
     * the given element cannot be found.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   136
     *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   137
     * @param forElement element for which the source element should be searched
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   138
     * @return source element if found, the original element otherwise
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   139
     * @throws IOException if something goes wrong in the search
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   140
     */
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   141
    public abstract Element getSourceElement(Element forElement) throws IOException;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   142
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   143
    /**Closes the helper.
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   144
     *
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   145
     * @throws IOException if something foes wrong during the close
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   146
     */
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   147
    @Override
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   148
    public abstract void close() throws IOException;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   149
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   150
    private static final class OnDemandJavadocHelper extends JavadocHelper {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   151
        private final JavacTask mainTask;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   152
        private final JavaFileManager baseFileManager;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   153
        private final StandardJavaFileManager fm;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   154
        private final Map<String, Pair<JavacTask, TreePath>> signature2Source = new HashMap<>();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   155
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   156
        private OnDemandJavadocHelper(JavacTask mainTask, StandardJavaFileManager fm) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   157
            this.mainTask = mainTask;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   158
            this.baseFileManager = ((JavacTaskImpl) mainTask).getContext().get(JavaFileManager.class);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   159
            this.fm = fm;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   160
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   161
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   162
        @Override
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   163
        public String getResolvedDocComment(Element forElement) throws IOException {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   164
            Pair<JavacTask, TreePath> sourceElement = getSourceElement(mainTask, forElement);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   165
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   166
            if (sourceElement == null)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   167
                return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   168
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   169
            return getResolvedDocComment(sourceElement.fst, sourceElement.snd);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   170
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   171
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   172
        @Override
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   173
        public Element getSourceElement(Element forElement) throws IOException {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   174
            Pair<JavacTask, TreePath> sourceElement = getSourceElement(mainTask, forElement);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   175
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   176
            if (sourceElement == null)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   177
                return forElement;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   178
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   179
            Element result = Trees.instance(sourceElement.fst).getElement(sourceElement.snd);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   180
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   181
            if (result == null)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   182
                return forElement;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   183
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   184
            return result;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   185
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   186
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   187
        private String getResolvedDocComment(JavacTask task, TreePath el) throws IOException {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   188
            DocTrees trees = DocTrees.instance(task);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   189
            Element element = trees.getElement(el);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   190
            String docComment = trees.getDocComment(el);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   191
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   192
            if (docComment == null && element.getKind() == ElementKind.METHOD) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   193
                ExecutableElement executableElement = (ExecutableElement) element;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   194
                Iterable<Element> superTypes =
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   195
                        () -> superTypeForInheritDoc(task, element.getEnclosingElement()).iterator();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   196
                for (Element sup : superTypes) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   197
                   for (ExecutableElement supMethod : ElementFilter.methodsIn(sup.getEnclosedElements())) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   198
                       TypeElement clazz = (TypeElement) executableElement.getEnclosingElement();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   199
                       if (task.getElements().overrides(executableElement, supMethod, clazz)) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   200
                           Pair<JavacTask, TreePath> source = getSourceElement(task, supMethod);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   201
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   202
                           if (source != null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   203
                               String overriddenComment = getResolvedDocComment(source.fst, source.snd);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   204
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   205
                               if (overriddenComment != null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   206
                                   return overriddenComment;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   207
                               }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   208
                           }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   209
                       }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   210
                   }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   211
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   212
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   213
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   214
            DocCommentTree docCommentTree = parseDocComment(task, docComment);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   215
            IOException[] exception = new IOException[1];
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   216
            Map<int[], String> replace = new TreeMap<>((span1, span2) -> span2[0] - span1[0]);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   217
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   218
            new DocTreeScanner<Void, Void>() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   219
                private Stack<DocTree> interestingParent = new Stack<>();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   220
                private DocCommentTree dcTree;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   221
                private JavacTask inheritedJavacTask;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   222
                private TreePath inheritedTreePath;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   223
                private String inherited;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   224
                private Map<DocTree, String> syntheticTrees = new IdentityHashMap<>();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   225
                private long lastPos = 0;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   226
                @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   227
                public Void visitDocComment(DocCommentTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   228
                    dcTree = node;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   229
                    interestingParent.push(node);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   230
                    try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   231
                        scan(node.getFirstSentence(), p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   232
                        scan(node.getBody(), p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   233
                        List<DocTree> augmentedBlockTags = new ArrayList<>(node.getBlockTags());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   234
                        if (element.getKind() == ElementKind.METHOD) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   235
                            ExecutableElement executableElement = (ExecutableElement) element;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   236
                            List<String> parameters =
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   237
                                    executableElement.getParameters()
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   238
                                                     .stream()
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   239
                                                     .map(param -> param.getSimpleName().toString())
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   240
                                                     .collect(Collectors.toList());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   241
                            List<String> throwsList =
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   242
                                    executableElement.getThrownTypes()
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   243
                                                     .stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 41865
diff changeset
   244
                                                     .map(TypeMirror::toString)
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   245
                                                     .collect(Collectors.toList());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   246
                            Set<String> missingParams = new HashSet<>(parameters);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   247
                            Set<String> missingThrows = new HashSet<>(throwsList);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   248
                            boolean hasReturn = false;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   249
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   250
                            for (DocTree dt : augmentedBlockTags) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   251
                                switch (dt.getKind()) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   252
                                    case PARAM:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   253
                                        missingParams.remove(((ParamTree) dt).getName().getName().toString());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   254
                                        break;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   255
                                    case THROWS:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   256
                                        missingThrows.remove(getThrownException(task, el, docCommentTree, (ThrowsTree) dt));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   257
                                        break;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   258
                                    case RETURN:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   259
                                        hasReturn = true;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   260
                                        break;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   261
                                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   262
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   263
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   264
                            for (String missingParam : missingParams) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   265
                                DocTree syntheticTag = parseBlockTag(task, "@param " + missingParam + " {@inheritDoc}");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   266
                                syntheticTrees.put(syntheticTag, "@param " + missingParam + " ");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   267
                                insertTag(augmentedBlockTags, syntheticTag, parameters, throwsList);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   268
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   269
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   270
                            for (String missingThrow : missingThrows) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   271
                                DocTree syntheticTag = parseBlockTag(task, "@throws " + missingThrow + " {@inheritDoc}");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   272
                                syntheticTrees.put(syntheticTag, "@throws " + missingThrow + " ");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   273
                                insertTag(augmentedBlockTags, syntheticTag, parameters, throwsList);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   274
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   275
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   276
                            if (!hasReturn) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   277
                                DocTree syntheticTag = parseBlockTag(task, "@return {@inheritDoc}");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   278
                                syntheticTrees.put(syntheticTag, "@return ");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   279
                                insertTag(augmentedBlockTags, syntheticTag, parameters, throwsList);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   280
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   281
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   282
                        scan(augmentedBlockTags, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   283
                        return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   284
                    } finally {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   285
                        interestingParent.pop();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   286
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   287
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   288
                @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   289
                public Void visitParam(ParamTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   290
                    interestingParent.push(node);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   291
                    try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   292
                        return super.visitParam(node, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   293
                    } finally {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   294
                        interestingParent.pop();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   295
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   296
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   297
                @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   298
                public Void visitThrows(ThrowsTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   299
                    interestingParent.push(node);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   300
                    try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   301
                        return super.visitThrows(node, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   302
                    } finally {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   303
                        interestingParent.pop();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   304
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   305
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   306
                @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   307
                public Void visitReturn(ReturnTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   308
                    interestingParent.push(node);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   309
                    try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   310
                        return super.visitReturn(node, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   311
                    } finally {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   312
                        interestingParent.pop();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   313
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   314
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   315
                @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   316
                public Void visitInheritDoc(InheritDocTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   317
                    if (inherited == null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   318
                        try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   319
                            if (element.getKind() == ElementKind.METHOD) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   320
                                ExecutableElement executableElement = (ExecutableElement) element;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   321
                                Iterable<Element> superTypes = () -> superTypeForInheritDoc(task, element.getEnclosingElement()).iterator();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   322
                                OUTER: for (Element sup : superTypes) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   323
                                   for (ExecutableElement supMethod : ElementFilter.methodsIn(sup.getEnclosedElements())) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   324
                                       if (task.getElements().overrides(executableElement, supMethod, (TypeElement) executableElement.getEnclosingElement())) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   325
                                           Pair<JavacTask, TreePath> source = getSourceElement(task, supMethod);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   326
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   327
                                           if (source != null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   328
                                               String overriddenComment = getResolvedDocComment(source.fst, source.snd);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   329
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   330
                                               if (overriddenComment != null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   331
                                                   inheritedJavacTask = source.fst;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   332
                                                   inheritedTreePath = source.snd;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   333
                                                   inherited = overriddenComment;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   334
                                                   break OUTER;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   335
                                               }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   336
                                           }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   337
                                       }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   338
                                   }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   339
                                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   340
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   341
                        } catch (IOException ex) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   342
                            exception[0] = ex;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   343
                            return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   344
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   345
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   346
                    if (inherited == null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   347
                        return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   348
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   349
                    DocCommentTree inheritedDocTree = parseDocComment(inheritedJavacTask, inherited);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   350
                    List<List<? extends DocTree>> inheritedText = new ArrayList<>();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   351
                    DocTree parent = interestingParent.peek();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   352
                    switch (parent.getKind()) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   353
                        case DOC_COMMENT:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   354
                            inheritedText.add(inheritedDocTree.getFullBody());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   355
                            break;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   356
                        case PARAM:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   357
                            String paramName = ((ParamTree) parent).getName().getName().toString();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   358
                            new DocTreeScanner<Void, Void>() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   359
                                @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   360
                                public Void visitParam(ParamTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   361
                                    if (node.getName().getName().contentEquals(paramName)) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   362
                                        inheritedText.add(node.getDescription());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   363
                                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   364
                                    return super.visitParam(node, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   365
                                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   366
                            }.scan(inheritedDocTree, null);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   367
                            break;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   368
                        case THROWS:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   369
                            String thrownName = getThrownException(task, el, docCommentTree, (ThrowsTree) parent);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   370
                            new DocTreeScanner<Void, Void>() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   371
                                @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   372
                                public Void visitThrows(ThrowsTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   373
                                    if (Objects.equals(getThrownException(inheritedJavacTask, inheritedTreePath, inheritedDocTree, node), thrownName)) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   374
                                        inheritedText.add(node.getDescription());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   375
                                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   376
                                    return super.visitThrows(node, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   377
                                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   378
                            }.scan(inheritedDocTree, null);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   379
                            break;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   380
                        case RETURN:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   381
                            new DocTreeScanner<Void, Void>() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   382
                                @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   383
                                public Void visitReturn(ReturnTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   384
                                    inheritedText.add(node.getDescription());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   385
                                    return super.visitReturn(node, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   386
                                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   387
                            }.scan(inheritedDocTree, null);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   388
                            break;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   389
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   390
                    if (!inheritedText.isEmpty()) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   391
                        long offset = trees.getSourcePositions().getStartPosition(null, inheritedDocTree, inheritedDocTree);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   392
                        long start = Long.MAX_VALUE;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   393
                        long end = Long.MIN_VALUE;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   394
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   395
                        for (DocTree t : inheritedText.get(0)) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   396
                            start = Math.min(start, trees.getSourcePositions().getStartPosition(null, inheritedDocTree, t) - offset);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   397
                            end   = Math.max(end,   trees.getSourcePositions().getEndPosition(null, inheritedDocTree, t) - offset);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   398
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   399
                        String text = inherited.substring((int) start, (int) end);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   400
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   401
                        if (syntheticTrees.containsKey(parent)) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   402
                            replace.put(new int[] {(int) lastPos + 1, (int) lastPos}, "\n" + syntheticTrees.get(parent) + text);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   403
                        } else {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   404
                            long inheritedStart = trees.getSourcePositions().getStartPosition(null, dcTree, node);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   405
                            long inheritedEnd   = trees.getSourcePositions().getEndPosition(null, dcTree, node);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   406
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   407
                            replace.put(new int[] {(int) inheritedStart, (int) inheritedEnd}, text);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   408
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   409
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   410
                    return super.visitInheritDoc(node, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   411
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   412
                private boolean inSynthetic;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   413
                @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   414
                public Void scan(DocTree tree, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   415
                    if (exception[0] != null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   416
                        return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   417
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   418
                    boolean prevInSynthetic = inSynthetic;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   419
                    try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   420
                        inSynthetic |= syntheticTrees.containsKey(tree);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   421
                        return super.scan(tree, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   422
                    } finally {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   423
                        if (!inSynthetic) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   424
                            lastPos = trees.getSourcePositions().getEndPosition(null, dcTree, tree);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   425
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   426
                        inSynthetic = prevInSynthetic;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   427
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   428
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   429
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   430
                private void insertTag(List<DocTree> tags, DocTree toInsert, List<String> parameters, List<String> throwsTypes) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   431
                    Comparator<DocTree> comp = (tag1, tag2) -> {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   432
                        if (tag1.getKind() == tag2.getKind()) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   433
                            switch (toInsert.getKind()) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   434
                                case PARAM: {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   435
                                    ParamTree p1 = (ParamTree) tag1;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   436
                                    ParamTree p2 = (ParamTree) tag2;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   437
                                    int i1 = parameters.indexOf(p1.getName().getName().toString());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   438
                                    int i2 = parameters.indexOf(p2.getName().getName().toString());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   439
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   440
                                    return i1 - i2;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   441
                                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   442
                                case THROWS: {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   443
                                    ThrowsTree t1 = (ThrowsTree) tag1;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   444
                                    ThrowsTree t2 = (ThrowsTree) tag2;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   445
                                    int i1 = throwsTypes.indexOf(getThrownException(task, el, docCommentTree, t1));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   446
                                    int i2 = throwsTypes.indexOf(getThrownException(task, el, docCommentTree, t2));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   447
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   448
                                    return i1 - i2;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   449
                                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   450
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   451
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   452
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   453
                        int i1 = tagOrder.indexOf(tag1.getKind());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   454
                        int i2 = tagOrder.indexOf(tag2.getKind());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   455
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   456
                        return i1 - i2;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   457
                    };
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   458
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   459
                    for (int i = 0; i < tags.size(); i++) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   460
                        if (comp.compare(tags.get(i), toInsert) >= 0) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   461
                            tags.add(i, toInsert);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   462
                            return ;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   463
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   464
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   465
                    tags.add(toInsert);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   466
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   467
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   468
                private final List<DocTree.Kind> tagOrder = Arrays.asList(DocTree.Kind.PARAM, DocTree.Kind.THROWS, DocTree.Kind.RETURN);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   469
            }.scan(docCommentTree, null);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   470
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   471
            if (replace.isEmpty())
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   472
                return docComment;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   473
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   474
            StringBuilder replacedInheritDoc = new StringBuilder(docComment);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   475
            int offset = (int) trees.getSourcePositions().getStartPosition(null, docCommentTree, docCommentTree);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   476
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   477
            for (Entry<int[], String> e : replace.entrySet()) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   478
                replacedInheritDoc.delete(e.getKey()[0] - offset, e.getKey()[1] - offset + 1);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   479
                replacedInheritDoc.insert(e.getKey()[0] - offset, e.getValue());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   480
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   481
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   482
            return replacedInheritDoc.toString();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   483
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   484
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   485
        private Stream<Element> superTypeForInheritDoc(JavacTask task, Element type) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   486
            TypeElement clazz = (TypeElement) type;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   487
            Stream<Element> result = interfaces(clazz);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   488
            result = Stream.concat(result, interfaces(clazz).flatMap(el -> superTypeForInheritDoc(task, el)));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   489
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   490
            if (clazz.getSuperclass().getKind() == TypeKind.DECLARED) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   491
                Element superClass = ((DeclaredType) clazz.getSuperclass()).asElement();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   492
                result = Stream.concat(result, Stream.of(superClass));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   493
                result = Stream.concat(result, superTypeForInheritDoc(task, superClass));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   494
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   495
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   496
            return result;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   497
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   498
        //where:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   499
            private Stream<Element> interfaces(TypeElement clazz) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   500
                return clazz.getInterfaces()
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   501
                            .stream()
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   502
                            .filter(tm -> tm.getKind() == TypeKind.DECLARED)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   503
                            .map(tm -> ((DeclaredType) tm).asElement());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   504
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   505
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   506
         private DocTree parseBlockTag(JavacTask task, String blockTag) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   507
            DocCommentTree dc = parseDocComment(task, blockTag);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   508
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   509
            return dc.getBlockTags().get(0);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   510
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   511
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   512
        private DocCommentTree parseDocComment(JavacTask task, String javadoc) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   513
            DocTrees trees = DocTrees.instance(task);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   514
            try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   515
                return trees.getDocCommentTree(new SimpleJavaFileObject(new URI("mem://doc.html"), javax.tools.JavaFileObject.Kind.HTML) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   516
                    @Override @DefinedBy(Api.COMPILER)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   517
                    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   518
                        return "<body>" + javadoc + "</body>";
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   519
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   520
                });
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   521
            } catch (URISyntaxException ex) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   522
                return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   523
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   524
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   525
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   526
        private String getThrownException(JavacTask task, TreePath rootOn, DocCommentTree comment, ThrowsTree tt) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   527
            DocTrees trees = DocTrees.instance(task);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   528
            Element exc = trees.getElement(new DocTreePath(new DocTreePath(rootOn, comment), tt.getExceptionName()));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   529
            return exc != null ? exc.toString() : null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   530
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   531
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   532
        private Pair<JavacTask, TreePath> getSourceElement(JavacTask origin, Element el) throws IOException {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   533
            String handle = elementSignature(el);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   534
            Pair<JavacTask, TreePath> cached = signature2Source.get(handle);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   535
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   536
            if (cached != null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   537
                return cached.fst != null ? cached : null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   538
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   539
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   540
            TypeElement type = topLevelType(el);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   541
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   542
            if (type == null)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   543
                return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   544
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   545
            String binaryName = origin.getElements().getBinaryName(type).toString();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   546
            Pair<JavacTask, CompilationUnitTree> source = findSource(binaryName);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   547
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   548
            if (source == null)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   549
                return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   550
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   551
            fillElementCache(source.fst, source.snd);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   552
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   553
            cached = signature2Source.get(handle);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   554
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   555
            if (cached != null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   556
                return cached;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   557
            } else {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   558
                signature2Source.put(handle, Pair.of(null, null));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   559
                return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   560
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   561
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   562
        //where:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   563
            private String elementSignature(Element el) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   564
                switch (el.getKind()) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   565
                    case ANNOTATION_TYPE: case CLASS: case ENUM: case INTERFACE:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   566
                        return ((TypeElement) el).getQualifiedName().toString();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   567
                    case FIELD:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   568
                        return elementSignature(el.getEnclosingElement()) + "." + el.getSimpleName() + ":" + el.asType();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   569
                    case ENUM_CONSTANT:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   570
                        return elementSignature(el.getEnclosingElement()) + "." + el.getSimpleName();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   571
                    case EXCEPTION_PARAMETER: case LOCAL_VARIABLE: case PARAMETER: case RESOURCE_VARIABLE:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   572
                        return el.getSimpleName() + ":" + el.asType();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   573
                    case CONSTRUCTOR: case METHOD:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   574
                        StringBuilder header = new StringBuilder();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   575
                        header.append(elementSignature(el.getEnclosingElement()));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   576
                        if (el.getKind() == ElementKind.METHOD) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   577
                            header.append(".");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   578
                            header.append(el.getSimpleName());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   579
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   580
                        header.append("(");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   581
                        String sep = "";
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   582
                        ExecutableElement method = (ExecutableElement) el;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   583
                        for (Iterator<? extends VariableElement> i = method.getParameters().iterator(); i.hasNext();) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   584
                            VariableElement p = i.next();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   585
                            header.append(sep);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   586
                            header.append(p.asType());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   587
                            sep = ", ";
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   588
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   589
                        header.append(")");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   590
                        return header.toString();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   591
                   default:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   592
                        return el.toString();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   593
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   594
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   595
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   596
            private TypeElement topLevelType(Element el) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   597
                if (el.getKind() == ElementKind.PACKAGE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   598
                    return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   599
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   600
                while (el != null && el.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   601
                    el = el.getEnclosingElement();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   602
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   603
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   604
                return el != null && (el.getKind().isClass() || el.getKind().isInterface()) ? (TypeElement) el : null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   605
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   606
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   607
            private void fillElementCache(JavacTask task, CompilationUnitTree cut) throws IOException {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   608
                Trees trees = Trees.instance(task);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   609
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   610
                new TreePathScanner<Void, Void>() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   611
                    @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   612
                    public Void visitMethod(MethodTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   613
                        handleDeclaration();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   614
                        return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   615
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   616
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   617
                    @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   618
                    public Void visitClass(ClassTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   619
                        handleDeclaration();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   620
                        return super.visitClass(node, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   621
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   622
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   623
                    @Override @DefinedBy(Api.COMPILER_TREE)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   624
                    public Void visitVariable(VariableTree node, Void p) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   625
                        handleDeclaration();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   626
                        return super.visitVariable(node, p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   627
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   628
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   629
                    private void handleDeclaration() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   630
                        Element currentElement = trees.getElement(getCurrentPath());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   631
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   632
                        if (currentElement != null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   633
                            signature2Source.put(elementSignature(currentElement), Pair.of(task, getCurrentPath()));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   634
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   635
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   636
                }.scan(cut, null);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   637
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   638
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   639
        private Pair<JavacTask, CompilationUnitTree> findSource(String binaryName) throws IOException {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   640
            JavaFileObject jfo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH,
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   641
                                                        binaryName,
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   642
                                                        JavaFileObject.Kind.SOURCE);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   643
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   644
            if (jfo == null)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   645
                return null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   646
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   647
            List<JavaFileObject> jfos = Arrays.asList(jfo);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   648
            JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, baseFileManager, d -> {}, null, null, jfos);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   649
            Iterable<? extends CompilationUnitTree> cuts = task.parse();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   650
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   651
            task.enter();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   652
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   653
            return Pair.of(task, cuts.iterator().next());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   654
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   655
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   656
        @Override
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   657
        public void close() throws IOException {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   658
            fm.close();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   659
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   660
    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   661
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents:
diff changeset
   662
}