Skip to content

Adds fluent setters and builder generation to NetBeans code generators.

Notifications You must be signed in to change notification settings

SIDSSIDS/netbeans_builder_module

 
 

Repository files navigation

A Netbeans module that adds constants, fluent setters and builder to code generators.

image of the builder

Download

The plugin can be downloaded from the Netbeans Plugin Center.

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 initialization parameters but it cannot be used with immutable classes.

Builder

A builder is a separate class uses a fluent interface to generate another class (can be used with immutable classes as well).

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.

About

Adds fluent setters and builder generation to NetBeans code generators.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%