简体   繁体   中英

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 eg 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..

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.

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).

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM