views:

627

answers:

4

Is there a way to reference one of the method parameters from the method documentation body? Something like:

/**
 * 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)
{...}
+2  A: 

That's not possible with standard Javadoc tool. You can reference classes, methods and fields, but not method arguments. I however question the value if it was possible anyway.

BalusC
Refactoring-aware. Renaming a parameter should change copies of it in the documentation (and textual match is ugly and error prone).
ripper234
This is one of my nits w/ JavaDoc and the one place I prefer the otherwise (IMHO) inferior doc system from .NET
Hank Gay
Interesting. But how often do you rename names after the code is brought into production? Sun for example doesn't, they just deprecate them, also see the well known Url >> URL change.
BalusC
+2  A: 

I guess you could write your own doclet or taglet to support this behaviour.

Taglet Overview

Doclet Overview

jitter
+2  A: 

As you can see in the Java Source of the java.lang.String class:

/**
 * 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);
}

Parameter references are surrounded by <code></code> tags, which means that the Javadoc syntax does not provide any way to do such a thing. (I think String.class is a good example of javadoc usage).

Lastnico
+2  A: 

Confirmed that this is one of the dozens of useful features that Javadoc lacks.

However, don't use <code>foo</code> as recommended above; you can use {@code foo}. This is especially good to know when you refer to a generic type such as {@code Iterator<String>} -- sure looks nicer than <code>Iterator&lt;String&gt;</code>, doesn't it!

Kevin Bourrillion