DocFinder.java revision 3692:87b48a8fb3cf
1/*
2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.javadoc.internal.doclets.toolkit.util;
27
28import java.util.*;
29
30import javax.lang.model.element.Element;
31import javax.lang.model.element.ExecutableElement;
32import javax.lang.model.element.TypeElement;
33import javax.lang.model.type.TypeMirror;
34
35import com.sun.source.doctree.DocTree;
36import jdk.javadoc.internal.doclets.toolkit.Configuration;
37import jdk.javadoc.internal.doclets.toolkit.taglets.InheritableTaglet;
38
39/**
40 * Search for the requested documentation.  Inherit documentation if necessary.
41 *
42 *  <p><b>This is NOT part of any supported API.
43 *  If you write code that depends on this, you do so at your own risk.
44 *  This code and its internal interfaces are subject to change or
45 *  deletion without notice.</b>
46 *
47 * @author Jamie Ho
48 */
49public class DocFinder {
50
51    public static final class DocTreeInfo {
52        public final DocTree docTree;
53        public final Element element;
54
55        public DocTreeInfo() {
56            this.docTree = null;
57            this.element = null;
58        }
59
60        public DocTreeInfo(DocTree docTree, Element baseElement) {
61            this.docTree = docTree;
62            this.element = baseElement;
63        }
64
65        @Override
66        public String toString() {
67            return "DocTreeInfo{" + "docTree=" + docTree + ", element=" + element + '}';
68        }
69    }
70
71    /**
72     * The class that encapsulates the input.
73     */
74    public static class Input {
75        /**
76         * The element to search documentation from.
77         */
78        public Element element;
79        /**
80         * The taglet to search for documentation on behalf of. Null if we want
81         * to search for overall documentation.
82         */
83        public InheritableTaglet taglet = null;
84
85        /**
86         * The id of the tag to retrieve documentation for.
87         */
88        public String tagId = null;
89
90        /**
91         * The tag to retrieve documentation for.  This is only used for the
92         * inheritDoc tag.
93         */
94        public final DocTreeInfo docTreeInfo;
95
96        /**
97         * True if we only want to search for the first sentence.
98         */
99        public boolean isFirstSentence = false;
100
101        /**
102         * True if we are looking for documentation to replace the inheritDocTag.
103         */
104        public boolean isInheritDocTag = false;
105
106        /**
107         * Used to distinguish between type variable param tags and regular
108         * param tags.
109         */
110        public boolean isTypeVariableParamTag = false;
111
112        public final Utils utils;
113
114        public Input(Utils utils, Element element, InheritableTaglet taglet, DocTreeInfo dtInfo,
115                boolean isFirstSentence, boolean isInheritDocTag) {
116            this.utils = utils;
117            this.element = element;
118            this.taglet = taglet;
119            this.isFirstSentence = isFirstSentence;
120            this.isInheritDocTag = isInheritDocTag;
121            this.docTreeInfo = dtInfo;
122        }
123
124        public Input(Utils utils, Element element, InheritableTaglet taglet, String tagId) {
125            this(utils, element);
126            this.taglet = taglet;
127            this.tagId = tagId;
128        }
129
130        public Input(Utils utils, Element element, InheritableTaglet taglet, String tagId,
131            boolean isTypeVariableParamTag) {
132            this(utils, element);
133            this.taglet = taglet;
134            this.tagId = tagId;
135            this.isTypeVariableParamTag = isTypeVariableParamTag;
136        }
137
138        public Input(Utils utils, Element element, InheritableTaglet taglet) {
139            this(utils, element);
140            this.taglet = taglet;
141        }
142
143        public Input(Utils utils, Element element) {
144            if (element == null)
145                throw new NullPointerException();
146            this.element = element;
147            this.utils = utils;
148            this.docTreeInfo = new DocTreeInfo();
149        }
150
151        public Input(Utils utils, Element element, boolean isFirstSentence) {
152            this(utils, element);
153            this.isFirstSentence = isFirstSentence;
154        }
155
156        public Input copy(Utils utils) {
157            if (this.element == null) {
158                throw new NullPointerException();
159            }
160            Input clone = new Input(utils, this.element, this.taglet, this.docTreeInfo,
161                    this.isFirstSentence, this.isInheritDocTag);
162            clone.tagId = this.tagId;
163            clone.isTypeVariableParamTag = this.isTypeVariableParamTag;
164            return clone;
165        }
166
167        /**
168         * For debugging purposes
169         * @return string representation
170         */
171        @Override
172        public String toString() {
173            String encl = element == null ? "" : element.getEnclosingElement().toString() + "::";
174            return "Input{" + "element=" + encl + element
175                    + ", taglet=" + taglet
176                    + ", tagId=" + tagId + ", tag=" + docTreeInfo
177                    + ", isFirstSentence=" + isFirstSentence
178                    + ", isInheritDocTag=" + isInheritDocTag
179                    + ", isTypeVariableParamTag=" + isTypeVariableParamTag
180                    + ", utils=" + utils + '}';
181        }
182    }
183
184    /**
185     * The class that encapsulates the output.
186     */
187    public static class Output {
188        /**
189         * The tag that holds the documentation.  Null if documentation
190         * is not held by a tag.
191         */
192        public DocTree holderTag;
193
194        /**
195         * The Doc object that holds the documentation.
196         */
197        public Element holder;
198
199        /**
200         * The inherited documentation.
201         */
202        public List<? extends DocTree> inlineTags = Collections.emptyList();
203
204        /**
205         * False if documentation could not be inherited.
206         */
207        public boolean isValidInheritDocTag = true;
208
209        /**
210         * When automatically inheriting throws tags, you sometime must inherit
211         * more than one tag.  For example if the element declares that it throws
212         * IOException and the overridden element has throws tags for IOException and
213         * ZipException, both tags would be inherited because ZipException is a
214         * subclass of IOException.  This subclass of DocFinder.Output allows
215         * multiple tag inheritence.
216         */
217        public List<DocTree> tagList  = new ArrayList<>();
218
219        /**
220         * Returns a string representation for debugging purposes
221         * @return string
222         */
223        @Override
224        public String toString() {
225            String encl = holder == null ? "" : holder.getEnclosingElement().toString() + "::";
226            return "Output{" + "holderTag=" + holderTag
227                    + ", holder=" + encl + holder
228                    + ", inlineTags=" + inlineTags
229                    + ", isValidInheritDocTag=" + isValidInheritDocTag
230                    + ", tagList=" + tagList + '}';
231        }
232    }
233
234    /**
235     * Search for the requested comments in the given element.  If it does not
236     * have comments, return documentation from the overridden element if possible.
237     * If the overridden element does not exist or does not have documentation to
238     * inherit, search for documentation to inherit from implemented methods.
239     *
240     * @param input the input object used to perform the search.
241     *
242     * @return an Output object representing the documentation that was found.
243     */
244    public static Output search(Configuration configuration, Input input) {
245        Output output = new Output();
246        Utils utils = configuration.utils;
247        if (input.isInheritDocTag) {
248            //Do nothing because "element" does not have any documentation.
249            //All it has is {@inheritDoc}.
250        } else if (input.taglet == null) {
251            //We want overall documentation.
252            output.inlineTags = input.isFirstSentence
253                    ? utils.getFirstSentenceTrees(input.element)
254                    : utils.getFullBody(input.element);
255            output.holder = input.element;
256        } else {
257            input.taglet.inherit(input, output);
258        }
259
260        if (output.inlineTags != null && !output.inlineTags.isEmpty()) {
261            return output;
262        }
263        output.isValidInheritDocTag = false;
264        Input inheritedSearchInput = input.copy(configuration.utils);
265        inheritedSearchInput.isInheritDocTag = false;
266        if (utils.isMethod(input.element)) {
267            ExecutableElement overriddenMethod = utils.overriddenMethod((ExecutableElement) input.element);
268            if (overriddenMethod != null) {
269                inheritedSearchInput.element = overriddenMethod;
270                output = search(configuration, inheritedSearchInput);
271                output.isValidInheritDocTag = true;
272                if (!output.inlineTags.isEmpty()) {
273                    return output;
274                }
275            }
276            //NOTE:  When we fix the bug where ClassDoc.interfaceTypes() does
277            //       not pass all implemented interfaces, we will use the
278            //       appropriate element here.
279            ImplementedMethods implMethods
280                    = new ImplementedMethods((ExecutableElement) input.element, configuration);
281            List<ExecutableElement> implementedMethods = implMethods.build();
282            for (ExecutableElement implementedMethod : implementedMethods) {
283                inheritedSearchInput.element = implementedMethod;
284                output = search(configuration, inheritedSearchInput);
285                output.isValidInheritDocTag = true;
286                if (!output.inlineTags.isEmpty()) {
287                    return output;
288                }
289            }
290        } else if (utils.isTypeElement(input.element)) {
291            TypeMirror t = ((TypeElement) input.element).getSuperclass();
292            Element superclass = utils.asTypeElement(t);
293            if (superclass != null) {
294                inheritedSearchInput.element = superclass;
295                output = search(configuration, inheritedSearchInput);
296                output.isValidInheritDocTag = true;
297                if (!output.inlineTags.isEmpty()) {
298                    return output;
299                }
300            }
301        }
302        return output;
303    }
304}
305