Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I exclude fields from lomboks @Data annotation?

Lets say this is my class. And I want getters and setters for all fields except the Date. Is the a way to exclude?

@Data
public class User {
    String first;
    String last;
    String email;
    Date dob;
    Boolean active;
}
like image 226
DIBits Avatar asked Feb 20 '19 09:02

DIBits


2 Answers

I think this is the only way to hide:

@Getter(value=AccessLevel.PRIVATE)
@Setter(value=AccessLevel.PRIVATE)
private Date dob;

or maybe better with AccessLevel.NONE like Ken Chan's answer suggests

so overriding the access level. However this does not hide it from constructors.

Also you can make tricks with inheritance. Define class like:

public class Base {
    // @Getter if you want
    private Date dob;
}

and let your User to extend that:

@Data
public class User extends Base {
    private String first;
    private String last;
    private String email;
    private Boolean active;
}
like image 196
pirho Avatar answered Sep 28 '22 03:09

pirho


Well , or better yet use AccessLevel.NONE to completely make it does not generate getter or setter. No private getter or setter will be generated.

@Getter(value=AccessLevel.NONE)
@Setter(value=AccessLevel.NONE)
private Date dob;
like image 20
Ken Chan Avatar answered Sep 28 '22 03:09

Ken Chan