lombok(二)

本文详细介绍了Lombok库中的@Getter/@Setter、@NonNull和@ToString注解的使用,展示了如何通过这些注解自动创建getter/setter方法、实现非空检查以及自动生成toString方法,从而简化Java代码,提高开发效率。
摘要由CSDN通过智能技术生成

lombok(二)

注解

get/set方法

减少get/set方法编写,通过自动生成增加

使用范围

get/set使用范围相同

使用范围使用后结果文字描述
FIELD(使用在成员变量上)被注解的参数编译后会增加get/set方法
TYPE(类上)被注解的类中参数编译后会增加get/set方法
//以Getter为例
@Target({ElementType.FIELD, ElementType.TYPE})
//只保留到源码中
@Retention(RetentionPolicy.SOURCE)
public @interface Getter {}
@Getter/@Setter
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class User {
    private String uid;
    private String uname;

    public User(String uid, String uname) {
        this.uid = uid;
        this.uname = uname;
    }
}

测试类

import org.junit.jupiter.api.Test;

public class test {

    @Test
    public void test01(){
		User user = new User("101", "小名");
        System.out.println(user.getUid());
        System.out.println(user.getUname());
    }

}

在这里插入图片描述

非空

增加非空检查,抛出NullPointerException

使用范围
使用范围使用后结果文字描述
FIELD(使用在成员变量上)使用后对set方法中参数判空检查(和@Setter注解一起使用最佳)
PARAMETER(使用在参数上)会对使用参数进行判空检查
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE})
//保留到class中
@Retention(RetentionPolicy.CLASS)
@NonNull

源码

@Setter
@Getter
public class User implements OO {
    @NonNull
    private String uid;
    private String uname;

    public User() {
    }
    public User(@NonNull String uid, @NonNull String uname) {
        this.uid = uid;
        this.uname = uname;
    }
}

.class 字节码反编译后文件

//部分类容
//User有参构造
public User(@NonNull String uid, @NonNull String uname) {
        if (uid == null) {
            throw new NullPointerException("uid is marked non-null but is null");
        } else if (uname == null) {
            throw new NullPointerException("uname is marked non-null but is null");
        } else {
            this.uid = uid;
            this.uname = uname;
        }
  }
//set方法
public void setUid(@NonNull String uid) {
        if (uid == null) {
            throw new NullPointerException("uid is marked non-null but is null");
        } else {
            this.uid = uid;
        }
    }
toString方法

减少toString方法编写,通过自动生成增加

注意:当存在toString方法时,使用此注解则会失效

使用范围
使用范围使用后结果描述
TYPE(使用在类上)被注解的类编译后会增加toString方法
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface ToString {...}
@ToString
简单使用

User类

@ToString
public class User implements OO {
    private String uid;
    private String uname;
}

测试代码

@Test
public void test03(){
    System.out.println(new User().toString());
}

在这里插入图片描述

ToString的属性
属性入参属性含义
includeFieldNames(不包含参数名字)boolean,默认为true生成的toString方法打印类容不包含参数名字
exclude(除外)String[]生成的toString方法打印类容不包含除外类容,入参为String[]
of(与exclude互斥)String[]生成的toString方法打印类容只包含of类容,入参为String[]
callSuperboolean,默认为false生成的toString方法打印类容包含调用父类的toString方法和自己参数类容
doNotUseGettersboolean,默认为false当存在set方法时,且doNotUseGetters=true,生成的toString将使用this.xxx的形式获取参数,doNotUseGetters=false时生成toString使用this.getxxx的方式.官方说避免使用直接调用属性的方式(this.xxx)
onlyExplicitlyIncluded(只包含明确字段)boolean,默认为false生成的toString方法只包含显示标记@ToString.Include的字段,ture为包含所有非静态字段,false为不包含非静态字段配合@ToString.Include和@ToString.Exclude使用
@ToString.Include

此注解只能配合@ToString或者@Data一起使用,当只注解在成员变量时,注解只能配合@ToString中的onlyExplicitlyIncluded=true属性一起使用使用

使用范围使用后的结果描述
FIELD当onlyExplicitlyIncluded=true时,只会显示被注解的成员变量和方法
METHOD生成toString方法中会添加方法的返回值
//可以使用在成员变量和方法上
@Target({ElementType.FIELD, ElementType.METHOD})
//只存留在源码中
@Retention(RetentionPolicy.SOURCE)
public @interface Include {}
属性使用后的结果
name给成员变量,和方法命名,生成的toString方法中属性名为这个名字
rank给生成的toString方法属性打印顺序排序,数值越大,打印越靠前

源码

@ToString(onlyExplicitlyIncluded = true)
public class User extends SuperUser {
    @ToString.Include(name = "uid1",rank = 1)
    private String uid;
    private String uname;
    @ToString.Include(name = "call",rank = 2)
    public String call(){
        return "this is call";
    }
}

class字节码转编译

public class User extends SuperUser {
    private String uid;
    private String uname;
    public static final String str = "123";
    public User() {
    }
    public String call() {
        return "this is call";
    }
    public String toString() {
        return "User(call=" + this.call() + ", uid1=" + this.uid + ", uname=" + this.uname + ")";
    }
}

在这里插入图片描述

@ToString.Exclude

此注解只能配合@ToString或者@Data一起使用,被注解的成员变量不会在toString中打印

//只能使用在成员变量中
@Target(ElementType.FIELD)
//只存在与源码中
@Retention(RetentionPolicy.SOURCE)
public @interface Exclude {}

源码

@ToString
public class User{
    @ToString.Exclude
    private String uid;
    private String uname;
}

class字节码转编译

public class User{
    private String uid;
    private String uname;
    public static final String str = "123";
    public User() {
    }
    public String toString() {
        return "User{uid='" + this.uid + '\'' + ", uname='" + this.uname + '\'' + '}';
    }
}

lombok安装

点击跳转

持续更新lombok

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值