@Data
现在所有一起:为快捷方式@ToString,@EqualsAndHashCode,@Getter在所有领域,@Setter所有非final字段,以及@RequiredArgsConstructor!
Overview
@Data
是一个方便的快捷方式注释,它捆绑了@ToString
,@EqualsAndHashCode
和@Getter
/@Setter
和@RequiredArgsConstructor
它们的特征:换句话说,@Data
生成通常与简单POJO(普通旧Java对象)和bean相关联的所有样板:所有字段的getter,所有非的setter最终场,和适当的toString
,equals
并hashCode
实现涉及类的字段和初始化所有final字段,以及不具有初始已打上所有非最终场构造@NonNull
,以保证该领域从来都不是空值。
@Data
就像具有隐式@Getter
,@Setter
,@ToString
,@EqualsAndHashCode
和@RequiredArgsConstructor
在类注解(不同之处在于没有构造将生成如果已经存在任何明确写入构造函数)。但是,这些注释的参数(例如callSuper
,includeFieldNames
和exclude
)不能设置@Data
。如果您需要为这些参数中的任何一个设置非默认值,只需显式添加这些注释; @Data
足够聪明,可以遵循那些注释。
所有生成的getter和setter都将是public
。要覆盖访问级别,请使用显式@Setter
和/或@Getter
注释来注释字段或类。您也可以使用此注释(通过将其组合AccessLevel.NONE
)来完全禁止生成getter和/或setter。
标记为的所有字段transient
都不会被考虑hashCode
和equals
。将完全跳过所有静态字段(不考虑任何生成的方法,并且不会为它们创建setter / getter)。
如果类已经包含与通常生成的任何方法具有相同名称和参数计数的方法,则不会生成该方法,也不会发出警告或错误。例如,如果您已经有一个带签名的方法,则不会生成equals(AnyType param)
任何equals
方法,即使从技术上讲,由于具有不同的参数类型,它可能是完全不同的方法。该规则同样适用于构造函数(任何显式构造将防止@Data
从生成一个),以及toString
,equals
和所有的getter和setter。您可以标记任何构造函数或方法,@lombok.experimental.Tolerate
以隐藏它们从lombok。
@Data
可以正常处理字段的泛型参数。为了在为具有泛型的类构造对象时减少样板,可以使用该staticConstructor
参数生成私有构造函数,以及返回新实例的静态方法。这样,javac将推断变量名称。因此,通过这样声明:@Data(staticConstructor="of") class Foo<T> { private T x;}
您可以Foo
通过编写创建新实例:Foo.of(5);
而不是必须写:new Foo<Integer>(5);
。
With Lombok
import lombok.AccessLevel;
import lombok.Setter;
import lombok.Data;
import lombok.ToString;
@Data public class DataExample {
private final String name;
@Setter(AccessLevel.PACKAGE) private int age;
private double score;
private String[] tags;
@ToString(includeFieldNames=true)
@Data(staticConstructor="of")
public static class Exercise<T> {
private final String name;
private final T value;
}
}
Vanilla Java
import java.util.Arrays;
public class DataExample {
private final String name;
private int age;
private double score;
private String[] tags;
public DataExample(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setScore(double score) {
this.score = score;
}
public double getScore() {
return this.score;
}
public String[] getTags() {
return this.tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
@Override public String toString() {
return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";
}
protected boolean canEqual(Object other) {
return other instanceof DataExample;
}
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof DataExample)) return false;
DataExample other = (DataExample) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
if (this.getAge() != other.getAge()) return false;
if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
return true;
}
@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
final long temp1 = Double.doubleToLongBits(this.getScore());
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + this.getAge();
result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
result = (result*PRIME) + Arrays.deepHashCode(this.getTags());
return result;
}
public static class Exercise<T> {
private final String name;
private final T value;
private Exercise(String name, T value) {
this.name = name;
this.value = value;
}
public static <T> Exercise<T> of(String name, T value) {
return new Exercise<T>(name, value);
}
public String getName() {
return this.name;
}
public T getValue() {
return this.value;
}
@Override public String toString() {
return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";
}
protected boolean canEqual(Object other) {
return other instanceof Exercise;
}
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Exercise)) return false;
Exercise<?> other = (Exercise<?>) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;
if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;
return true;
}
@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode());
return result;
}
}
}
Supported configuration keys:
lombok.data.flagUsage = [warning | error] (default: not set)
Small print
见的小字@ToString
,@EqualsAndHashCode
,@Getter / @Setter
和@RequiredArgsConstructor。
各种众所周知的关于nullity的注释会导致插入空检查并将其复制到相关位置(例如getter的方法,以及构造函数和setter的参数)。有关详细信息,请参阅Getter / Setter文档的小字体。
默认情况下,任何以$符号开头的变量都会自动排除。您可以通过指定显式注释(@Getter
或者@ToString
,例如)并使用'of'参数来包含它们。
网友评论