Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Java Bean setters in Eclipse

Tags:

We use Java beans on some projects where I work, this means a lot of handcrafted boilerplate code like this.

I'm after an Eclipse plugin, or a way of configuring Eclipse code templates that allows a developer to generate the setters from a simple skeleton class, in a similar fashion to the 'Generate Getters and Setters' does for POJOs.

Input

public class MyBean {
    private String value;
}

Expected output

 public class MyBean {
     private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

     private String value;

     public String getValue() {
         return this.value;
     }

     public void setValue(String newValue) {
         String oldValue = this.value;
         this.value = newValue;
         this.pcs.firePropertyChange("value", oldValue, newValue);
     }

     [...]
 }

I'm aware of project Lombok, but I'd prefer to stick to a pure Java/Eclipse based approach.

I'm considering writing an Eclipse plugin for this myself, what would be really useful is a more powerful template plugin in Eclipse, that could solve this problem and others.

like image 911
Adam Avatar asked Apr 17 '16 05:04

Adam


People also ask

Can Eclipse generate getters and setters?

An Eclipse plugin which generates Javadoc for getters and setters of a Java class. The generated Javadocs are based on those of fields corresponding to the getters or setters.

How do I run Java Beans in Eclipse?

Solution. Develop the JavaBean code and the JSPs in place. To do that, simply set your project's output folder to the WEB-INF\classes directory in which you want the bean code, and create the JSP that uses the bean as a linked file in Eclipse.


1 Answers

Here's a simple solution that uses the Eclipse code templates. This response is based on this answer which also provides a template for setting up the PropertyChangeSupport. I've simply provided additional details regarding the setup process.

In Eclipse, select Windows > Preferences > Java > Editor > Templates > New. Add the following code template using an unambiguous name, e.g. BeanProperty:

private ${Type} ${property};

public ${Type} get${Property}() {
    return ${property};
}

public void set${Property}(${Type} ${property}) {
    ${propertyChangeSupport}.firePropertyChange("${property}", this.${property}, this.${property} = ${property});
}

Now, simply type BeanProperty in your target class, press Ctrl+Space to show the Template Proposals, and select the BeanProperty template. You can cycle through the field type, field name, and getter/setter names using your tab key. Press Enter to apply your changes.

See the Using Code Templates Help entry for more details, and this question for additional, useful code templates. Of course, this solution is still bound by the limitations of the Eclipse, and a more powerful plugin akin to the auto getter/setter tool would be desirable.

like image 68
Austin Avatar answered Oct 11 '22 09:10

Austin