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

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

author
jjg
date
Tue, 14 May 2013 10:14:56 -0700
changeset 1750
081d7c72ee92
parent 1724
d918b63a5509
child 1751
ca8808c88f94
permissions
-rw-r--r--

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

mercurial