文章目录
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[] |
callSuper | boolean,默认为false | 生成的toString方法打印类容包含调用父类的toString方法和自己参数类容 |
doNotUseGetters | boolean,默认为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 + '\'' + '}';
}
}