avatar

Lombok

Lombok

lombok_map

Annotation Code Result

Getter & Setter

1
2
3
4
5
6
public class User {
@Getter private BigInteger id;
@Setter private String username;
private String password;
private String email;
}

getter_setter_result

ToString
1
2
3
4
5
6
7
@ToString
public class User {
private BigInteger id;
private String username;
private String password;
private String email;
}
1
2
3
4
5
6
7
8
9
10
// in target/classes/User.class
public String toString() {
return "User(id=" + this.getId()
+ ", username="
+ this.getUsername()
+ ", password="
+ this.getPassword()
+ ", email=" + this.getEmail()
+ ")";
}
EqualsAndHashCode
NonNull
1
2
3
4
//before class variables
@NonNull private BigDecimal id;
//before method arguments
public void test(@NonNull String s) {...}
Performs null check
NoArgsConstructor
1
2
3
4
5
6
7
@NoArgsConstructor
public class User {
@NonNull private BigInteger id;
private String username;
private String password;
private String email;
}
1
2
public User() {
}
RequiredArgsConstructor
1
2
3
4
5
6
7
@RequiredArgsConstructor
public class User {
@NonNull private BigInteger id;
final private String username;
private String password;
private String email;
}
1
2
3
4
5
6
7
8
9
// the arguments are NonNull and final variables
public User(@NonNull BigInteger id, String username) {
if (id == null) {
throw new NullPointerException("id is marked non-null but is null");
} else {
this.id = id;
this.username = username;
}
}
AllArgsConstructor
1
2
3
4
5
6
7
@AllArgsConstructor
public class User {
@NonNull private BigInteger id;
private String username;
private String password;
private String email;
}
1
2
3
4
5
6
7
8
9
10
public User(@NonNull BigInteger id, String username, String password, String email) {
if (id == null) {
throw new NullPointerException("id is marked non-null but is null");
} else {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
}
Data
1
2
3
4
5
6
7
@Data
public class User {
@NonNull private BigInteger id;
final private String username;
private String password;
private String email;
}

work as a combination of getter/setter, equalsAndHashcode, toString, requiredArgsConstructors.

data_result

Builder
1
2
3
4
5
6
7
8
@Data
@Builder
public class User {
@NonNull private BigInteger id;
final private String username;
private String password;
private String email;
}

builder_result

Log
1
2
@Log
log.info("create user");
val
1
val map = new HashMap<String, String>();
work as val in JavaScript
CleanUp
1
2
@Cleanup InputStream in = null;
@Cleanup FileOutputStream out = null;
1
2
3
4
5
6
7
8
9
10
11
Object in = null;
try {
FileOutputStream out = null;
if (Collections.singletonList(out).get(0) != null) {
((FileOutputStream)out).close();
}
} finally {
if (Collections.singletonList(in).get(0) != null) {
((InputStream)in).close();
}
}

Getter and Setter

  1. AccessLevel

    The default Getter is public unless it is specified with an AccessLevel.

    1
    2
    3
    4
    5
    6
    @Getter(AccessLevel.MODULE)
    @Getter(AccessLevel.PACKAGE)
    @Getter(AccessLevel.PRIVATE)
    @Getter(AccessLevel.PROTECTED)
    @Getter(AccessLevel.PUBLIC)
    @Getter(AccessLevel.NONE) // will not generate getter & setter
  2. lazy

    With lazy, the first time the getter is called, it will cache the value from then on.

    1
    @Getter(lazy = true)
  3. Getter and Setter can be added before the field or before the class

ToString

  1. @ToString.Exclude:

    1
    2
    3
    4
    5
    6
    7
    8
    @ToString
    public class User {
    private BigInteger id;
    private String username;
    private String password;
    // skip email in toString()
    @ToString.Exclude private String email;
    }
  2. @ToString.Include

  3. callSuper = true will include the output of toString of superclass

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @ToString(callSuper = true)
    public class VipUser extends User{
    private boolean vipStatus;
    }

    // VipUser.toString()
    public String toString() {
    return "VipUser(super=" + super.toString() + ", vipStatus=" + this.vipStatus + ")";
    }

EqualsAndHashCode

  • Code:

    1
    2
    3
    4
    5
    6
    7
    @EqualsAndHashCode
    public class User {
    private BigInteger id;
    private String username;
    private String password;
    private String email;
    }
  • Results:

    lombok will generate canEqual, equals, hashCode

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    // in target/classes/User.class
    public boolean equals(Object o) {
    if (o == this) {
    return true;
    } else if (!(o instanceof User)) {
    return false;
    } else {
    User other = (User)o;
    if (!other.canEqual(this)) {
    return false;
    } else {
    label59: {
    Object this$id = this.id;
    Object other$id = other.id;
    if (this$id == null) {
    if (other$id == null) {
    break label59;
    }
    } else if (this$id.equals(other$id)) {
    break label59;
    }

    return false;
    }

    Object this$username = this.username;
    Object other$username = other.username;
    if (this$username == null) {
    if (other$username != null) {
    return false;
    }
    } else if (!this$username.equals(other$username)) {
    return false;
    }

    Object this$password = this.password;
    Object other$password = other.password;
    if (this$password == null) {
    if (other$password != null) {
    return false;
    }
    } else if (!this$password.equals(other$password)) {
    return false;
    }

    Object this$email = this.email;
    Object other$email = other.email;
    if (this$email == null) {
    if (other$email != null) {
    return false;
    }
    } else if (!this$email.equals(other$email)) {
    return false;
    }

    return true;
    }
    }
    }

    protected boolean canEqual(Object other) {
    return other instanceof User;
    }

    public int hashCode() {
    int PRIME = true;
    int result = 1;
    Object $id = this.id;
    int result = result * 59 + ($id == null ? 43 : $id.hashCode());
    Object $username = this.username;
    result = result * 59 + ($username == null ? 43 : $username.hashCode());
    Object $password = this.password;
    result = result * 59 + ($password == null ? 43 : $password.hashCode());
    Object $email = this.email;
    result = result * 59 + ($email == null ? 43 : $email.hashCode());
    return result;
    }
  • exclude in EqualsAndHashCode

    1
    @EqualsAndHashCode(exclude = {"password"})
Author: hyangjudy
Link: https://hyangjudy.github.io/2020/05/24/lombok/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
    微信
  • 支付寶
    支付寶