欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中Lombok @Value注解導(dǎo)致的variable not been initialized問題

 更新時間:2021年07月28日 11:35:52   作者:liupjie  
本文主要介紹了Java中Lombok @Value注解導(dǎo)致的variable not been initialized問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下

背景

想要修改一個POJO類,在其中增加一個字段,但是增加以后就開始報錯:

  • 該類已經(jīng)存在一個構(gòu)造函數(shù),為了不破壞該類原來的使用方式,于是重新寫了一個構(gòu)造方法,之前的構(gòu)造函數(shù)未改動。
  • 該類被Lombok的@Value注解修飾

解決

  • 報錯信息顯示,變量未被初始化。于是主要排查是否有被初始化。
  • 在重寫的構(gòu)造方法中,我已經(jīng)對該變量進(jìn)行了初始化。
  • 不明所以,開始找不同,這個類中,唯一不熟悉的就是@Value注解,于是查看注解中的注釋:
/**
 * Generates a lot of code which fits with a class that is a representation of an immutable entity.
 *<p>
* Equivalent to {@code@Getter @FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE) @AllArgsConstructor @ToString @EqualsAndHashCode}.
 *<p>
* Complete documentation is found at<a href="<https://projectlombok.org/features/Value>" rel="external nofollow" >the project lombok features page for&#64;Value</a>.
 *
 *@seelombok.Getter
*@seelombok.experimental.FieldDefaults
*@seelombok.AllArgsConstructor
*@seelombok.ToString
*@seelombok.EqualsAndHashCode
*@seelombok.Data
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Value {
 /**
  * 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 @Value(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 "";
}

這個注解的作用是為一個不可變的實體類生成一系列與之匹配的代碼。效果等同于以下注解的組合:@Getter @FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE) @AllArgsConstructor @ToString @EqualsAndHashCode。

這其中有一個注解比較特殊,@FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE),見名知意,這是一個為字段設(shè)置默認(rèn)屬性的注解,注解的屬性值中,標(biāo)注了是否設(shè)置實例字段為final,訪問級別設(shè)置為private。

/**
 * Adds modifiers to each field in the type with this annotation.
 *<p>
* Complete documentation is found at<a href="<https://projectlombok.org/features/experimental/FieldDefaults>" rel="external nofollow" >the project lombok features page for&#64;FieldDefaults</a>.
 *<p>
* If {@codemakeFinal} is {@codetrue}, then each (instance) field that is not annotated with {@code@NonFinal} will have the {@codefinal} modifier added.
 *<p>
* If {@codelevel} is set, then each (instance) field that is package private (i.e. no access modifier) and does not have the {@code@PackagePrivate} annotation will
 * have the appropriate access level modifier added.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface FieldDefaults {
 AccessLevel level() default AccessLevel.NONE;
 boolean makeFinal() default false;
}

若makeFinal是true,則每個實例字段(被@NonFinal注解修飾的除外)都會被final修飾符修飾。
若level屬性有值,那么每個包私有訪問的(即沒有訪問修飾符修飾)和沒有被@PackagePrivate注解修飾的實例字段都會被添加一個與屬性值對應(yīng)的修飾符。

也就是說,@Value標(biāo)記了此POJO類為不可能變的類,其所有沒有被@NonFinal注解修飾的成員變量,都會被final修飾

事情到了這里,還是不知道問題在哪里(Java基礎(chǔ)差)。繼續(xù)找解決辦法。

Google搜索找到此問答:

Lombok @Wither, @Value, @NoArgsConstructor, @AllArgsConstructor do not work together

回答中有一段對于Java final的描述:

A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a "blank final" variable. A blank final instance variable of a class must be definitely assigned in every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs in both cases.

翻譯如下:
一個final修飾的變量只能通過初始化器或賦值語句初始化一次。它不需要在聲明處初始化:這被稱為“空白final”變量。類的空白final實例變量必須在聲明它的類的每個構(gòu)造函數(shù)中確定賦值;同樣,空白final靜態(tài)變量必須在聲明它的類的靜態(tài)初始化器中明確賦值;否則,以上兩種情況下都會發(fā)生編譯錯誤。

真相大白,原因是在原來的構(gòu)造器中沒有對新加入的字段進(jìn)行初始化。至此,問題解決。

到此這篇關(guān)于Java中Lombok @Value注解導(dǎo)致的variable not been initialized問題的文章就介紹到這了,更多相關(guān)Java Lombok @Value注解導(dǎo)致問題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JAVA 數(shù)據(jù)結(jié)構(gòu)鏈表操作循環(huán)鏈表

    JAVA 數(shù)據(jù)結(jié)構(gòu)鏈表操作循環(huán)鏈表

    這篇文章主要介紹了JAVA 數(shù)據(jù)結(jié)構(gòu)鏈表操作循環(huán)鏈表的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • SpringBoot簡單使用SpringData的jdbc和durid

    SpringBoot簡單使用SpringData的jdbc和durid

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著SpringBoot簡單使用SpringData的jdbc和durid,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Java單例模式分析

    Java單例模式分析

    這篇文章主要給大家介紹了關(guān)于Java單例模式,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-09-09
  • Redis打開rdb文件常用方法詳解

    Redis打開rdb文件常用方法詳解

    這篇文章主要介紹了Redis打開rdb文件常用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • 超詳細(xì)解析Spring Bean的創(chuàng)建過程

    超詳細(xì)解析Spring Bean的創(chuàng)建過程

    這篇文章主要揭秘了Spring Bean的創(chuàng)建過程,文中通過代碼示例和圖文結(jié)合的方式解析的超級詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-05-05
  • Java 導(dǎo)出excel進(jìn)行換行的案例

    Java 導(dǎo)出excel進(jìn)行換行的案例

    這篇文章主要介紹了Java 導(dǎo)出excel進(jìn)行換行的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 利用JWT如何實現(xiàn)對API的授權(quán)訪問詳解

    利用JWT如何實現(xiàn)對API的授權(quán)訪問詳解

    這篇文章主要給大家介紹了關(guān)于利用JWT如何實現(xiàn)對API的授權(quán)訪問的相關(guān)資料,需要的朋友可以參考下
    2018-09-09
  • 性能調(diào)優(yōu)之java服務(wù)器容器調(diào)優(yōu)詳解

    性能調(diào)優(yōu)之java服務(wù)器容器調(diào)優(yōu)詳解

    這篇文章主要介紹了java服務(wù)器容器調(diào)優(yōu),如果接口響應(yīng)時間超過了既定數(shù)據(jù),項目支撐不了這么大的請求,就需要對項目以及項目接口進(jìn)行數(shù)據(jù)庫、容器、緩存等方面的調(diào)優(yōu),文章中有詳細(xì)的代碼示例,需要的朋友可以參考一下
    2023-04-04
  • Java?JDK內(nèi)置常用接口和深淺拷貝

    Java?JDK內(nèi)置常用接口和深淺拷貝

    這篇文章主要介紹了Java?JDK內(nèi)置常用接口和深淺拷貝,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • Spring bean的實例化和IOC依賴注入詳解

    Spring bean的實例化和IOC依賴注入詳解

    這篇文章主要介紹了Spring bean的實例化和IOC依賴注入詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03

最新評論