在 Eclipse 中更改方法签名时如何自动更新 Javadoc?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2260359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:24:39  来源:igfitidea点击:

How can I automatically update the Javadoc when changing the method signature in Eclipse?

javaeclipsejavadoc

提问by Yatendra Goel

I am using Eclipse IDE for my Java Project.

我在 Java 项目中使用 Eclipse IDE。

I have one problem. I have the methods in my project which have the javadoc comments like as follows:

我有一个问题。我的项目中有一些方法,其中的 javadoc 注释如下:

/**
 * Retruns the string representation of a input stream
 * @param in
 * @return
 * @throws IOException
 */
public static String getStringFromInputStream (InputStream in) throws IOException {
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    for (int n; (n = in.read(b)) != -1;) {
        out.append(new String(b, 0, n));
    }
    return out.toString();
}

Now I want to know that Is there any way by which whenever if I make changes in my method's signature, those changes reflect in the javadoc automatically.

现在我想知道有什么方法可以让我在我的方法签名中进行更改时,这些更改会自动反映在 javadoc 中。

采纳答案by Yatendra Goel

Press Atl+Shift+Rand change

按下Atl+Shift+R并改变

回答by Csaba_H

Eclipse provides fairly good options to ensure the correctness of javadoc besides the Renamerefactor JesperE mentioned:

除了提到的Rename重构 JesperE之外,Eclipse 还提供了相当不错的选项来确保 javadoc 的正确性:

  • The Change method signaturerefactor operation also modifies javadoc (add/remove necessary tags). You should use this one or Renameto modify code which are already in use.
  • If you activate Add Javadoc tagson Preferences/Java/Editor/Typingpage then Eclipse generates the correct javadoc stub after typing /**+ Enterbefore a method.
  • 更改方法签名重构操作还修改的javadoc(添加/删除必要的标记)。你应该使用这个或重命名来修改已经在使用的代码。
  • 如果您在Preferences/Java/Editor/Typing页面上激活Add Javadoc 标签,则 Eclipse在方法前键入+后会生成正确的 javadoc 存根。/**Enter

You can also set compiler options to check javadoc missing tags on Preferences/Java/Compiler/Javadoc. In this case you get warnings from the compiler on missing/extra tags and you have quickfix (Ctrl+1) for fixing them. It is a good option to ensure the correctness of existing javadocs in the long run.

您还可以设置编译器选项以检查Preferences/Java/Compiler/Javadoc上的 javadoc 缺失标签。在这种情况下,您会收到编译器关于缺少/额外标签的警告,并且您可以使用 quickfix ( Ctrl+1) 来修复它们。从长远来看,确保现有 javadoc 的正确性是一个不错的选择。

回答by JesperE

I don't know about any way to automatically sync the Javadoc header, but if you rename a parameter using Ctrl-1+ Rename in file, the Javadoc header is appropriately renamed.

我不知道自动同步 Javadoc 标头的任何方法,但是如果您使用Ctrl-1+重命名参数Rename in file,Javadoc 标头将被适当地重命名。

回答by Mat

Refactoring with the "Update references" option is not sufficient. You need to ensure that "Process Javadoc comments" is checked in Window->Preferences, Java->Compiler->Javadoc. Tweak the preference page like you prefer and it will work fine.

使用“更新引用”选项进行重构是不够的。您需要确保在 Window->Preferences、Java->Compiler->Javadoc 中选中“Process Javadoc 注释”。按照您的喜好调整首选项页面,它会正常工作。

回答by Yaza Fatutu

Just press ALT+SHIFT+j on the method name and delete the extra lines:

只需在方法名称上按 ALT+SHIFT+j 并删除多余的行:

BEFORE:

前:

/**
 * Copies all the details from the passed template into the passed new
 * header.
 *
 * @param pNewHeader
 */
private void doCopy(int pNewHeader, int pTemplate) {

AFTER:

后:

/**
 * Copies all the details from the passed template into the passed new
 * header.
 *
 * @param pNewHeader   << DELETE
 */                    << DELETE
/**                    << DELETE
 * @param pNewHeader
 * @param pTemplate
 */
private void doCopy(int pNewHeader, int pTemplate) {

回答by mtk

As I have commented the scenario, in which refactor won't work

正如我所评论的那样,重构将不起作用

All answer refer to refactor option. But what if I added a new param or deleted a param from method signature. In that case, how to update the javadoc?

所有答案均参考重构选项。但是如果我添加了一个新的参数或者从方法签名中删除了一个参数呢?在这种情况下,如何更新 javadoc?

There is a workaround I found, but yes it's still not a automated process and is not good for large number of changes.

我找到了一个解决方法,但是它仍然不是一个自动化过程,并且不适合大量更改。

The workaround is to,
1. remove the javadoc comment and make it plain comment i.e. update the /**' and change it to just/*'.
2. Now once again just above the method signature/declaration enter /**and press enter. It would re-populate the updated params and return info. Now just move the description lines from the older comment to the new.
3. You can use Alt+ Up/Downarrows for achieving this.
4. Done delete the old javadoc comment after it's copied in correct place.

解决方法是,
1. 删除 javadoc 注释并使其成为普通注释,即更新/**' and change it to just/*'。
2. 现在再次在方法签名/声明上方输入/**并按回车键。它会重新填充更新的参数并返回信息。现在只需将描述行从旧评论移动到新评论。
3. 您可以使用Alt+ Up/Down箭头来实现此目的。
4. 将旧的 javadoc 注释复制到正确的位置后,完成删除。