src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ParamTaglet.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/ParamTaglet.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,324 @@
     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 @param 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 ParamTaglet extends BaseTaglet implements InheritableTaglet {
    1.49 +
    1.50 +    /**
    1.51 +     * Construct a ParamTaglet.
    1.52 +     */
    1.53 +    public ParamTaglet() {
    1.54 +        name = "param";
    1.55 +    }
    1.56 +
    1.57 +    /**
    1.58 +     * Given an array of <code>Parameter</code>s, return
    1.59 +     * a name/rank number map.  If the array is null, then
    1.60 +     * null is returned.
    1.61 +     * @param params The array of parmeters (from type or executable member) to
    1.62 +     *               check.
    1.63 +     * @return a name-rank number map.
    1.64 +     */
    1.65 +    private static Map<String,String> getRankMap(Object[] params){
    1.66 +        if (params == null) {
    1.67 +            return null;
    1.68 +        }
    1.69 +        HashMap<String,String> result = new HashMap<String,String>();
    1.70 +        for (int i = 0; i < params.length; i++) {
    1.71 +            String name = params[i] instanceof Parameter ?
    1.72 +                ((Parameter) params[i]).name() :
    1.73 +                ((TypeVariable) params[i]).typeName();
    1.74 +            result.put(name, String.valueOf(i));
    1.75 +        }
    1.76 +        return result;
    1.77 +    }
    1.78 +
    1.79 +    /**
    1.80 +     * {@inheritDoc}
    1.81 +     */
    1.82 +    public void inherit(DocFinder.Input input, DocFinder.Output output) {
    1.83 +        if (input.tagId == null) {
    1.84 +            input.isTypeVariableParamTag = ((ParamTag) input.tag).isTypeParameter();
    1.85 +            Object[] parameters = input.isTypeVariableParamTag ?
    1.86 +                (Object[]) ((MethodDoc) input.tag.holder()).typeParameters() :
    1.87 +                (Object[]) ((MethodDoc) input.tag.holder()).parameters();
    1.88 +            String target = ((ParamTag) input.tag).parameterName();
    1.89 +            int i;
    1.90 +            for (i = 0; i < parameters.length; i++) {
    1.91 +                String name = parameters[i] instanceof Parameter ?
    1.92 +                    ((Parameter) parameters[i]).name() :
    1.93 +                    ((TypeVariable) parameters[i]).typeName();
    1.94 +                if (name.equals(target)) {
    1.95 +                    input.tagId = String.valueOf(i);
    1.96 +                    break;
    1.97 +                }
    1.98 +            }
    1.99 +            if (i == parameters.length) {
   1.100 +                //Someone used {@inheritDoc} on an invalid @param tag.
   1.101 +                //We don't know where to inherit from.
   1.102 +                //XXX: in the future when Configuration is available here,
   1.103 +                //print a warning for this mistake.
   1.104 +                return;
   1.105 +            }
   1.106 +        }
   1.107 +        ParamTag[] tags = input.isTypeVariableParamTag ?
   1.108 +            ((MethodDoc)input.element).typeParamTags() : ((MethodDoc)input.element).paramTags();
   1.109 +        Map<String, String> rankMap = getRankMap(input.isTypeVariableParamTag ?
   1.110 +            (Object[]) ((MethodDoc)input.element).typeParameters() :
   1.111 +            (Object[]) ((MethodDoc)input.element).parameters());
   1.112 +        for (int i = 0; i < tags.length; i++) {
   1.113 +            if (rankMap.containsKey(tags[i].parameterName()) &&
   1.114 +                    rankMap.get(tags[i].parameterName()).equals((input.tagId))) {
   1.115 +                output.holder = input.element;
   1.116 +                output.holderTag = tags[i];
   1.117 +                output.inlineTags = input.isFirstSentence ?
   1.118 +                    tags[i].firstSentenceTags() : tags[i].inlineTags();
   1.119 +                return;
   1.120 +            }
   1.121 +        }
   1.122 +    }
   1.123 +
   1.124 +    /**
   1.125 +     * {@inheritDoc}
   1.126 +     */
   1.127 +    public boolean inField() {
   1.128 +        return false;
   1.129 +    }
   1.130 +
   1.131 +    /**
   1.132 +     * {@inheritDoc}
   1.133 +     */
   1.134 +    public boolean inMethod() {
   1.135 +        return true;
   1.136 +    }
   1.137 +
   1.138 +    /**
   1.139 +     * {@inheritDoc}
   1.140 +     */
   1.141 +    public boolean inOverview() {
   1.142 +        return false;
   1.143 +    }
   1.144 +
   1.145 +    /**
   1.146 +     * {@inheritDoc}
   1.147 +     */
   1.148 +    public boolean inPackage() {
   1.149 +        return false;
   1.150 +    }
   1.151 +
   1.152 +    /**
   1.153 +     * {@inheritDoc}
   1.154 +     */
   1.155 +    public boolean inType() {
   1.156 +        return true;
   1.157 +    }
   1.158 +
   1.159 +    /**
   1.160 +     * {@inheritDoc}
   1.161 +     */
   1.162 +    public boolean isInlineTag() {
   1.163 +        return false;
   1.164 +    }
   1.165 +
   1.166 +    /**
   1.167 +     * Given an array of <code>ParamTag</code>s,return its string representation.
   1.168 +     * @param holder the member that holds the param tags.
   1.169 +     * @param writer the TagletWriter that will write this tag.
   1.170 +     * @return the TagletOutput representation of these <code>ParamTag</code>s.
   1.171 +     */
   1.172 +    public Content getTagletOutput(Doc holder, TagletWriter writer) {
   1.173 +        if (holder instanceof ExecutableMemberDoc) {
   1.174 +            ExecutableMemberDoc member = (ExecutableMemberDoc) holder;
   1.175 +            Content output = getTagletOutput(false, member, writer,
   1.176 +                member.typeParameters(), member.typeParamTags());
   1.177 +            output.addContent(getTagletOutput(true, member, writer,
   1.178 +                member.parameters(), member.paramTags()));
   1.179 +            return output;
   1.180 +        } else {
   1.181 +            ClassDoc classDoc = (ClassDoc) holder;
   1.182 +            return getTagletOutput(false, classDoc, writer,
   1.183 +                classDoc.typeParameters(), classDoc.typeParamTags());
   1.184 +        }
   1.185 +    }
   1.186 +
   1.187 +    /**
   1.188 +     * Given an array of <code>ParamTag</code>s,return its string representation.
   1.189 +     * Try to inherit the param tags that are missing.
   1.190 +     *
   1.191 +     * @param holder            the doc that holds the param tags.
   1.192 +     * @param writer            the TagletWriter that will write this tag.
   1.193 +     * @param formalParameters  The array of parmeters (from type or executable
   1.194 +     *                          member) to check.
   1.195 +     *
   1.196 +     * @return the TagletOutput representation of these <code>ParamTag</code>s.
   1.197 +     */
   1.198 +    private Content getTagletOutput(boolean isNonTypeParams, Doc holder,
   1.199 +            TagletWriter writer, Object[] formalParameters, ParamTag[] paramTags) {
   1.200 +        Content result = writer.getOutputInstance();
   1.201 +        Set<String> alreadyDocumented = new HashSet<String>();
   1.202 +        if (paramTags.length > 0) {
   1.203 +            result.addContent(
   1.204 +                processParamTags(isNonTypeParams, paramTags,
   1.205 +                getRankMap(formalParameters), writer, alreadyDocumented)
   1.206 +            );
   1.207 +        }
   1.208 +        if (alreadyDocumented.size() != formalParameters.length) {
   1.209 +            //Some parameters are missing corresponding @param tags.
   1.210 +            //Try to inherit them.
   1.211 +            result.addContent(getInheritedTagletOutput (isNonTypeParams, holder,
   1.212 +                writer, formalParameters, alreadyDocumented));
   1.213 +        }
   1.214 +        return result;
   1.215 +    }
   1.216 +
   1.217 +    /**
   1.218 +     * Loop through each indivitual parameter.  It it does not have a
   1.219 +     * corresponding param tag, try to inherit it.
   1.220 +     */
   1.221 +    private Content getInheritedTagletOutput(boolean isNonTypeParams, Doc holder,
   1.222 +            TagletWriter writer, Object[] formalParameters,
   1.223 +            Set<String> alreadyDocumented) {
   1.224 +        Content result = writer.getOutputInstance();
   1.225 +        if ((! alreadyDocumented.contains(null)) &&
   1.226 +                holder instanceof MethodDoc) {
   1.227 +            for (int i = 0; i < formalParameters.length; i++) {
   1.228 +                if (alreadyDocumented.contains(String.valueOf(i))) {
   1.229 +                    continue;
   1.230 +                }
   1.231 +                //This parameter does not have any @param documentation.
   1.232 +                //Try to inherit it.
   1.233 +                DocFinder.Output inheritedDoc =
   1.234 +                    DocFinder.search(new DocFinder.Input((MethodDoc) holder, this,
   1.235 +                        String.valueOf(i), ! isNonTypeParams));
   1.236 +                if (inheritedDoc.inlineTags != null &&
   1.237 +                        inheritedDoc.inlineTags.length > 0) {
   1.238 +                    result.addContent(
   1.239 +                        processParamTag(isNonTypeParams, writer,
   1.240 +                            (ParamTag) inheritedDoc.holderTag,
   1.241 +                            isNonTypeParams ?
   1.242 +                                ((Parameter) formalParameters[i]).name():
   1.243 +                                ((TypeVariable) formalParameters[i]).typeName(),
   1.244 +                            alreadyDocumented.size() == 0));
   1.245 +                }
   1.246 +                alreadyDocumented.add(String.valueOf(i));
   1.247 +            }
   1.248 +        }
   1.249 +        return result;
   1.250 +    }
   1.251 +
   1.252 +    /**
   1.253 +     * Given an array of <code>Tag</code>s representing this custom
   1.254 +     * tag, return its string representation.  Print a warning for param
   1.255 +     * tags that do not map to parameters.  Print a warning for param
   1.256 +     * tags that are duplicated.
   1.257 +     *
   1.258 +     * @param paramTags the array of <code>ParamTag</code>s to convert.
   1.259 +     * @param writer the TagletWriter that will write this tag.
   1.260 +     * @param alreadyDocumented the set of exceptions that have already
   1.261 +     *        been documented.
   1.262 +     * @param rankMap a {@link java.util.Map} which holds ordering
   1.263 +     *                    information about the parameters.
   1.264 +     * @param rankMap a {@link java.util.Map} which holds a mapping
   1.265 +     *                of a rank of a parameter to its name.  This is
   1.266 +     *                used to ensure that the right name is used
   1.267 +     *                when parameter documentation is inherited.
   1.268 +     * @return the Content representation of this <code>Tag</code>.
   1.269 +     */
   1.270 +    private Content processParamTags(boolean isNonTypeParams,
   1.271 +            ParamTag[] paramTags, Map<String, String> rankMap, TagletWriter writer,
   1.272 +            Set<String> alreadyDocumented) {
   1.273 +        Content result = writer.getOutputInstance();
   1.274 +        if (paramTags.length > 0) {
   1.275 +            for (int i = 0; i < paramTags.length; ++i) {
   1.276 +                ParamTag pt = paramTags[i];
   1.277 +                String paramName = isNonTypeParams ?
   1.278 +                    pt.parameterName() : "<" + pt.parameterName() + ">";
   1.279 +                if (! rankMap.containsKey(pt.parameterName())) {
   1.280 +                    writer.getMsgRetriever().warning(pt.position(),
   1.281 +                        isNonTypeParams ?
   1.282 +                            "doclet.Parameters_warn" :
   1.283 +                            "doclet.Type_Parameters_warn",
   1.284 +                        paramName);
   1.285 +                }
   1.286 +                String rank = rankMap.get(pt.parameterName());
   1.287 +                if (rank != null && alreadyDocumented.contains(rank)) {
   1.288 +                    writer.getMsgRetriever().warning(pt.position(),
   1.289 +                       isNonTypeParams ?
   1.290 +                           "doclet.Parameters_dup_warn" :
   1.291 +                           "doclet.Type_Parameters_dup_warn",
   1.292 +                       paramName);
   1.293 +                }
   1.294 +                result.addContent(processParamTag(isNonTypeParams, writer, pt,
   1.295 +                     pt.parameterName(), alreadyDocumented.size() == 0));
   1.296 +                alreadyDocumented.add(rank);
   1.297 +            }
   1.298 +        }
   1.299 +        return result;
   1.300 +    }
   1.301 +    /**
   1.302 +     * Convert the individual ParamTag into Content.
   1.303 +     *
   1.304 +     * @param isNonTypeParams true if this is just a regular param tag.  False
   1.305 +     *                        if this is a type param tag.
   1.306 +     * @param writer          the taglet writer for output writing.
   1.307 +     * @param paramTag        the tag whose inline tags will be printed.
   1.308 +     * @param name            the name of the parameter.  We can't rely on
   1.309 +     *                        the name in the param tag because we might be
   1.310 +     *                        inheriting documentation.
   1.311 +     * @param isFirstParam    true if this is the first param tag being printed.
   1.312 +     *
   1.313 +     */
   1.314 +    private Content processParamTag(boolean isNonTypeParams,
   1.315 +            TagletWriter writer, ParamTag paramTag, String name,
   1.316 +            boolean isFirstParam) {
   1.317 +        Content result = writer.getOutputInstance();
   1.318 +        String header = writer.configuration().getText(
   1.319 +            isNonTypeParams ? "doclet.Parameters" : "doclet.TypeParameters");
   1.320 +        if (isFirstParam) {
   1.321 +            result.addContent(writer.getParamHeader(header));
   1.322 +        }
   1.323 +        result.addContent(writer.paramTagOutput(paramTag,
   1.324 +            name));
   1.325 +        return result;
   1.326 +    }
   1.327 +}

mercurial