langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ThrowsTaglet.java
author ksrini
Sun, 15 Jun 2014 08:41:57 -0700
changeset 25454 376a52c9540c
parent 22163 3651128c74eb
permissions -rw-r--r--
8039028: [javadoc] refactor the usage of Util.java Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
     2
 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.doclets.internal.toolkit.taglets;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    28
import java.util.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.javadoc.*;
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
    31
import com.sun.tools.doclets.internal.toolkit.Content;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.doclets.internal.toolkit.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 * A taglet that represents the @throws tag.
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    37
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    38
 *  If you write code that depends on this, you do so at your own risk.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    39
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    40
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * @author Jamie Ho
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * @since 1.4
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
public class ThrowsTaglet extends BaseExecutableMemberTaglet
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    implements InheritableTaglet {
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    public ThrowsTaglet() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        name = "throws";
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    public void inherit(DocFinder.Input input, DocFinder.Output output) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        ClassDoc exception;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        if (input.tagId == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            ThrowsTag throwsTag = (ThrowsTag) input.tag;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
            exception = throwsTag.exception();
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
            input.tagId = exception == null ?
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
                throwsTag.exceptionName() :
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
                throwsTag.exception().qualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        } else {
17547
40f2f9a5a445 8008768: Using {@inheritDoc} in simple tag defined via -tag fails
jjg
parents: 14260
diff changeset
    64
            exception = input.element.containingClass().findClass(input.tagId);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    67
        for (ThrowsTag tag : ((MethodDoc)input.element).throwsTags()) {
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    68
            if (input.tagId.equals(tag.exceptionName()) ||
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    69
                (tag.exception() != null &&
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    70
                 (input.tagId.equals(tag.exception().qualifiedName())))) {
17547
40f2f9a5a445 8008768: Using {@inheritDoc} in simple tag defined via -tag fails
jjg
parents: 14260
diff changeset
    71
                output.holder = input.element;
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    72
                output.holderTag = tag;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
                output.inlineTags = input.isFirstSentence ?
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    74
                                    tag.firstSentenceTags() : tag.inlineTags();
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    75
                output.tagList.add(tag);
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    76
            } else if (exception != null && tag.exception() != null &&
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    77
                     tag.exception().subclassOf(exception)) {
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    78
                output.tagList.add(tag);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     * Add links for exceptions that are declared but not documented.
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     */
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
    86
    private Content linkToUndocumentedDeclaredExceptions(
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    87
            Type[] declaredExceptionTypes, Set<String> alreadyDocumented,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            TagletWriter writer) {
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
    89
        Content result = writer.getOutputInstance();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        //Add links to the exceptions declared but not documented.
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    91
        for (Type declaredExceptionType : declaredExceptionTypes) {
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    92
            if (declaredExceptionType.asClassDoc() != null &&
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    93
                !alreadyDocumented.contains(
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    94
                        declaredExceptionType.asClassDoc().name()) &&
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    95
                !alreadyDocumented.contains(
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
    96
                        declaredExceptionType.asClassDoc().qualifiedName())) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                if (alreadyDocumented.size() == 0) {
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
    98
                    result.addContent(writer.getThrowsHeader());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                }
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
   100
                result.addContent(writer.throwsTagOutput(declaredExceptionType));
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
   101
                alreadyDocumented.add(declaredExceptionType.asClassDoc().name());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * Inherit throws documentation for exceptions that were declared but not
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * documented.
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     */
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   111
    private Content inheritThrowsDocumentation(Doc holder,
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   112
            Type[] declaredExceptionTypes, Set<String> alreadyDocumented,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            TagletWriter writer) {
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   114
        Content result = writer.getOutputInstance();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        if (holder instanceof MethodDoc) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
   116
            Set<Tag> declaredExceptionTags = new LinkedHashSet<>();
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
   117
            for (Type declaredExceptionType : declaredExceptionTypes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                DocFinder.Output inheritedDoc =
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
   119
                        DocFinder.search(writer.configuration(), new DocFinder.Input((MethodDoc) holder, this,
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
   120
                                                             declaredExceptionType.typeName()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                if (inheritedDoc.tagList.size() == 0) {
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
   122
                    inheritedDoc = DocFinder.search(writer.configuration(), new DocFinder.Input(
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
   123
                            (MethodDoc) holder, this,
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
   124
                            declaredExceptionType.qualifiedTypeName()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                declaredExceptionTags.addAll(inheritedDoc.tagList);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            }
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   128
            result.addContent(throwsTagsOutput(
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   129
                declaredExceptionTags.toArray(new ThrowsTag[] {}),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                writer, alreadyDocumented, false));
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     */
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   138
    public Content getTagletOutput(Doc holder, TagletWriter writer) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        ExecutableMemberDoc execHolder = (ExecutableMemberDoc) holder;
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        ThrowsTag[] tags = execHolder.throwsTags();
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   141
        Content result = writer.getOutputInstance();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
   142
        HashSet<String> alreadyDocumented = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        if (tags.length > 0) {
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   144
            result.addContent(throwsTagsOutput(
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                execHolder.throwsTags(), writer, alreadyDocumented, true));
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        }
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   147
        result.addContent(inheritThrowsDocumentation(holder,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            execHolder.thrownExceptionTypes(), alreadyDocumented, writer));
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   149
        result.addContent(linkToUndocumentedDeclaredExceptions(
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            execHolder.thrownExceptionTypes(), alreadyDocumented, writer));
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     * Given an array of <code>Tag</code>s representing this custom
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     * tag, return its string representation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     * @param throwTags the array of <code>ThrowsTag</code>s to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     * @param writer the TagletWriter that will write this tag.
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     * @param alreadyDocumented the set of exceptions that have already
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     *        been documented.
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     * @param allowDups True if we allow duplicate throws tags to be documented.
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   163
     * @return the Content representation of this <code>Tag</code>.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     */
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   165
    protected Content throwsTagsOutput(ThrowsTag[] throwTags,
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   166
        TagletWriter writer, Set<String> alreadyDocumented, boolean allowDups) {
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   167
        Content result = writer.getOutputInstance();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        if (throwTags.length > 0) {
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
   169
            for (ThrowsTag tt : throwTags) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                ClassDoc cd = tt.exception();
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                if ((!allowDups) && (alreadyDocumented.contains(tt.exceptionName()) ||
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
   172
                                     (cd != null && alreadyDocumented.contains(cd.qualifiedName())))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                    continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                if (alreadyDocumented.size() == 0) {
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   176
                    result.addContent(writer.getThrowsHeader());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                }
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17547
diff changeset
   178
                result.addContent(writer.throwsTagOutput(tt));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                alreadyDocumented.add(cd != null ?
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17574
diff changeset
   180
                                      cd.qualifiedName() : tt.exceptionName());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
}