问题
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?
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.
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.
回答1:
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.
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!
I wonder if this Q/A thread might have motivated the author to implement this nice feature.
回答2:
I finally found a solution (or at least a workaround) myself. I read about Spoon on 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.
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.
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!
回答3:
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.
回答4:
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.)
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 javadoc as follows:
I suspect search performance will be affected, but I haven't yet used tested by how much.
回答5:
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".
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?
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.
It's a bit annoying, but for some processings that's the way I have successfully adopted.
回答6:
The JavadocWriter plugin 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.
回答7:
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.
回答8:
I use Eclipse Luna 4.4.
- Choose menu
Window
\Preferences
, chooseJava
\Code Style
\Code Templates
. Choose SectionComments
\Getters
|Setters
, press buttonEdit...
.

There are many existing variables for you, use button
Insert Variable...
Edit your comment format, then press button
Apply
, then pressOK
to finish.
回答9:
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.
来源:https://stackoverflow.com/questions/996093/how-to-automatically-generate-comments-for-getter-setter-based-on-field-comments