duke@1: /* ohair@554: * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.doclets.internal.toolkit.taglets; duke@1: duke@1: import com.sun.javadoc.*; duke@1: import com.sun.tools.doclets.internal.toolkit.util.*; duke@1: import java.util.*; duke@1: duke@1: /** duke@1: * A taglet that represents the @param tag. duke@1: * duke@1: * This code is not part of an API. duke@1: * It is implementation that is subject to change. duke@1: * Do not use it as an API duke@1: * duke@1: * @author Jamie Ho duke@1: * @since 1.4 duke@1: */ duke@1: public class ParamTaglet extends BaseTaglet implements InheritableTaglet { duke@1: duke@1: /** duke@1: * Construct a ParamTaglet. duke@1: */ duke@1: public ParamTaglet() { duke@1: name = "param"; duke@1: } duke@1: duke@1: /** duke@1: * Given an array of Parameters, return duke@1: * a name/rank number map. If the array is null, then duke@1: * null is returned. duke@1: * @param params The array of parmeters (from type or executable member) to duke@1: * check. duke@1: * @return a name-rank number map. duke@1: */ jjg@74: private static Map getRankMap(Object[] params){ duke@1: if (params == null) { duke@1: return null; duke@1: } jjg@74: HashMap result = new HashMap(); duke@1: for (int i = 0; i < params.length; i++) { duke@1: String name = params[i] instanceof Parameter ? duke@1: ((Parameter) params[i]).name() : duke@1: ((TypeVariable) params[i]).typeName(); duke@1: result.put(name, String.valueOf(i)); duke@1: } duke@1: return result; duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public void inherit(DocFinder.Input input, DocFinder.Output output) { duke@1: if (input.tagId == null) { duke@1: input.isTypeVariableParamTag = ((ParamTag) input.tag).isTypeParameter(); duke@1: Object[] parameters = input.isTypeVariableParamTag ? duke@1: (Object[]) ((MethodDoc) input.tag.holder()).typeParameters() : duke@1: (Object[]) ((MethodDoc) input.tag.holder()).parameters(); duke@1: String target = ((ParamTag) input.tag).parameterName(); duke@1: int i; duke@1: for (i = 0; i < parameters.length; i++) { duke@1: String name = parameters[i] instanceof Parameter ? duke@1: ((Parameter) parameters[i]).name() : duke@1: ((TypeVariable) parameters[i]).typeName(); duke@1: if (name.equals(target)) { duke@1: input.tagId = String.valueOf(i); duke@1: break; duke@1: } duke@1: } duke@1: if (i == parameters.length) { duke@1: //Someone used {@inheritDoc} on an invalid @param tag. duke@1: //We don't know where to inherit from. duke@1: //XXX: in the future when Configuration is available here, duke@1: //print a warning for this mistake. duke@1: return; duke@1: } duke@1: } duke@1: ParamTag[] tags = input.isTypeVariableParamTag ? duke@1: input.method.typeParamTags() : input.method.paramTags(); mcimadamore@184: Map rankMap = getRankMap(input.isTypeVariableParamTag ? duke@1: (Object[]) input.method.typeParameters() : duke@1: (Object[]) input.method.parameters()); duke@1: for (int i = 0; i < tags.length; i++) { duke@1: if (rankMap.containsKey(tags[i].parameterName()) && duke@1: rankMap.get(tags[i].parameterName()).equals((input.tagId))) { duke@1: output.holder = input.method; duke@1: output.holderTag = tags[i]; duke@1: output.inlineTags = input.isFirstSentence ? duke@1: tags[i].firstSentenceTags() : tags[i].inlineTags(); duke@1: return; duke@1: } duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public boolean inField() { duke@1: return false; duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public boolean inMethod() { duke@1: return true; duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public boolean inOverview() { duke@1: return false; duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public boolean inPackage() { duke@1: return false; duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public boolean inType() { duke@1: return true; duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public boolean isInlineTag() { duke@1: return false; duke@1: } duke@1: duke@1: /** duke@1: * Given an array of ParamTags,return its string representation. duke@1: * @param holder the member that holds the param tags. duke@1: * @param writer the TagletWriter that will write this tag. duke@1: * @return the TagletOutput representation of these ParamTags. duke@1: */ duke@1: public TagletOutput getTagletOutput(Doc holder, TagletWriter writer) { duke@1: if (holder instanceof ExecutableMemberDoc) { duke@1: ExecutableMemberDoc member = (ExecutableMemberDoc) holder; duke@1: TagletOutput output = getTagletOutput(false, member, writer, duke@1: member.typeParameters(), member.typeParamTags()); duke@1: output.appendOutput(getTagletOutput(true, member, writer, duke@1: member.parameters(), member.paramTags())); duke@1: return output; duke@1: } else { duke@1: ClassDoc classDoc = (ClassDoc) holder; duke@1: return getTagletOutput(false, classDoc, writer, duke@1: classDoc.typeParameters(), classDoc.typeParamTags()); duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Given an array of ParamTags,return its string representation. duke@1: * Try to inherit the param tags that are missing. duke@1: * duke@1: * @param doc the doc that holds the param tags. duke@1: * @param writer the TagletWriter that will write this tag. duke@1: * @param formalParameters The array of parmeters (from type or executable duke@1: * member) to check. duke@1: * duke@1: * @return the TagletOutput representation of these ParamTags. duke@1: */ duke@1: private TagletOutput getTagletOutput(boolean isNonTypeParams, Doc holder, duke@1: TagletWriter writer, Object[] formalParameters, ParamTag[] paramTags) { duke@1: TagletOutput result = writer.getOutputInstance(); jjg@74: Set alreadyDocumented = new HashSet(); duke@1: if (paramTags.length > 0) { duke@1: result.appendOutput( duke@1: processParamTags(isNonTypeParams, paramTags, duke@1: getRankMap(formalParameters), writer, alreadyDocumented) duke@1: ); duke@1: } duke@1: if (alreadyDocumented.size() != formalParameters.length) { duke@1: //Some parameters are missing corresponding @param tags. duke@1: //Try to inherit them. duke@1: result.appendOutput(getInheritedTagletOutput (isNonTypeParams, holder, duke@1: writer, formalParameters, alreadyDocumented)); duke@1: } duke@1: return result; duke@1: } duke@1: duke@1: /** duke@1: * Loop through each indivitual parameter. It it does not have a duke@1: * corresponding param tag, try to inherit it. duke@1: */ duke@1: private TagletOutput getInheritedTagletOutput(boolean isNonTypeParams, Doc holder, duke@1: TagletWriter writer, Object[] formalParameters, jjg@74: Set alreadyDocumented) { duke@1: TagletOutput result = writer.getOutputInstance(); duke@1: if ((! alreadyDocumented.contains(null)) && duke@1: holder instanceof MethodDoc) { duke@1: for (int i = 0; i < formalParameters.length; i++) { duke@1: if (alreadyDocumented.contains(String.valueOf(i))) { duke@1: continue; duke@1: } duke@1: //This parameter does not have any @param documentation. duke@1: //Try to inherit it. duke@1: DocFinder.Output inheritedDoc = duke@1: DocFinder.search(new DocFinder.Input((MethodDoc) holder, this, duke@1: String.valueOf(i), ! isNonTypeParams)); duke@1: if (inheritedDoc.inlineTags != null && duke@1: inheritedDoc.inlineTags.length > 0) { duke@1: result.appendOutput( duke@1: processParamTag(isNonTypeParams, writer, duke@1: (ParamTag) inheritedDoc.holderTag, duke@1: isNonTypeParams ? duke@1: ((Parameter) formalParameters[i]).name(): duke@1: ((TypeVariable) formalParameters[i]).typeName(), duke@1: alreadyDocumented.size() == 0)); duke@1: } duke@1: alreadyDocumented.add(String.valueOf(i)); duke@1: } duke@1: } duke@1: return result; duke@1: } duke@1: duke@1: /** duke@1: * Given an array of Tags representing this custom duke@1: * tag, return its string representation. Print a warning for param duke@1: * tags that do not map to parameters. Print a warning for param duke@1: * tags that are duplicated. duke@1: * duke@1: * @param paramTags the array of ParamTags to convert. duke@1: * @param writer the TagletWriter that will write this tag. duke@1: * @param alreadyDocumented the set of exceptions that have already duke@1: * been documented. duke@1: * @param rankMap a {@link java.util.Map} which holds ordering duke@1: * information about the parameters. duke@1: * @param nameMap a {@link java.util.Map} which holds a mapping duke@1: * of a rank of a parameter to its name. This is duke@1: * used to ensure that the right name is used duke@1: * when parameter documentation is inherited. duke@1: * @return the TagletOutput representation of this Tag. duke@1: */ duke@1: private TagletOutput processParamTags(boolean isNonTypeParams, mcimadamore@184: ParamTag[] paramTags, Map rankMap, TagletWriter writer, jjg@74: Set alreadyDocumented) { duke@1: TagletOutput result = writer.getOutputInstance(); duke@1: if (paramTags.length > 0) { duke@1: for (int i = 0; i < paramTags.length; ++i) { duke@1: ParamTag pt = paramTags[i]; duke@1: String paramName = isNonTypeParams ? duke@1: pt.parameterName() : "<" + pt.parameterName() + ">"; duke@1: if (! rankMap.containsKey(pt.parameterName())) { duke@1: writer.getMsgRetriever().warning(pt.position(), duke@1: isNonTypeParams ? duke@1: "doclet.Parameters_warn" : duke@1: "doclet.Type_Parameters_warn", duke@1: paramName); duke@1: } mcimadamore@184: String rank = rankMap.get(pt.parameterName()); duke@1: if (rank != null && alreadyDocumented.contains(rank)) { duke@1: writer.getMsgRetriever().warning(pt.position(), duke@1: isNonTypeParams ? duke@1: "doclet.Parameters_dup_warn" : duke@1: "doclet.Type_Parameters_dup_warn", duke@1: paramName); duke@1: } duke@1: result.appendOutput(processParamTag(isNonTypeParams, writer, pt, duke@1: pt.parameterName(), alreadyDocumented.size() == 0)); duke@1: alreadyDocumented.add(rank); duke@1: } duke@1: } duke@1: return result; duke@1: } duke@1: /** duke@1: * Convert the individual ParamTag into TagletOutput. duke@1: * duke@1: * @param isNonTypeParams true if this is just a regular param tag. False duke@1: * if this is a type param tag. duke@1: * @param writer the taglet writer for output writing. duke@1: * @param paramTag the tag whose inline tags will be printed. duke@1: * @param name the name of the parameter. We can't rely on duke@1: * the name in the param tag because we might be duke@1: * inheriting documentation. duke@1: * @param isFirstParam true if this is the first param tag being printed. duke@1: * duke@1: */ duke@1: private TagletOutput processParamTag(boolean isNonTypeParams, duke@1: TagletWriter writer, ParamTag paramTag, String name, duke@1: boolean isFirstParam) { duke@1: TagletOutput result = writer.getOutputInstance(); duke@1: String header = writer.configuration().getText( duke@1: isNonTypeParams ? "doclet.Parameters" : "doclet.TypeParameters"); duke@1: if (isFirstParam) { duke@1: result.appendOutput(writer.getParamHeader(header)); duke@1: } duke@1: result.appendOutput(writer.paramTagOutput(paramTag, duke@1: name)); duke@1: return result; duke@1: } duke@1: }