【问题标题】:How to add reference to a method parameter in javadoc?如何在javadoc中添加对方法参数的引用?
【发布时间】:2010-12-12 15:19:49
【问题描述】:

有没有办法从方法文档正文中添加对一个或多个方法参数的引用? 比如:

/**
 * When {@paramref a} is null, we rely on b for the discombobulation.
 *
 * @param a this is one of the parameters
 * @param b another param
 */
void foo(String a, int b)
{...}

【问题讨论】:

    标签: java arguments javadoc


    【解决方案1】:

    正如您在 java.lang.String 类的 Java 源代码中看到的:

    /**
     * Allocates a new <code>String</code> that contains characters from
     * a subarray of the character array argument. The <code>offset</code>
     * argument is the index of the first character of the subarray and
     * the <code>count</code> argument specifies the length of the
     * subarray. The contents of the subarray are copied; subsequent
     * modification of the character array does not affect the newly
     * created string.
     *
     * @param      value    array that is the source of characters.
     * @param      offset   the initial offset.
     * @param      count    the length.
     * @exception  IndexOutOfBoundsException  if the <code>offset</code>
     *               and <code>count</code> arguments index characters outside
     *               the bounds of the <code>value</code> array.
     */
    public String(char value[], int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }
    
        this.value = new char[count];
        this.count = count;
        System.arraycopy(value, offset, this.value, 0, count);
    }
    

    参数引用被&lt;code&gt;&lt;/code&gt;标签包围,这意味着Javadoc语法没有提供任何方法来做这样的事情。 (我认为 String.class 是 javadoc 使用的一个很好的例子)。

    【讨论】:

    • 标签未引用特定参数。它将“字符串”一词格式化为“代码外观”文本。
    • @Naxos84 对于第一个 标记是正确的,但在 javadoc 中他们进一步引用了由 标记包围的 offsetcount 参数
    【解决方案2】:

    引用方法参数的正确方式是这样的:

    【讨论】:

    • 它不仅回答了问题,而且直观地解释了如何使用 Intellij 等 IDE 修改 Javadoc 的参数。这对于正在寻找答案的搜索者很有用。
    • 在 Eclipse 上它不起作用。但这仍然是一个不错的答案
    • 这应该被删除。想象不再存在。
    • @user4504267 图片看起来不错,至少现在是这样。
    • 尝试重构参数名称,intellij 不会更新此代码块。
    【解决方案3】:

    据我在阅读the docs for javadoc 后所知,没有这样的功能。

    不要像其他答案中推荐的那样使用&lt;code&gt;foo&lt;/code&gt;;你可以使用{@code foo}。当您引用诸如{@code Iterator&lt;String&gt;} 之类的泛型类型时,知道这一点尤其好——当然看起来比&lt;code&gt;Iterator&amp;lt;String&amp;gt;&lt;/code&gt; 更好,不是吗!

    【讨论】:

    【解决方案4】:

    我猜你可以编写自己的 doclet 或 taglet 来支持这种行为。

    Taglet Overview

    Doclet Overview

    【讨论】:

    • 然后向 javadoc 发出拉取请求 :)
    猜你喜欢
    • 2016-11-15
    • 2011-08-20
    • 2011-07-12
    • 2014-05-05
    • 1970-01-01
    • 2013-01-01
    • 2020-04-12
    • 2023-03-20
    • 2017-07-31
    相关资源
    最近更新 更多