Lombok之@Data使用

x33g5p2x  于2021-12-25 转载在 其他  
字(3.4k)|赞(0)|评价(0)|浏览(315)

一. 为什么要用@Data?

我们已经学习了@Getter/@Setter,@ToString,@EqualsAndHashCode,@RequiredArgsConstructor等注解,这个几个注解通常都是一个实体类所需要的。如果为每一个类都去加上这四个注解会有点麻烦,所以Lombok给出了一个注解用来表示这个四个注解,它就是@Data。使用了@Data后,这四个注解的注解属性都会采用默认值,即无法为其中某个注解指定该注解的注解属性值。如果要指定某个注解的注解属性值,需要开发者显示地指定。

二. @Data如何使用?

@Data的使用非常简单,它的注解属性也只有staticConstructor一个。

@Data(staticConstructor = "of")
public class Student {

    @Setter(AccessLevel.PROTECTED)
    @NonNull
    private String name;

    private Integer age;
}

反编译后的文件。

package com.cauchy6317.common.Data;

import lombok.NonNull;

public class Student {
    @NonNull
    private String name;
    private Integer age;

    private Student(@NonNull String name) {
        if (name == null) {
            throw new NullPointerException("name is marked non-null but is null");
        } else {
            this.name = name;
        }
    }

    public static Student of(@NonNull String name) {
        return new Student(name);
    }

    @NonNull
    public String getName() {
        return this.name;
    }

    public Integer getAge() {
        return this.age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Student)) {
            return false;
        } else {
            Student other = (Student)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

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

                return true;
            }
        }
    }

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

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $name = this.getName();
        int result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $age = this.getAge();
        result = result * 59 + ($age == null ? 43 : $age.hashCode());
        return result;
    }

    public String toString() {
        return "Student(name=" + this.getName() + ", age=" + this.getAge() + ")";
    }

    protected void setName(@NonNull String name) {
        if (name == null) {
            throw new NullPointerException("name is marked non-null but is null");
        } else {
            this.name = name;
        }
    }
}

可以看到,生成的setName方法的访问控制符为protected。同时,生成了静态工厂方法of,从此可以看出,staticConstructor的作用和@RequiredArgsConstructor中的staticName一样,都是生成静态工厂方法的。

三. @Data源码

package lombok;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/** * Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check * all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor. * <p> * Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}. * <p> * Complete documentation is found at <a href="https://projectlombok.org/features/Data">the project lombok features page for &#64;Data</a>. * * @see Getter * @see Setter * @see RequiredArgsConstructor * @see ToString * @see EqualsAndHashCode * @see lombok.Value */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Data {
	/** * If you specify a static constructor name, then the generated constructor will be private, and * instead a static factory method is created that other classes can use to create instances. * We suggest the name: "of", like so: * * <pre> * public @Data(staticConstructor = "of") class Point { final int x, y; } * </pre> * * Default: No static constructor, instead the normal constructor is public. * * @return Name of static 'constructor' method to generate (blank = generate a normal constructor). */
	String staticConstructor() default "";
}

四. 特别说明

本文已经收录在Lombok注解系列文章总览中,并继承上文中所提的特别说明。
源码地址:gitee

相关文章