关于java:编辑Eclipse Javadoc $ {tags}变量

Editing Eclipse Javadoc ${tags} Variable

版本:Luna Service Release 2(4.4.2)

我通常使用" / **"方法在我的方法上插入Javadoc。 Eclipse为所有args插入@param,为所有throwables插入@throws,并为@return。 但是,@return永远不会附加类型。 看起来像这样:

1
2
3
4
5
6
7
8
/**
 *
 * @param criteria
 * @param filters
 * @return
 */

protected static String
          getColumnNameFromCriteria(SelectedCriteria criteria, List<SelectionFilter> filters)

第一个问题是:在Eclipse中是否有一个开关可以使它在添加Javadoc时自动插入方法返回类型?

我找不到一个,于是我抬起头来:偏好设置-> java->代码样式->代码模板->方法

在该模板上,我看到了变量${tags}。 该变量将生成上面显示的Javadoc。

第二个问题是:有没有一种方法可以编辑${tags}以包括附加到${tags}生成的@return的变量${return_type}

我希望能够键入/**并让Eclipse自动创建以下Javadoc:

1
2
3
4
5
6
7
8
/**
 *
 * @param criteria
 * @param filters
 * @return String
 */

protected static String
          getColumnNameFromCriteria(SelectedCriteria criteria, List<SelectionFilter> filters)


您尝试过jautodoc插件吗?看看它可能会帮助您。它比alt-shift-j中内置的eclipse更好

http://jautodoc.sourceforge.net/


${tags}变量在Eclipse中似乎不可编辑。看完一些代码之后,这里是负责解析变量的类的链接。特别是insertTag方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private static void insertTag(IDocument textBuffer, int offset, int length, String[] paramNames, String[] exceptionNames, String returnType, String[] typeParameterNames, boolean isDeprecated,
        String lineDelimiter) throws BadLocationException {
    IRegion region= textBuffer.getLineInformationOfOffset(offset);
    if (region == null) {
        return;
    }
    String lineStart= textBuffer.get(region.getOffset(), offset - region.getOffset());

    StringBuffer buf= new StringBuffer();
    for (int i= 0; i < typeParameterNames.length; i++) {
        if (buf.length() > 0) {
            buf.append(lineDelimiter).append(lineStart);
        }
        buf.append("@param <").append(typeParameterNames[i]).append('>'); //$NON-NLS-1$
    }
    for (int i= 0; i < paramNames.length; i++) {
        if (buf.length() > 0) {
            buf.append(lineDelimiter).append(lineStart);
        }
        buf.append("@param").append(paramNames[i]); //$NON-NLS-1$
    }
    if (returnType != null && !returnType.equals("void")) { //$NON-NLS-1$
        if (buf.length() > 0) {
            buf.append(lineDelimiter).append(lineStart);
        }
        buf.append("@return"); //$NON-NLS-1$
    }
    if (exceptionNames != null) {
        for (int i= 0; i < exceptionNames.length; i++) {
            if (buf.length() > 0) {
                buf.append(lineDelimiter).append(lineStart);
            }
            buf.append("@throws").append(exceptionNames[i]); //$NON-NLS-1$
        }
    }

    ...

请注意,没有任何方法可以附加返回类型。模板中仅插入@return

至少有一种非常怪异的方法。您仍然可以通过以下方法更新模板:转到窗口->首选项-> Java->代码样式->代码模板->注释,然后选择编辑以编辑注释模板。然后,您可以将模板更改为:

1
2
3
4
/**
 * ${tags}
 * @return ${return_type}
 */

有关可用变量,请参见http://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.user/concepts/concept-template-variables.htm。

但这会导致添加两个@return注释。如另一个答案所述,不需要添加返回类型,因为Javadoc生成器可以自动确定返回类型。如果您使用其他工具解析注释,则上述解决方法可以解决该问题。


根据您的问题,Eclipse中尚未配置(允许您修改此配置)。这是因为javadoc中的注释必须使用自动生成类javadoc HTML的工具进行投诉。如果允许更改它,则javadoc工具还应该具有配置以了解该修改。

您不必担心,因为此标记@return是针对Javadoc生成的。生成项目javadoc后,您将看到每个方法都有返回类型(在生成的html中)。该标记用于方法结果的特定描述。

采取这种方法,例如:

1
2
3
4
5
6
7
8
9
/**
 * This is an example of a documentation of a method with a return type.
 * Note that there isn't a type on the `@return` tag
 *
 * @return the someAttribute
 */

public String getSomeAttribute() {
    return someAttribute;
}

在Eclipse中,当您将鼠标指针停在此方法上时,它将显示以下内容:
enter image description here

请注意图像上该文档的第一行。它说:String Foo.getSomeAttribute()

当您通过javadoc工具或其他工具(例如Maven)生成Javadoc时,它将使用类的所有javadoc生成所有HTML文件,并且方法摘要将类似于此(String类)
enter image description here

您可以在"修饰符和类型"列中看到方法的返回类型。如果查看其中一个方法的源代码,例如图像charAt(int index)中的第一个方法,您会发现@return标记中没有类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 * Returns the <wyn>char</wyn> value at the
 * specified index. An index ranges from <wyn>0</wyn> to
 * <wyn>length() - 1</wyn>. The first <wyn>char</wyn> value of the sequence
 * is at index <wyn>0</wyn>, the next at index <wyn>1</wyn>,
 * and so on, as for array indexing.
 *
 * <p>
If the <wyn>char</wyn> value specified by the index is a
 * surrogate, the surrogate
 * value is returned.
 *
 * @param      index   the index of the <wyn>char</wyn> value.
 * @return     the <wyn>char</wyn> value at the specified index of this string.
 *             The first <wyn>char</wyn> value is at index <wyn>0</wyn>.
 * @exception  IndexOutOfBoundsException  if the <wyn>index</wyn>
 *             argument is negative or not less than the length of this
 *             string.
 */

public char charAt(int index) {
    if ((index < 0) || (index >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index];
}

希望它可以帮助您了解该标签。