Java 如何根据Eclipse中的字段注释自动为getter/setter生成注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/996093/
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
How to automatically generate comments for getter/setter based on field comments in Eclipse?
提问by r?ph
I want Eclipse to automatically generate Javadoc comments for my getter and setter methods based on the previously defined comments for the fields. How can I achieve this?
我希望 Eclipse 根据先前定义的字段注释为我的 getter 和 setter 方法自动生成 Javadoc 注释。我怎样才能做到这一点?
Background: A policy in our company is to comment every method and field (even if they have self-explanatory names). So I have to do redundant work by describing the fields and describing the getters / setters again.
背景:我们公司的一项政策是评论每个方法和字段(即使它们有不言自明的名称)。所以我必须通过描述字段并再次描述 getter/setter 来做多余的工作。
Example:
例子:
/**
* name of the dynamic strategy
*/
private String dynName;
/**
* get the name of the dynamic strategy
* @return
*/
public String getDynName() {
return dynName;
}
Searching the web showed that I'm not the only one with the problem - but I couldn't find any solutions. I checked out http://jautodoc.sourceforge.net/but seems like it is not capable of doing this.
搜索网络表明我不是唯一遇到问题的人 - 但我找不到任何解决方案。我查看了http://jautodoc.sourceforge.net/,但似乎无法做到这一点。
采纳答案by r?ph
I finally found a solution (or at least a workaround) myself. I read about Spoonon SO. It's an Java program processor which allows to read and modify java source files. It can even be used as Eclipse Plugin or Ant/Maven script.
我终于自己找到了解决方案(或至少是解决方法)。我在 SO 上阅读了Spoon。它是一个 Java 程序处理器,允许读取和修改 Java 源文件。它甚至可以用作 Eclipse 插件或 Ant/Maven 脚本。
Everything you have to do, is to extend the AbstractProcessor, which will process a method. If the method name starts with get/set it looks for the corresponding field, extracts its comment and replaces or extends the accessors comment with it.
你所要做的就是扩展 AbstractProcessor,它将处理一个方法。如果方法名称以 get/set 开头,它将查找相应的字段,提取其注释并用它替换或扩展访问器注释。
I have a little ant script, which takes all my sources and processes them.
我有一个小蚂蚁脚本,它获取我所有的来源并处理它们。
Something integrated in eclipses code templates would be of course more convenient, but for now this way is ok!
集成在 eclipses 代码模板中的东西当然会更方便,但现在这种方式是可以的!
回答by akf
if you use Eclipse's tool to override/implement methods... from the source menu, there is an option to automatically generate javadoc comments. there are comment templates that you can modify in preferences->java->code style -> code templates -> comments.
如果您使用 Eclipse 的工具来覆盖/实现方法...从源菜单中,有一个选项可以自动生成 javadoc 注释。您可以在首选项->java->代码样式->代码模板->注释中修改注释模板。
回答by flicken
The JavadocWriterplugin for IntelliJ IDEA says it does a "smart copy the javadoc from field to accessor". Caveat utilitor: I haven't tried the plugin myself, and it hasn't been updated in 3 years.
IntelliJ IDEA的JavadocWriter插件说它“将 javadoc 从字段智能复制到访问器”。警告实用程序:我自己没有尝试过该插件,并且已经 3 年没有更新了。
回答by Michael Zilbermann
If you had a macro language, you could write a function like "open a popup that allows me to type in some text, then generates the getter and setter, including its javadoc, based on templates".
如果您有宏语言,您可以编写一个函数,例如“打开一个允许我输入一些文本的弹出窗口,然后基于模板生成 getter 和 setter,包括其 javadoc”。
Eclipse has actually no real support for such a macro language, but maybe you could anyway have a look at : Is there a Macro Recorder for Eclipse?
Eclipse 实际上并没有真正支持这种宏语言,但也许您可以看看:是否有用于 Eclipse 的宏记录器?
If you're not reluctant to switch between eclipse and another tool, then you could try JEdit (jedit.org) that includes powerful beanshell macro language. In such a way, you can have eclipse & jedit opened, you drag&drop the file you want to process from eclipse to jedit, you use jedit macro power, then save the file and finally refresh file within eclipse.
如果您不愿意在 eclipse 和其他工具之间切换,那么您可以尝试包含强大的 beanshell 宏语言的 JEdit (jedit.org)。通过这种方式,您可以打开eclipse和jedit,将要处理的文件从eclipse拖放到jedit,使用jedit宏功能,然后保存文件,最后在eclipse中刷新文件。
It's a bit annoying, but for some processings that's the way I have successfully adopted.
这有点烦人,但对于某些处理,这是我成功采用的方式。
回答by Peter Lawrey
IMHO If the comments can be automatically generated, they don't add much value.
If you called your method getDynamicStrategyName() you won't need to comment it as the name contains all the information you would have put in the comment.
恕我直言,如果评论可以自动生成,它们不会增加太多价值。
如果您调用了您的方法 getDynamicStrategyName(),则无需对其进行注释,因为该名称包含您将要放入注释中的所有信息。
回答by pawelsto
Actually JAutodoc can generate comments for getter/setter based on field comments. You have to check option "Create comment from element name", see http://jautodoc.sourceforge.net/for documentation.
实际上 JAutodoc 可以根据字段注释为 getter/setter 生成注释。您必须选中“根据元素名称创建注释”选项,请参阅http://jautodoc.sourceforge.net/以获取文档。
回答by hideaki
JAutodoc since ver 1.6 (1 year after the question) has a new option "[G,S]etter from field comment", which does exactly what you want.
JAutodoc 自 ver 1.6(问题后 1 年)有一个新选项“[G,S]etter from field comment”,它完全符合您的要求。
This is a quite handy feature. Field comments are usually not included in the final Javadoc HTML because they might/should be private members (generating Javadoc for every private member is not good either), so the comments would be completely lost without it!
这是一个非常方便的功能。字段注释通常不包含在最终的 Javadoc HTML 中,因为它们可能/应该是私有成员(为每个私有成员生成 Javadoc 也不好),因此如果没有它,注释将完全丢失!
I wonder if this Q/A thread might have motivated the author to implement this nice feature.
我想知道这个 Q/A 线程是否可能促使作者实现这个不错的功能。
回答by Stephen Paul
I agree that duplicating documentation is a problem.
What about documenting the private variable and then providing a link to that documentation in the accessor methods?
(Obviously, the following solution is for very simple accessor methods. You don't really want to expose private variable documentation in your API, especially if your accessor method actually does something noteworthy.)
我同意复制文档是一个问题。如何记录私有变量,然后在访问器方法中提供指向该文档的链接?
(显然,以下解决方案适用于非常简单的访问器方法。您真的不想在 API 中公开私有变量文档,尤其是当您的访问器方法确实做了一些值得注意的事情时。)
public class MyBean {
/**
* The names description
*/
private String name;
/**
* @return {@link #name}
*/
public String getName() {
return name;
}
/**
* @param name {@link #name}
*/
public void setName(String name) {
this.name = name;
}
}
Note that, if you're using Eclipse, you may need to enable referencing private variables in your javadocas follows:
请注意,如果您使用 Eclipse,您可能需要在您的 javadoc 中启用引用私有变量,如下所示:
I suspect search performance will be affected, but I haven't yet used tested by how much.
我怀疑搜索性能会受到影响,但我还没有使用测试过多少。
回答by Do Nhu Vy
I use Eclipse Luna 4.4.
我使用 Eclipse Luna 4.4。
- Choose menu
Window
\Preferences
, chooseJava
\Code Style
\Code Templates
. Choose SectionComments
\Getters
|Setters
, press buttonEdit...
.
- 选择菜单
Window
\Preferences
,选择Java
\Code Style
\Code Templates
。选择部分Comments
\Getters
|Setters
, 按下按钮Edit...
。
There are many existing variables for you, use button
Insert Variable...
Edit your comment format, then press button
Apply
, then pressOK
to finish.
有许多现有变量可供您使用,请使用按钮
Insert Variable...
编辑您的评论格式,然后按 按钮
Apply
,然后按OK
完成。