天天看點

Lombok - Accessors詳細說明

分享知識 傳遞快樂

Accessor 的中文含義是存取器,@Accessors 用于配置 get/set 方法的生成結果,同時也支援鍊式操作,減少多餘對象的建立,另外 builder 類元資訊又可以減少。

@Accessors 注解有三個參數,下面分别簡單說明一下:

  • fluent:中文含義是流暢的,設定為 true,則 get/set 方法的方法名都是基礎屬性名,且 set 方法傳回目前對象
  • chain:中文含義是鍊式的,設定為 true,則 set 方法傳回目前對象
  • prefix:中文含義是字首,用于生成 get/set 方法的字段名會忽視指定字首(遵守駝峰命名)

示例代碼:

import lombok.Data;
import lombok.experimental.Accessors;

@Data
public class UserEntity {

    private Long id;
    private String name;
    private int age;

}      

1. fluent

@Accessors(fluent = true) 後編譯後的代碼:

public class UserEntity
{
    private Long id;
    private String name;
    private int age;
    
    public Long id() {
        return this.id;
    }
    
    public String name() {
        return this.name;
    }
    
    public int age() {
        return this.age;
    }
    
    public UserEntity id(final Long id) {
        this.id = id;
        return this;
    }
    
    public UserEntity name(final String name) {
        this.name = name;
        return this;
    }
    
    public UserEntity age(final int age) {
        this.age = age;
        return this;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof UserEntity)) {
            return false;
        }
        final UserEntity other = (UserEntity)o;
        if (!other.canEqual(this)) {
            return false;
        }
        final Object this$id = this.id();
        final Object other$id = other.id();
        Label_0065: {
            if (this$id == null) {
                if (other$id == null) {
                    break Label_0065;
                }
            }
            else if (this$id.equals(other$id)) {
                break Label_0065;
            }
            return false;
        }
        final Object this$name = this.name();
        final Object other$name = other.name();
        if (this$name == null) {
            if (other$name == null) {
                return this.age() == other.age();
            }
        }
        else if (this$name.equals(other$name)) {
            return this.age() == other.age();
        }
        return false;
    }
    
    protected boolean canEqual(final Object other) {
        return other instanceof UserEntity;
    }
    
    @Override
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $id = this.id();
        result = result * 59 + (($id == null) ? 43 : $id.hashCode());
        final Object $name = this.name();
        result = result * 59 + (($name == null) ? 43 : $name.hashCode());
        result = result * 59 + this.age();
        return result;
    }
    
    @Override
    public String toString() {
        return "UserEntity(id=" + this.id() + ", name=" + this.name() + ", age=" + this.age() + ")";
    }
}      

2. chain

@Accessors(chain = true) 編譯後的代碼:

public class UserEntity
{
    private Long id;
    private String name;
    private int age;
    
    public Long getId() {
        return this.id;
    }
    
    public String getName() {
        return this.name;
    }
    
    public int getAge() {
        return this.age;
    }
    
    public UserEntity setId(final Long id) {
        this.id = id;
        return this;
    }
    
    public UserEntity setName(final String name) {
        this.name = name;
        return this;
    }
    
    public UserEntity setAge(final int age) {
        this.age = age;
        return this;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof UserEntity)) {
            return false;
        }
        final UserEntity other = (UserEntity)o;
        if (!other.canEqual(this)) {
            return false;
        }
        final Object this$id = this.getId();
        final Object other$id = other.getId();
        Label_0065: {
            if (this$id == null) {
                if (other$id == null) {
                    break Label_0065;
                }
            }
            else if (this$id.equals(other$id)) {
                break Label_0065;
            }
            return false;
        }
        final Object this$name = this.getName();
        final Object other$name = other.getName();
        if (this$name == null) {
            if (other$name == null) {
                return this.getAge() == other.getAge();
            }
        }
        else if (this$name.equals(other$name)) {
            return this.getAge() == other.getAge();
        }
        return false;
    }
    
    protected boolean canEqual(final Object other) {
        return other instanceof UserEntity;
    }
    
    @Override
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $id = this.getId();
        result = result * 59 + (($id == null) ? 43 : $id.hashCode());
        final Object $name = this.getName();
        result = result * 59 + (($name == null) ? 43 : $name.hashCode());
        result = result * 59 + this.getAge();
        return result;
    }
    
    @Override
    public String toString() {
        return "UserEntity(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ")";
    }
}      

3. prefix

@Accessors(prefix = "user") 設定字尾示例

import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(prefix = "user")
public class UserEntity {

    private Long userId;
    private String userName;
    private int userAge;

}      
public class UserEntity
{
    private Long userId;
    private String userName;
    private int userAge;
    
    public Long getId() {
        return this.userId;
    }
    
    public String getName() {
        return this.userName;
    }
    
    public int getAge() {
        return this.userAge;
    }
    
    public void setId(final Long userId) {
        this.userId = userId;
    }
    
    public void setName(final String userName) {
        this.userName = userName;
    }
    
    public void setAge(final int userAge) {
        this.userAge = userAge;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof UserEntity)) {
            return false;
        }
        final UserEntity other = (UserEntity)o;
        if (!other.canEqual(this)) {
            return false;
        }
        final Object this$userId = this.getId();
        final Object other$userId = other.getId();
        Label_0065: {
            if (this$userId == null) {
                if (other$userId == null) {
                    break Label_0065;
                }
            }
            else if (this$userId.equals(other$userId)) {
                break Label_0065;
            }
            return false;
        }
        final Object this$userName = this.getName();
        final Object other$userName = other.getName();
        if (this$userName == null) {
            if (other$userName == null) {
                return this.getAge() == other.getAge();
            }
        }
        else if (this$userName.equals(other$userName)) {
            return this.getAge() == other.getAge();
        }
        return false;
    }
    
    protected boolean canEqual(final Object other) {
        return other instanceof UserEntity;
    }
    
    @Override
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $userId = this.getId();
        result = result * 59 + (($userId == null) ? 43 : $userId.hashCode());
        final Object $userName = this.getName();
        result = result * 59 + (($userName == null) ? 43 : $userName.hashCode());
        result = result * 59 + this.getAge();
        return result;
    }
    
    @Override
    public String toString() {
        return "UserEntity(userId=" + this.getId() + ", userName=" + this.getName() + ", userAge=" + this.getAge() + ")";
    }
}