Njax3SmmM2x2a0Zf7Hpd
Njax3SmmM2x2a0Zf7Hpd

Reputation: 1364

How to generate the Builder java class for your POJO

I have this pojo file that consists over 50 attributes. Creating a manual builder class can be error prone activity.

Is there a easy way to generate the builder class? for e.g. If you required to generate getter setters you would normally use eclipse Source > Generate Getters and Setters Is there a painless procedure to perform this?

Really appreciate any help..

Upvotes: 3

Views: 5341

Answers (3)

Bishnubrata Panigrahi
Bishnubrata Panigrahi

Reputation: 51

Lombok has support for all the above requirement:

You can define all the getters and setters by adding @Data. @Builder will make the class as builder.

There are more customisation also available for these annotations, you can find in their website https://projectlombok.org/

@Builder
@Data
public class Emails {
    @Builder.Default
    private String type = "work";
    private String value;
}

Upvotes: 0

davidhilton68
davidhilton68

Reputation: 331

I have just used Practical macros, within a few minutes of install from the market place, I could generate *constructors*, getters / setters, toString, hashcode and equals (basically chaining the standard eclipse commands) in a single command. Just what I was looking for and saved me loads of time. I can also see a lot more uses for it, well done to Earnst (the creator).

Upvotes: 1

darijan
darijan

Reputation: 9775

Use Lombok.

You can annotate your class, for example:

@Data //generate getters and setters
@EqualsAndHashCode(callSuper=true) //self descriptive
@NoArgsConstructor //self descriptive
@AllArgsConstructor //self descriptive

Remark: it works only with Eclipse for now.

Upvotes: 5

Related Questions