Modifying NetBeans Generated Getters And Setters

For quite a long time I was thinking about automatically adding Javadoc to getters and setters generated by NetBeans, and also automatically adding final modifier to setter parameter. NetBeans does not have this feature yet. So what exactly I wanted?

package com.mycompany.mavenproject1;

public class App {

    /**
     * Contains exciting values.
     */
    private String test;

    /**
     * Getter for {@link #test}.
     *
     * @return {@link #test}
     */
    public String getTest() {
        return test;
    }

    /**
     * Setter for {@link #test}.
     *
     * @param test {@link #test}
     */
    public void setTest(final String test) {
        this.test = test;
    }
}

With some guidance from Petr Pišl from NetBeans team, I was able to do this patch: netbeans-getter-setter.patch. You can patch your NetBeans with it, adjust the generated Javadocs to your needs, compile NetBeans and your generated getters and setters should contain exactly what you need without pressing any extra key on your keyboard.

If you want to achieve the same for php, look at org.netbeans.modules.php.editor.codegen.CGSGenerator at GETTER_TEMPLATE and SETTER_TEMPLATE declarations. it’s even easier to modify these to get the comments added.

Sure it would be better if this would be done using templates, but atm this is better than nothing.

Also I updated NetBeans ebuild in Gentoo so that you can emerge NetBeans including your patches. You can find more info at NetBeans article at Gentoo Wiki.

Tags:

Leave a Reply

You must be logged in to post a comment.