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