Constants, Fluent Setters and Builder Generator - plugin detail

Adds three new code generators: constants, fluent setters and builder. Constants are useful when you access a class fields via reflection. Fluent setters are setters named like the field they set that return "this" so that they can be used one after the other in a way called "fluent interface". The builder adds an inner class named "Builder", a static method that creates the class and a private constructor to the bean.

NetBeans Plugin - Constants, Fluent Setters and Builder Generator
Plugin owner: fillumina
Website: https://github.com/fillumina/netbeans_builder_module
Added: 2014-06-14
License: CDDL
Category: Code Generation
Downloaded: 1,334 times
Rating:
 4.65, by 5 users

Plugin Log Show log

Versions available

Download plugin   Download size: 0.03 MB   Last Update: 2014-07-02

What's new in this version

Updated plugin info.

Verifications for NetBeans versions

Plugin is not subject to any verification



Introduction

Constants

Accessing fields via reflection is risky because string names cannot be enforced to match the fields they refer to. To mitigate this problem an automatic procedure can be used to auto-generate them. The constants start with an underscore so to allow the automatic removal of removed or changed fields. The underscore can also be useful to implicitly specify that the constant refers to a field name.
public static final String _FIELD_NAME = "fieldName";

private int fieldName;

Fluent Setters

Fluent setters are field setters that return this and so can be appended one to another:
    public NamedBean name(final String value) {
        this.name = value;
        return this;
    }
The previous setter can be used like this:
    NamedBean nb = new NamedBean().name("Some Name");
This methods allows for a better understanding of the class parameters and it's more usable than the telescopic constructors but it doesn't allow for immutable beans.

Builder

A builder is a separate class that allows to use a fluent interface to generate immutable beans. This is an example of a builder as created by the plugin:
public class MyBean {
    private static String pippo = "";
    
    private final int a = 1;
    private String name;
    private final int age;

    public static class Builder {

        private String name;
        private int age;

        private Builder() {
        }

        public Builder name(final String value) {
            this.name = value;
            return this;
        }

        public Builder age(final int value) {
            this.age = value;
            return this;
        }

        public MyBean build() {
            return new javaapplication1.MyBean(name, age);
        }
    }

    public static MyBean.Builder builder() {
        return new MyBean.Builder();
    }

    private MyBean(final String name, final int age) {
        this.name = name;
        this.age = age;
    }

}
Note that the initialized final field is not considered in the builder. The generators can also be used while refactoring because they remove automatically the old artifacts and replace them with the new version.

[ You have to be logged in to be able to comment. ]

User Comments

  

Perfect!

I prefer this method. Randomly found this and its great! I'll be devastated if this were to stop working!
Posted by djc391 on Jun 17, 2014
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo