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

Tue, 14 May 2013 10:14:57 -0700

author
jjg
date
Tue, 14 May 2013 10:14:57 -0700
changeset 1751
ca8808c88f94
parent 1724
d918b63a5509
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8012308: Remove TagletOutput in favor of direct use of Content
Reviewed-by: darcy

duke@1 1 /*
jjg@1724 2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.doclets.internal.toolkit.taglets;
duke@1 27
jjg@1357 28 import java.util.*;
jjg@1357 29
duke@1 30 import com.sun.javadoc.*;
jjg@1751 31 import com.sun.tools.doclets.internal.toolkit.Content;
duke@1 32 import com.sun.tools.doclets.internal.toolkit.util.*;
duke@1 33
duke@1 34 /**
duke@1 35 * A taglet that represents the @param tag.
duke@1 36 *
jjg@1359 37 * <p><b>This is NOT part of any supported API.
jjg@1359 38 * If you write code that depends on this, you do so at your own risk.
jjg@1359 39 * This code and its internal interfaces are subject to change or
jjg@1359 40 * deletion without notice.</b>
duke@1 41 *
duke@1 42 * @author Jamie Ho
duke@1 43 * @since 1.4
duke@1 44 */
duke@1 45 public class ParamTaglet extends BaseTaglet implements InheritableTaglet {
duke@1 46
duke@1 47 /**
duke@1 48 * Construct a ParamTaglet.
duke@1 49 */
duke@1 50 public ParamTaglet() {
duke@1 51 name = "param";
duke@1 52 }
duke@1 53
duke@1 54 /**
duke@1 55 * Given an array of <code>Parameter</code>s, return
duke@1 56 * a name/rank number map. If the array is null, then
duke@1 57 * null is returned.
duke@1 58 * @param params The array of parmeters (from type or executable member) to
duke@1 59 * check.
duke@1 60 * @return a name-rank number map.
duke@1 61 */
jjg@74 62 private static Map<String,String> getRankMap(Object[] params){
duke@1 63 if (params == null) {
duke@1 64 return null;
duke@1 65 }
jjg@74 66 HashMap<String,String> result = new HashMap<String,String>();
duke@1 67 for (int i = 0; i < params.length; i++) {
duke@1 68 String name = params[i] instanceof Parameter ?
duke@1 69 ((Parameter) params[i]).name() :
duke@1 70 ((TypeVariable) params[i]).typeName();
duke@1 71 result.put(name, String.valueOf(i));
duke@1 72 }
duke@1 73 return result;
duke@1 74 }
duke@1 75
duke@1 76 /**
duke@1 77 * {@inheritDoc}
duke@1 78 */
duke@1 79 public void inherit(DocFinder.Input input, DocFinder.Output output) {
duke@1 80 if (input.tagId == null) {
duke@1 81 input.isTypeVariableParamTag = ((ParamTag) input.tag).isTypeParameter();
duke@1 82 Object[] parameters = input.isTypeVariableParamTag ?
duke@1 83 (Object[]) ((MethodDoc) input.tag.holder()).typeParameters() :
duke@1 84 (Object[]) ((MethodDoc) input.tag.holder()).parameters();
duke@1 85 String target = ((ParamTag) input.tag).parameterName();
duke@1 86 int i;
duke@1 87 for (i = 0; i < parameters.length; i++) {
duke@1 88 String name = parameters[i] instanceof Parameter ?
duke@1 89 ((Parameter) parameters[i]).name() :
duke@1 90 ((TypeVariable) parameters[i]).typeName();
duke@1 91 if (name.equals(target)) {
duke@1 92 input.tagId = String.valueOf(i);
duke@1 93 break;
duke@1 94 }
duke@1 95 }
duke@1 96 if (i == parameters.length) {
duke@1 97 //Someone used {@inheritDoc} on an invalid @param tag.
duke@1 98 //We don't know where to inherit from.
duke@1 99 //XXX: in the future when Configuration is available here,
duke@1 100 //print a warning for this mistake.
duke@1 101 return;
duke@1 102 }
duke@1 103 }
duke@1 104 ParamTag[] tags = input.isTypeVariableParamTag ?
jjg@1724 105 ((MethodDoc)input.element).typeParamTags() : ((MethodDoc)input.element).paramTags();
mcimadamore@184 106 Map<String, String> rankMap = getRankMap(input.isTypeVariableParamTag ?
jjg@1724 107 (Object[]) ((MethodDoc)input.element).typeParameters() :
jjg@1724 108 (Object[]) ((MethodDoc)input.element).parameters());
duke@1 109 for (int i = 0; i < tags.length; i++) {
duke@1 110 if (rankMap.containsKey(tags[i].parameterName()) &&
duke@1 111 rankMap.get(tags[i].parameterName()).equals((input.tagId))) {
jjg@1724 112 output.holder = input.element;
duke@1 113 output.holderTag = tags[i];
duke@1 114 output.inlineTags = input.isFirstSentence ?
duke@1 115 tags[i].firstSentenceTags() : tags[i].inlineTags();
duke@1 116 return;
duke@1 117 }
duke@1 118 }
duke@1 119 }
duke@1 120
duke@1 121 /**
duke@1 122 * {@inheritDoc}
duke@1 123 */
duke@1 124 public boolean inField() {
duke@1 125 return false;
duke@1 126 }
duke@1 127
duke@1 128 /**
duke@1 129 * {@inheritDoc}
duke@1 130 */
duke@1 131 public boolean inMethod() {
duke@1 132 return true;
duke@1 133 }
duke@1 134
duke@1 135 /**
duke@1 136 * {@inheritDoc}
duke@1 137 */
duke@1 138 public boolean inOverview() {
duke@1 139 return false;
duke@1 140 }
duke@1 141
duke@1 142 /**
duke@1 143 * {@inheritDoc}
duke@1 144 */
duke@1 145 public boolean inPackage() {
duke@1 146 return false;
duke@1 147 }
duke@1 148
duke@1 149 /**
duke@1 150 * {@inheritDoc}
duke@1 151 */
duke@1 152 public boolean inType() {
duke@1 153 return true;
duke@1 154 }
duke@1 155
duke@1 156 /**
duke@1 157 * {@inheritDoc}
duke@1 158 */
duke@1 159 public boolean isInlineTag() {
duke@1 160 return false;
duke@1 161 }
duke@1 162
duke@1 163 /**
duke@1 164 * Given an array of <code>ParamTag</code>s,return its string representation.
duke@1 165 * @param holder the member that holds the param tags.
duke@1 166 * @param writer the TagletWriter that will write this tag.
duke@1 167 * @return the TagletOutput representation of these <code>ParamTag</code>s.
duke@1 168 */
jjg@1751 169 public Content getTagletOutput(Doc holder, TagletWriter writer) {
duke@1 170 if (holder instanceof ExecutableMemberDoc) {
duke@1 171 ExecutableMemberDoc member = (ExecutableMemberDoc) holder;
jjg@1751 172 Content output = getTagletOutput(false, member, writer,
duke@1 173 member.typeParameters(), member.typeParamTags());
jjg@1751 174 output.addContent(getTagletOutput(true, member, writer,
duke@1 175 member.parameters(), member.paramTags()));
duke@1 176 return output;
duke@1 177 } else {
duke@1 178 ClassDoc classDoc = (ClassDoc) holder;
duke@1 179 return getTagletOutput(false, classDoc, writer,
duke@1 180 classDoc.typeParameters(), classDoc.typeParamTags());
duke@1 181 }
duke@1 182 }
duke@1 183
duke@1 184 /**
duke@1 185 * Given an array of <code>ParamTag</code>s,return its string representation.
duke@1 186 * Try to inherit the param tags that are missing.
duke@1 187 *
jjg@1358 188 * @param holder the doc that holds the param tags.
duke@1 189 * @param writer the TagletWriter that will write this tag.
duke@1 190 * @param formalParameters The array of parmeters (from type or executable
duke@1 191 * member) to check.
duke@1 192 *
duke@1 193 * @return the TagletOutput representation of these <code>ParamTag</code>s.
duke@1 194 */
jjg@1751 195 private Content getTagletOutput(boolean isNonTypeParams, Doc holder,
duke@1 196 TagletWriter writer, Object[] formalParameters, ParamTag[] paramTags) {
jjg@1751 197 Content result = writer.getOutputInstance();
jjg@74 198 Set<String> alreadyDocumented = new HashSet<String>();
duke@1 199 if (paramTags.length > 0) {
jjg@1751 200 result.addContent(
duke@1 201 processParamTags(isNonTypeParams, paramTags,
duke@1 202 getRankMap(formalParameters), writer, alreadyDocumented)
duke@1 203 );
duke@1 204 }
duke@1 205 if (alreadyDocumented.size() != formalParameters.length) {
duke@1 206 //Some parameters are missing corresponding @param tags.
duke@1 207 //Try to inherit them.
jjg@1751 208 result.addContent(getInheritedTagletOutput (isNonTypeParams, holder,
duke@1 209 writer, formalParameters, alreadyDocumented));
duke@1 210 }
duke@1 211 return result;
duke@1 212 }
duke@1 213
duke@1 214 /**
duke@1 215 * Loop through each indivitual parameter. It it does not have a
duke@1 216 * corresponding param tag, try to inherit it.
duke@1 217 */
jjg@1751 218 private Content getInheritedTagletOutput(boolean isNonTypeParams, Doc holder,
duke@1 219 TagletWriter writer, Object[] formalParameters,
jjg@74 220 Set<String> alreadyDocumented) {
jjg@1751 221 Content result = writer.getOutputInstance();
duke@1 222 if ((! alreadyDocumented.contains(null)) &&
duke@1 223 holder instanceof MethodDoc) {
duke@1 224 for (int i = 0; i < formalParameters.length; i++) {
duke@1 225 if (alreadyDocumented.contains(String.valueOf(i))) {
duke@1 226 continue;
duke@1 227 }
duke@1 228 //This parameter does not have any @param documentation.
duke@1 229 //Try to inherit it.
duke@1 230 DocFinder.Output inheritedDoc =
duke@1 231 DocFinder.search(new DocFinder.Input((MethodDoc) holder, this,
duke@1 232 String.valueOf(i), ! isNonTypeParams));
duke@1 233 if (inheritedDoc.inlineTags != null &&
duke@1 234 inheritedDoc.inlineTags.length > 0) {
jjg@1751 235 result.addContent(
duke@1 236 processParamTag(isNonTypeParams, writer,
duke@1 237 (ParamTag) inheritedDoc.holderTag,
duke@1 238 isNonTypeParams ?
duke@1 239 ((Parameter) formalParameters[i]).name():
duke@1 240 ((TypeVariable) formalParameters[i]).typeName(),
duke@1 241 alreadyDocumented.size() == 0));
duke@1 242 }
duke@1 243 alreadyDocumented.add(String.valueOf(i));
duke@1 244 }
duke@1 245 }
duke@1 246 return result;
duke@1 247 }
duke@1 248
duke@1 249 /**
duke@1 250 * Given an array of <code>Tag</code>s representing this custom
duke@1 251 * tag, return its string representation. Print a warning for param
duke@1 252 * tags that do not map to parameters. Print a warning for param
duke@1 253 * tags that are duplicated.
duke@1 254 *
duke@1 255 * @param paramTags the array of <code>ParamTag</code>s to convert.
duke@1 256 * @param writer the TagletWriter that will write this tag.
duke@1 257 * @param alreadyDocumented the set of exceptions that have already
duke@1 258 * been documented.
duke@1 259 * @param rankMap a {@link java.util.Map} which holds ordering
duke@1 260 * information about the parameters.
jjg@1358 261 * @param rankMap a {@link java.util.Map} which holds a mapping
duke@1 262 * of a rank of a parameter to its name. This is
duke@1 263 * used to ensure that the right name is used
duke@1 264 * when parameter documentation is inherited.
jjg@1751 265 * @return the Content representation of this <code>Tag</code>.
duke@1 266 */
jjg@1751 267 private Content processParamTags(boolean isNonTypeParams,
mcimadamore@184 268 ParamTag[] paramTags, Map<String, String> rankMap, TagletWriter writer,
jjg@74 269 Set<String> alreadyDocumented) {
jjg@1751 270 Content result = writer.getOutputInstance();
duke@1 271 if (paramTags.length > 0) {
duke@1 272 for (int i = 0; i < paramTags.length; ++i) {
duke@1 273 ParamTag pt = paramTags[i];
duke@1 274 String paramName = isNonTypeParams ?
duke@1 275 pt.parameterName() : "<" + pt.parameterName() + ">";
duke@1 276 if (! rankMap.containsKey(pt.parameterName())) {
duke@1 277 writer.getMsgRetriever().warning(pt.position(),
duke@1 278 isNonTypeParams ?
duke@1 279 "doclet.Parameters_warn" :
duke@1 280 "doclet.Type_Parameters_warn",
duke@1 281 paramName);
duke@1 282 }
mcimadamore@184 283 String rank = rankMap.get(pt.parameterName());
duke@1 284 if (rank != null && alreadyDocumented.contains(rank)) {
duke@1 285 writer.getMsgRetriever().warning(pt.position(),
duke@1 286 isNonTypeParams ?
duke@1 287 "doclet.Parameters_dup_warn" :
duke@1 288 "doclet.Type_Parameters_dup_warn",
duke@1 289 paramName);
duke@1 290 }
jjg@1751 291 result.addContent(processParamTag(isNonTypeParams, writer, pt,
duke@1 292 pt.parameterName(), alreadyDocumented.size() == 0));
duke@1 293 alreadyDocumented.add(rank);
duke@1 294 }
duke@1 295 }
duke@1 296 return result;
duke@1 297 }
duke@1 298 /**
jjg@1751 299 * Convert the individual ParamTag into Content.
duke@1 300 *
duke@1 301 * @param isNonTypeParams true if this is just a regular param tag. False
duke@1 302 * if this is a type param tag.
duke@1 303 * @param writer the taglet writer for output writing.
duke@1 304 * @param paramTag the tag whose inline tags will be printed.
duke@1 305 * @param name the name of the parameter. We can't rely on
duke@1 306 * the name in the param tag because we might be
duke@1 307 * inheriting documentation.
duke@1 308 * @param isFirstParam true if this is the first param tag being printed.
duke@1 309 *
duke@1 310 */
jjg@1751 311 private Content processParamTag(boolean isNonTypeParams,
duke@1 312 TagletWriter writer, ParamTag paramTag, String name,
duke@1 313 boolean isFirstParam) {
jjg@1751 314 Content result = writer.getOutputInstance();
duke@1 315 String header = writer.configuration().getText(
duke@1 316 isNonTypeParams ? "doclet.Parameters" : "doclet.TypeParameters");
duke@1 317 if (isFirstParam) {
jjg@1751 318 result.addContent(writer.getParamHeader(header));
duke@1 319 }
jjg@1751 320 result.addContent(writer.paramTagOutput(paramTag,
duke@1 321 name));
duke@1 322 return result;
duke@1 323 }
duke@1 324 }

mercurial