Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically generate comments for getter/setter based on field comments in Eclipse?

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.

like image 252
räph Avatar asked Jun 15 '09 13:06

räph


People also ask

Can generate getters and setters in Eclipse?

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.

How do I create a function comment in Eclipse?

Shift-Alt-J is a useful keyboard shortcut in Eclipse for creating Javadoc comment templates.

How do you generate javadocs for getters and setters?

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.


2 Answers

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.

like image 91
hideaki Avatar answered Sep 23 '22 14:09

hideaki


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!

like image 20
räph Avatar answered Sep 21 '22 14:09

räph