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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ThrowsTaglet.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,187 @@
     1.4 +/*
     1.5 + * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.doclets.internal.toolkit.taglets;
    1.30 +
    1.31 +import java.util.*;
    1.32 +
    1.33 +import com.sun.javadoc.*;
    1.34 +import com.sun.tools.doclets.internal.toolkit.Content;
    1.35 +import com.sun.tools.doclets.internal.toolkit.util.*;
    1.36 +
    1.37 +/**
    1.38 + * A taglet that represents the @throws tag.
    1.39 + *
    1.40 + *  <p><b>This is NOT part of any supported API.
    1.41 + *  If you write code that depends on this, you do so at your own risk.
    1.42 + *  This code and its internal interfaces are subject to change or
    1.43 + *  deletion without notice.</b>
    1.44 + *
    1.45 + * @author Jamie Ho
    1.46 + * @since 1.4
    1.47 + */
    1.48 +public class ThrowsTaglet extends BaseExecutableMemberTaglet
    1.49 +    implements InheritableTaglet {
    1.50 +
    1.51 +    public ThrowsTaglet() {
    1.52 +        name = "throws";
    1.53 +    }
    1.54 +
    1.55 +    /**
    1.56 +     * {@inheritDoc}
    1.57 +     */
    1.58 +    public void inherit(DocFinder.Input input, DocFinder.Output output) {
    1.59 +        ClassDoc exception;
    1.60 +        if (input.tagId == null) {
    1.61 +            ThrowsTag throwsTag = (ThrowsTag) input.tag;
    1.62 +            exception = throwsTag.exception();
    1.63 +            input.tagId = exception == null ?
    1.64 +                throwsTag.exceptionName() :
    1.65 +                throwsTag.exception().qualifiedName();
    1.66 +        } else {
    1.67 +            exception = input.element.containingClass().findClass(input.tagId);
    1.68 +        }
    1.69 +
    1.70 +        ThrowsTag[] tags = ((MethodDoc)input.element).throwsTags();
    1.71 +        for (int i = 0; i < tags.length; i++) {
    1.72 +            if (input.tagId.equals(tags[i].exceptionName()) ||
    1.73 +                (tags[i].exception() != null &&
    1.74 +                    (input.tagId.equals(tags[i].exception().qualifiedName())))) {
    1.75 +                output.holder = input.element;
    1.76 +                output.holderTag = tags[i];
    1.77 +                output.inlineTags = input.isFirstSentence ?
    1.78 +                    tags[i].firstSentenceTags() : tags[i].inlineTags();
    1.79 +                output.tagList.add(tags[i]);
    1.80 +            } else if (exception != null && tags[i].exception() != null &&
    1.81 +                    tags[i].exception().subclassOf(exception)) {
    1.82 +                output.tagList.add(tags[i]);
    1.83 +            }
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    /**
    1.88 +     * Add links for exceptions that are declared but not documented.
    1.89 +     */
    1.90 +    private Content linkToUndocumentedDeclaredExceptions(
    1.91 +            Type[] declaredExceptionTypes, Set<String> alreadyDocumented,
    1.92 +            TagletWriter writer) {
    1.93 +        Content result = writer.getOutputInstance();
    1.94 +        //Add links to the exceptions declared but not documented.
    1.95 +        for (int i = 0; i < declaredExceptionTypes.length; i++) {
    1.96 +            if (declaredExceptionTypes[i].asClassDoc() != null &&
    1.97 +                ! alreadyDocumented.contains(
    1.98 +                        declaredExceptionTypes[i].asClassDoc().name()) &&
    1.99 +                ! alreadyDocumented.contains(
   1.100 +                    declaredExceptionTypes[i].asClassDoc().qualifiedName())) {
   1.101 +                if (alreadyDocumented.size() == 0) {
   1.102 +                    result.addContent(writer.getThrowsHeader());
   1.103 +                }
   1.104 +                result.addContent(writer.throwsTagOutput(declaredExceptionTypes[i]));
   1.105 +                alreadyDocumented.add(declaredExceptionTypes[i].asClassDoc().name());
   1.106 +            }
   1.107 +        }
   1.108 +        return result;
   1.109 +    }
   1.110 +
   1.111 +    /**
   1.112 +     * Inherit throws documentation for exceptions that were declared but not
   1.113 +     * documented.
   1.114 +     */
   1.115 +    private Content inheritThrowsDocumentation(Doc holder,
   1.116 +            Type[] declaredExceptionTypes, Set<String> alreadyDocumented,
   1.117 +            TagletWriter writer) {
   1.118 +        Content result = writer.getOutputInstance();
   1.119 +        if (holder instanceof MethodDoc) {
   1.120 +            Set<Tag> declaredExceptionTags = new LinkedHashSet<Tag>();
   1.121 +            for (int j = 0; j < declaredExceptionTypes.length; j++) {
   1.122 +                DocFinder.Output inheritedDoc =
   1.123 +                    DocFinder.search(new DocFinder.Input((MethodDoc) holder, this,
   1.124 +                        declaredExceptionTypes[j].typeName()));
   1.125 +                if (inheritedDoc.tagList.size() == 0) {
   1.126 +                    inheritedDoc = DocFinder.search(new DocFinder.Input(
   1.127 +                        (MethodDoc) holder, this,
   1.128 +                        declaredExceptionTypes[j].qualifiedTypeName()));
   1.129 +                }
   1.130 +                declaredExceptionTags.addAll(inheritedDoc.tagList);
   1.131 +            }
   1.132 +            result.addContent(throwsTagsOutput(
   1.133 +                declaredExceptionTags.toArray(new ThrowsTag[] {}),
   1.134 +                writer, alreadyDocumented, false));
   1.135 +        }
   1.136 +        return result;
   1.137 +    }
   1.138 +
   1.139 +    /**
   1.140 +     * {@inheritDoc}
   1.141 +     */
   1.142 +    public Content getTagletOutput(Doc holder, TagletWriter writer) {
   1.143 +        ExecutableMemberDoc execHolder = (ExecutableMemberDoc) holder;
   1.144 +        ThrowsTag[] tags = execHolder.throwsTags();
   1.145 +        Content result = writer.getOutputInstance();
   1.146 +        HashSet<String> alreadyDocumented = new HashSet<String>();
   1.147 +        if (tags.length > 0) {
   1.148 +            result.addContent(throwsTagsOutput(
   1.149 +                execHolder.throwsTags(), writer, alreadyDocumented, true));
   1.150 +        }
   1.151 +        result.addContent(inheritThrowsDocumentation(holder,
   1.152 +            execHolder.thrownExceptionTypes(), alreadyDocumented, writer));
   1.153 +        result.addContent(linkToUndocumentedDeclaredExceptions(
   1.154 +            execHolder.thrownExceptionTypes(), alreadyDocumented, writer));
   1.155 +        return result;
   1.156 +    }
   1.157 +
   1.158 +
   1.159 +    /**
   1.160 +     * Given an array of <code>Tag</code>s representing this custom
   1.161 +     * tag, return its string representation.
   1.162 +     * @param throwTags the array of <code>ThrowsTag</code>s to convert.
   1.163 +     * @param writer the TagletWriter that will write this tag.
   1.164 +     * @param alreadyDocumented the set of exceptions that have already
   1.165 +     *        been documented.
   1.166 +     * @param allowDups True if we allow duplicate throws tags to be documented.
   1.167 +     * @return the Content representation of this <code>Tag</code>.
   1.168 +     */
   1.169 +    protected Content throwsTagsOutput(ThrowsTag[] throwTags,
   1.170 +        TagletWriter writer, Set<String> alreadyDocumented, boolean allowDups) {
   1.171 +        Content result = writer.getOutputInstance();
   1.172 +        if (throwTags.length > 0) {
   1.173 +            for (int i = 0; i < throwTags.length; ++i) {
   1.174 +                ThrowsTag tt = throwTags[i];
   1.175 +                ClassDoc cd = tt.exception();
   1.176 +                if ((!allowDups) && (alreadyDocumented.contains(tt.exceptionName()) ||
   1.177 +                    (cd != null && alreadyDocumented.contains(cd.qualifiedName())))) {
   1.178 +                    continue;
   1.179 +                }
   1.180 +                if (alreadyDocumented.size() == 0) {
   1.181 +                    result.addContent(writer.getThrowsHeader());
   1.182 +                }
   1.183 +                result.addContent(writer.throwsTagOutput(tt));
   1.184 +                alreadyDocumented.add(cd != null ?
   1.185 +                    cd.qualifiedName() : tt.exceptionName());
   1.186 +            }
   1.187 +        }
   1.188 +        return result;
   1.189 +    }
   1.190 +}

mercurial