@Data

@Data

現(xiàn)在所有一起:為快捷方式@ToString,@EqualsAndHashCode,@Getter在所有領(lǐng)域,@Setter所有非final字段,以及@RequiredArgsConstructor!

Overview

@Data是一個(gè)方便的快捷方式注釋,它捆綁了@ToString,@EqualsAndHashCode@Getter/@Setter@RequiredArgsConstructor它們的特征:換句話說(shuō),@Data生成通常與簡(jiǎn)單POJO(普通舊Java對(duì)象)和bean相關(guān)聯(lián)的所有樣板:所有字段的getter,所有非的setter最終場(chǎng),和適當(dāng)?shù)?code>toString,equalshashCode實(shí)現(xiàn)涉及類的字段和初始化所有final字段,以及不具有初始已打上所有非最終場(chǎng)構(gòu)造@NonNull,以保證該領(lǐng)域從來(lái)都不是空值。

@Data就像具有隱式@Getter,@Setter,@ToString,@EqualsAndHashCode@RequiredArgsConstructor在類注解(不同之處在于沒(méi)有構(gòu)造將生成如果已經(jīng)存在任何明確寫(xiě)入構(gòu)造函數(shù))。但是,這些注釋的參數(shù)(例如callSuper,includeFieldNamesexclude)不能設(shè)置@Data。如果您需要為這些參數(shù)中的任何一個(gè)設(shè)置非默認(rèn)值,只需顯式添加這些注釋; @Data足夠聰明,可以遵循那些注釋。

所有生成的getter和setter都將是public。要覆蓋訪問(wèn)級(jí)別,請(qǐng)使用顯式@Setter和/或@Getter注釋來(lái)注釋字段或類。您也可以使用此注釋(通過(guò)將其組合AccessLevel.NONE)來(lái)完全禁止生成getter和/或setter。

標(biāo)記為的所有字段transient都不會(huì)被考慮hashCodeequals。將完全跳過(guò)所有靜態(tài)字段(不考慮任何生成的方法,并且不會(huì)為它們創(chuàng)建setter / getter)。

如果類已經(jīng)包含與通常生成的任何方法具有相同名稱和參數(shù)計(jì)數(shù)的方法,則不會(huì)生成該方法,也不會(huì)發(fā)出警告或錯(cuò)誤。例如,如果您已經(jīng)有一個(gè)帶簽名的方法,則不會(huì)生成equals(AnyType param)任何equals方法,即使從技術(shù)上講,由于具有不同的參數(shù)類型,它可能是完全不同的方法。該規(guī)則同樣適用于構(gòu)造函數(shù)(任何顯式構(gòu)造將防止@Data從生成一個(gè)),以及toString,equals和所有的getter和setter。您可以標(biāo)記任何構(gòu)造函數(shù)或方法,@lombok.experimental.Tolerate以隱藏它們從lombok。

@Data可以正常處理字段的泛型參數(shù)。為了在為具有泛型的類構(gòu)造對(duì)象時(shí)減少樣板,可以使用該staticConstructor參數(shù)生成私有構(gòu)造函數(shù),以及返回新實(shí)例的靜態(tài)方法。這樣,javac將推斷變量名稱。因此,通過(guò)這樣聲明:@Data(staticConstructor="of") class Foo<T> { private T x;}您可以Foo通過(guò)編寫(xiě)創(chuàng)建新實(shí)例:Foo.of(5);而不是必須寫(xiě):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

見(jiàn)的小字@ToString,@EqualsAndHashCode,@Getter / @Setter@RequiredArgsConstructor。

各種眾所周知的關(guān)于nullity的注釋會(huì)導(dǎo)致插入空檢查并將其復(fù)制到相關(guān)位置(例如getter的方法,以及構(gòu)造函數(shù)和setter的參數(shù))。有關(guān)詳細(xì)信息,請(qǐng)參閱Getter / Setter文檔的小字體。

默認(rèn)情況下,任何以$符號(hào)開(kāi)頭的變量都會(huì)自動(dòng)排除。您可以通過(guò)指定顯式注釋(@Getter或者@ToString,例如)并使用'of'參數(shù)來(lái)包含它們。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 獨(dú)自撐傘走在淅瀝瀝雨夜的村莊。 從未感到這般貼近生活。 明天就是婆婆三周年的祭奠 那些痛苦折磨如同煉獄般的日子,竟...
    甜TT_35ab閱讀 239評(píng)論 0 0
  • 你見(jiàn)過(guò)海底撈針嗎?我也沒(méi)有 但我見(jiàn)過(guò)河底撈人 我初中以前生活在農(nóng)村里,很偏僻的那種。村子旁有一條河,我們上學(xué)都要經(jīng)...
    觀同學(xué)閱讀 3,738評(píng)論 1 0
  • 脆嫩的鳥(niǎo)啼聲,歡歡快快的鳴著。 油亮的綠樹(shù)葉,層層疊疊的堆著。 枯干的黃落葉,悉悉索索的響著。 講出我心里的話。 ...
    木木青苔閱讀 368評(píng)論 0 1

友情鏈接更多精彩內(nèi)容