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

Fri, 18 Jun 2010 21:13:56 -0700

author
jjg
date
Fri, 18 Jun 2010 21:13:56 -0700
changeset 589
4177f5bdd189
parent 554
9d9f26857129
child 1357
c75be5bc5283
permissions
-rw-r--r--

6961178: Allow doclet.xml to contain XML attributes
Reviewed-by: bpatel

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

mercurial