src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ThrowsTaglet.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 2001, 2013, 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  */
    26 package com.sun.tools.doclets.internal.toolkit.taglets;
    28 import java.util.*;
    30 import com.sun.javadoc.*;
    31 import com.sun.tools.doclets.internal.toolkit.Content;
    32 import com.sun.tools.doclets.internal.toolkit.util.*;
    34 /**
    35  * A taglet that represents the @throws tag.
    36  *
    37  *  <p><b>This is NOT part of any supported API.
    38  *  If you write code that depends on this, you do so at your own risk.
    39  *  This code and its internal interfaces are subject to change or
    40  *  deletion without notice.</b>
    41  *
    42  * @author Jamie Ho
    43  * @since 1.4
    44  */
    45 public class ThrowsTaglet extends BaseExecutableMemberTaglet
    46     implements InheritableTaglet {
    48     public ThrowsTaglet() {
    49         name = "throws";
    50     }
    52     /**
    53      * {@inheritDoc}
    54      */
    55     public void inherit(DocFinder.Input input, DocFinder.Output output) {
    56         ClassDoc exception;
    57         if (input.tagId == null) {
    58             ThrowsTag throwsTag = (ThrowsTag) input.tag;
    59             exception = throwsTag.exception();
    60             input.tagId = exception == null ?
    61                 throwsTag.exceptionName() :
    62                 throwsTag.exception().qualifiedName();
    63         } else {
    64             exception = input.element.containingClass().findClass(input.tagId);
    65         }
    67         ThrowsTag[] tags = ((MethodDoc)input.element).throwsTags();
    68         for (int i = 0; i < tags.length; i++) {
    69             if (input.tagId.equals(tags[i].exceptionName()) ||
    70                 (tags[i].exception() != null &&
    71                     (input.tagId.equals(tags[i].exception().qualifiedName())))) {
    72                 output.holder = input.element;
    73                 output.holderTag = tags[i];
    74                 output.inlineTags = input.isFirstSentence ?
    75                     tags[i].firstSentenceTags() : tags[i].inlineTags();
    76                 output.tagList.add(tags[i]);
    77             } else if (exception != null && tags[i].exception() != null &&
    78                     tags[i].exception().subclassOf(exception)) {
    79                 output.tagList.add(tags[i]);
    80             }
    81         }
    82     }
    84     /**
    85      * Add links for exceptions that are declared but not documented.
    86      */
    87     private Content linkToUndocumentedDeclaredExceptions(
    88             Type[] declaredExceptionTypes, Set<String> alreadyDocumented,
    89             TagletWriter writer) {
    90         Content result = writer.getOutputInstance();
    91         //Add links to the exceptions declared but not documented.
    92         for (int i = 0; i < declaredExceptionTypes.length; i++) {
    93             if (declaredExceptionTypes[i].asClassDoc() != null &&
    94                 ! alreadyDocumented.contains(
    95                         declaredExceptionTypes[i].asClassDoc().name()) &&
    96                 ! alreadyDocumented.contains(
    97                     declaredExceptionTypes[i].asClassDoc().qualifiedName())) {
    98                 if (alreadyDocumented.size() == 0) {
    99                     result.addContent(writer.getThrowsHeader());
   100                 }
   101                 result.addContent(writer.throwsTagOutput(declaredExceptionTypes[i]));
   102                 alreadyDocumented.add(declaredExceptionTypes[i].asClassDoc().name());
   103             }
   104         }
   105         return result;
   106     }
   108     /**
   109      * Inherit throws documentation for exceptions that were declared but not
   110      * documented.
   111      */
   112     private Content inheritThrowsDocumentation(Doc holder,
   113             Type[] declaredExceptionTypes, Set<String> alreadyDocumented,
   114             TagletWriter writer) {
   115         Content result = writer.getOutputInstance();
   116         if (holder instanceof MethodDoc) {
   117             Set<Tag> declaredExceptionTags = new LinkedHashSet<Tag>();
   118             for (int j = 0; j < declaredExceptionTypes.length; j++) {
   119                 DocFinder.Output inheritedDoc =
   120                     DocFinder.search(new DocFinder.Input((MethodDoc) holder, this,
   121                         declaredExceptionTypes[j].typeName()));
   122                 if (inheritedDoc.tagList.size() == 0) {
   123                     inheritedDoc = DocFinder.search(new DocFinder.Input(
   124                         (MethodDoc) holder, this,
   125                         declaredExceptionTypes[j].qualifiedTypeName()));
   126                 }
   127                 declaredExceptionTags.addAll(inheritedDoc.tagList);
   128             }
   129             result.addContent(throwsTagsOutput(
   130                 declaredExceptionTags.toArray(new ThrowsTag[] {}),
   131                 writer, alreadyDocumented, false));
   132         }
   133         return result;
   134     }
   136     /**
   137      * {@inheritDoc}
   138      */
   139     public Content getTagletOutput(Doc holder, TagletWriter writer) {
   140         ExecutableMemberDoc execHolder = (ExecutableMemberDoc) holder;
   141         ThrowsTag[] tags = execHolder.throwsTags();
   142         Content result = writer.getOutputInstance();
   143         HashSet<String> alreadyDocumented = new HashSet<String>();
   144         if (tags.length > 0) {
   145             result.addContent(throwsTagsOutput(
   146                 execHolder.throwsTags(), writer, alreadyDocumented, true));
   147         }
   148         result.addContent(inheritThrowsDocumentation(holder,
   149             execHolder.thrownExceptionTypes(), alreadyDocumented, writer));
   150         result.addContent(linkToUndocumentedDeclaredExceptions(
   151             execHolder.thrownExceptionTypes(), alreadyDocumented, writer));
   152         return result;
   153     }
   156     /**
   157      * Given an array of <code>Tag</code>s representing this custom
   158      * tag, return its string representation.
   159      * @param throwTags the array of <code>ThrowsTag</code>s to convert.
   160      * @param writer the TagletWriter that will write this tag.
   161      * @param alreadyDocumented the set of exceptions that have already
   162      *        been documented.
   163      * @param allowDups True if we allow duplicate throws tags to be documented.
   164      * @return the Content representation of this <code>Tag</code>.
   165      */
   166     protected Content throwsTagsOutput(ThrowsTag[] throwTags,
   167         TagletWriter writer, Set<String> alreadyDocumented, boolean allowDups) {
   168         Content result = writer.getOutputInstance();
   169         if (throwTags.length > 0) {
   170             for (int i = 0; i < throwTags.length; ++i) {
   171                 ThrowsTag tt = throwTags[i];
   172                 ClassDoc cd = tt.exception();
   173                 if ((!allowDups) && (alreadyDocumented.contains(tt.exceptionName()) ||
   174                     (cd != null && alreadyDocumented.contains(cd.qualifiedName())))) {
   175                     continue;
   176                 }
   177                 if (alreadyDocumented.size() == 0) {
   178                     result.addContent(writer.getThrowsHeader());
   179                 }
   180                 result.addContent(writer.throwsTagOutput(tt));
   181                 alreadyDocumented.add(cd != null ?
   182                     cd.qualifiedName() : tt.exceptionName());
   183             }
   184         }
   185         return result;
   186     }
   187 }

mercurial