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