How to use Lombok @Builder in abstract java class – @SuperBuilder? – Check How It Is Easy In 2 Min!

You are currently viewing How to use Lombok @Builder in abstract java class – @SuperBuilder? – Check How It Is Easy In 2 Min!
Photo by Tandya Rachmat on Unsplash
Share This Post, Help Others, And Earn My Heartfelt Appreciation! :)
4.9
(2336)

The answer is – you cannot, but you can use @SuperBuilder annotation instead of @Builder.

@SuperBuilder was introduced as experimental feature in lombok v1.18.2.
@SuperBuilder’s toBuilder feature and limited support for customization was added with lombok v1.18.4.

Short Introduction

The Lombok project is the java library which will allow you to forget about getters, setters, builders, constructors etc. This library will automatically generate them and plug into your IDE and build tool.

Example of SuperPoint and Point3D

Let’s create abstract SuperPoint class which has two variables: X and Y which allow us to describe point in 2D world.

package com.bigdataetl;

import lombok.Getter;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Getter
public abstract class SupperPoint {

    private int x;
    private int y;

}

In the next step we are going to create child class Point3D which extends SuperPoint class. Additionally we will create Z variable for the third dimension. Take a look at the toString() method. I used the variables from SuperPoint class to print the X and Y values.

SuperBuilder

package com.bigdataetl;

import lombok.experimental.SuperBuilder;

@SuperBuilder
public class Point3D extends SupperPoint {

    private int z;

    @Override
    public String toString() {
        return "Point3D{" +
                "x=" + getX() + "," +
                "y=" + getY() + "," +
                "z=" + z +
                '}';
    }
}

Create Object of Point3D

In the last step we will create the instance of Point3D class using Builder.

package com.bigdataetl;

public class LombokSuperBuilderMain {

    public static void main(String[] args) {

        Point3D point3D = Point3D.builder().x(5).y(10).z(15).build();
        System.out.println(point3D.toString());

    }

}

As the output you will see:

Point3D{x=5,y=10,z=15}

That’s all about how to use @Builder in abstract java class!

Read More Official Information

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.

Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

https://projectlom bok.org/

@Data

@Data is a convenient shortcut annotation that bundles the features of @ToString@EqualsAndHashCode@Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, setters for all non-final fields, and appropriate toStringequals and hashCode implementations that involve the fields of the class, and a constructor that initializes all final fields, as well as all non-final fields with no initializer that have been marked with @NonNull, in order to ensure the field is never null.

@Data is like having implicit @Getter@Setter@ToString@EqualsAndHashCode and @RequiredArgsConstructor annotations on the class (except that no constructor will be generated if any explicitly written constructors already exist). However, the parameters of these annotations (such as callSuperincludeFieldNames and exclude) cannot be set with @Data. If you need to set non-default values for any of these parameters, just add those annotations explicitly; @Data is smart enough to defer to those annotations.

All generated getters and setters will be public. To override the access level, annotate the field or class with an explicit @Setter and/or @Getter annotation. You can also use this annotation (by combining it with AccessLevel.NONE) to suppress generating a getter and/or setter altogether.

All fields marked as transient will not be considered for hashCode and equals. All static fields will be skipped entirely (not considered for any of the generated methods, and no setter/getter will be made for them).

If the class already contains a method with the same name and parameter count as any method that would normally be generated, that method is not generated, and no warning or error is emitted. For example, if you already have a method with signature equals(AnyType param), no equals method will be generated, even though technically it might be an entirely different method due to having different parameter types. The same rule applies to the constructor (any explicit constructor will prevent @Data from generating one), as well as toStringequals, and all getters and setters.

@Data can handle generics parameters for fields just fine. In order to reduce the boilerplate when constructing objects for classes with generics, you can use the staticConstructor parameter to generate a private constructor, as well as a static method that returns a new instance. This way, javac will infer the variable name. Thus, by declaring like so: @Data(staticConstructor="of") class Foo<T> { private T x;} you can create new instances of Foo by writing: Foo.of(5); instead of having to write: new Foo<Integer>(5);.

See the small print of @ToString@EqualsAndHashCode@Getter / @Setter and @RequiredArgsConstructor.

method for getters, and the parameter for the constructor and setters). See Getter/Setter documentation’s small print for more information.

Various well known annotations about nullity cause null checks to be inserted and will be copied to the relevant places (such as the

By default, any variables that start with a $ symbol are excluded automatically. You can include them by specifying an explicit annotation (@Getter or @ToString, for example) and using the ‘of’ parameter.

https://projectlombok.org/features/Data

How to implements Lombok @Builder for Abstract class

If you enjoyed this post please add the comment below and share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 2336

No votes so far! Be the first to rate this post.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments