简体   繁体   English

编辑Eclipse Javadoc $ {tags}变量

[英]Editing Eclipse Javadoc ${tags} Variable

Version: Luna Service Release 2 (4.4.2) 版本:Luna Service Release 2(4.4.2)

I typically use the "/**" method to insert Javadoc on my methods. 我通常使用“ / **”方法在我的方法上插入Javadoc。 Eclipse inserts @param for the all the args, @throws for all the throwables, and a @return . Eclipse为所有的args插入@param ,为所有的throwable插入@throws ,并为@return插入。 However the @return never has a type appended to it. 但是, @return从未附加任何类型。 It looks like this: 看起来像这样:

/**
 * 
 * @param criteria
 * @param filters
 * @return
 */
protected static String 
          getColumnNameFromCriteria(SelectedCriteria criteria, List<SelectionFilter> filters)

The first question is: is there a switch somewhere in Eclipse to make it automatically insert the method return type when adding Javadoc? 第一个问题是:在Eclipse中是否有一个开关可以使它在添加Javadoc时自动插入方法返回类型?

I could not find one, so I looked up: preferences->java->code style->code templates->Methods 我找不到一个,于是我抬起头来:偏好设置-> java->代码样式->代码模板->方法

On that template I see a variable ${tags} . 在该模板上,我看到了一个变量${tags} That variable is what generates the Javadoc shown above. 该变量将生成上面显示的Javadoc。

The second question is: is there a way to edit ${tags} to include the variable ${return_type} appended to @return that is generated by ${tags} ? 第二个问题是:有没有一种方法可以编辑${tags}以包括附加到${return_type}的变量${return_type}该变量由${tags}生成?

I want to be able to type /**<enter> and have Eclipse automatically create the following Javadoc: 我希望能够输入/**<enter>并让Eclipse自动创建以下Javadoc:

/**
 * 
 * @param criteria
 * @param filters
 * @return String 
 */
protected static String 
          getColumnNameFromCriteria(SelectedCriteria criteria, List<SelectionFilter> filters)

Have you tried jautodoc plugin? 您尝试过jautodoc插件吗? have a look in to it may be it will help you.its better than eclipse built in alt-shift-j 看看它可能会帮助您。它比alt-shift-j中内置的eclipse更好

http://jautodoc.sourceforge.net/ http://jautodoc.sourceforge.net/

As per your question, there is no configuration in Eclipse (yet) that allows you to modify this. 根据您的问题,Eclipse中尚未配置(允许您修改此配置)。 That is because the comments in javadoc must complain with the tool that automatic generates the classes javadoc HTML. 这是因为javadoc中的注释必须使用自动生成类javadoc HTML的工具进行投诉。 If one was allowed to change that, the javadoc tool should also have a configuration to understand that modifications. 如果允许更改它,则javadoc工具还应该具有配置以了解该修改。

You don't need to worry about that since this tag @return is meant to a javadoc generation. 您不必担心,因为此标记@return是针对Javadoc生成的。 Once you generate your project javadoc you will see that every method will have the return type (in the generated html). 生成项目javadoc之后,您将看到每个方法都有返回类型(在生成的html中)。 That tag is for a specific DESCRIPTION of the result of method. 该标记用于方法结果的特定描述。

Take this method exmaple: 采取这种方法,例如:

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

When, in Eclipse, you stop mouse pointer over this method it will show this: 在Eclipse中,当您将鼠标指针停在此方法上时,它将显示以下内容: 在此处输入图片说明

Pay attention to the very first line of that docs on the image. 请注意图像上该文档的第一行。 It says: String Foo.getSomeAttribute() 它说: String Foo.getSomeAttribute()

When you generate the Javadoc via javadoc tool or another tool like Maven it willl generate all HTML files with all javadocs of your classes and the Method summary will be like this one ( String class ) 当您通过javadoc工具或其他工具(如Maven生成Javadoc时,它将使用您的类的所有javadoc生成所有HTML文件,并且方法摘要将与此类似( String类 在此处输入图片说明

You can see at the "Modifier and type" column the return type of the methods. 您可以在“修饰符和类型”列中看到方法的返回类型。 If you look at the source code of one of those methods like the first one in the image charAt(int index) you wil see that there is no type in the @return tag. 如果您查看其中一种方法的源代码,例如图像charAt(int index)的第一种方法,您会发现@return标记中没有类型。

/**
 * Returns the <code>char</code> value at the
 * specified index. An index ranges from <code>0</code> to
 * <code>length() - 1</code>. The first <code>char</code> value of the sequence
 * is at index <code>0</code>, the next at index <code>1</code>,
 * and so on, as for array indexing.
 *
 * <p>If the <code>char</code> value specified by the index is a
 * <a href="Character.html#unicode">surrogate</a>, the surrogate
 * value is returned.
 *
 * @param      index   the index of the <code>char</code> value.
 * @return     the <code>char</code> value at the specified index of this string.
 *             The first <code>char</code> value is at index <code>0</code>.
 * @exception  IndexOutOfBoundsException  if the <code>index</code>
 *             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];
}

I hope it helps you to understand that tag. 希望它可以帮助您了解该标签。

The ${tags} variable doesn't seem to be editable in Eclipse. ${tags}变量在Eclipse中似乎不可编辑。 After going through some of the code, here's a link to the class responsible for resolving the variable. 看完一些代码之后,这里是负责解析变量的类的链接 Specifically the insertTag method: 具体来说是insertTag方法:

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$
        }
    }

    ...

Notice that there is no way to append the return type. 请注意,没有任何方法可以附加返回类型。 Only @return is inserted in the template. 模板中仅插入@return

There is at least a very hacky way to do it. 至少有一种非常怪异的方法。 You can still update the template by going to Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments , and selecting Edit to edit the comment template. 您仍然可以通过以下方法更新模板:转到窗口->首选项-> Java->代码样式->代码模板->注释 ,然后选择编辑以编辑注释模板。 You can then change the template to: 然后,您可以将模板更改为:

/**
 * ${tags}
 * @return ${return_type}
 */

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

But this causes two @return comments to be added. 但这会导致添加两个@return注释。 As mentioned in the other answer, adding a return type is not needed as the Javadoc generator can automatically determine the return type. 如另一个答案所述,不需要添加返回类型,因为Javadoc生成器可以自动确定返回类型。 If you're parsing the comments in some other tool, the above workaround could solve it though. 如果您使用其他工具解析注释,则上述解决方法可以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM