views:

1692

answers:

5

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.

+2  A: 

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.

akf
yes, but the only variable available for the templates is ${bare_field_name}, which is the name of the field - and not its description in the comment
räph
it is a start. you will ultimately need to provide this description somewhere.
akf
right, I wanted to comment the field and then let eclipse generate the getter/setter with comments based on the field comment.
räph
A: 

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.

flicken
that's what i need - but I won't switch from eclipse to IDEA to get that feature ;)
räph
+1  A: 

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 : http://stackoverflow.com/questions/103202/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.

zim2001
I will take a look at the macro thing. Working with eclipse and jedit is kind of cumbersome. I think then it is faster for me to copy the comments by hand!
räph
A: 

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.

Peter Lawrey
as I said, it's an requirement, even if the names are easy understandable. so I can't do much about it.the comments are of course not really generated - you enter the comment for the field - and eclipse should use that comment for the getter/setters too! it's not always possible to put every needed information into the fieldname (unless you want to have really long names...).
räph
+2  A: 

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!

räph