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.
To generate getters and setters, do the following: Create the fields you want in the class then press Alt+Shift+S, R. A dialog will pop up allowing you to choose the fields you want to generate getters and setters for. Click Select All to create getters/setters for all fields.
Shift-Alt-J is a useful keyboard shortcut in Eclipse for creating Javadoc comment templates.
The shortcut key is Ctrl+Shift+D. In the dialog select getters and setters for which you want to generate Javadocs and click "OK" button. Javadocs are generated for the selected getters and setters.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With