久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:111日期:2023-02-07 08:46:59
目錄背景解決背景

想要修改一個(gè)POJO類(lèi),在其中增加一個(gè)字段,但是增加以后就開(kāi)始報(bào)錯(cuò):

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

該類(lèi)已經(jīng)存在一個(gè)構(gòu)造函數(shù),為了不破壞該類(lèi)原來(lái)的使用方式,于是重新寫(xiě)了一個(gè)構(gòu)造方法,之前的構(gòu)造函數(shù)未改動(dòng)。 該類(lèi)被Lombok的@Value注解修飾解決 報(bào)錯(cuò)信息顯示,變量未被初始化。于是主要排查是否有被初始化。 在重寫(xiě)的構(gòu)造方法中,我已經(jīng)對(duì)該變量進(jìn)行了初始化。 不明所以,開(kāi)始找不同,這個(gè)類(lèi)中,唯一不熟悉的就是@Value注解,于是查看注解中的注釋?zhuān)?p>/** * 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='http://m.baoyu77737.com/bcjs/<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 '';}

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

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

/** * Adds modifiers to each field in the type with this annotation. *<p>* Complete documentation is found at<a href='http://m.baoyu77737.com/bcjs/<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,則每個(gè)實(shí)例字段(被@NonFinal注解修飾的除外)都會(huì)被final修飾符修飾。若level屬性有值,那么每個(gè)包私有訪問(wèn)的(即沒(méi)有訪問(wèn)修飾符修飾)和沒(méi)有被@PackagePrivate注解修飾的實(shí)例字段都會(huì)被添加一個(gè)與屬性值對(duì)應(yīng)的修飾符。

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

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

Google搜索找到此問(wèn)答:

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

回答中有一段對(duì)于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.

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

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

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

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 历史| 台安县| 城固县| 襄汾县| 丘北县| 印江| 多伦县| 呼和浩特市| 资中县| 万盛区| 永春县| 芜湖市| 天气| 云南省| 旌德县| 焉耆| 铜梁县| 额尔古纳市| 营口市| 美姑县| 讷河市| 永州市| 昌吉市| 济源市| 洛隆县| 左云县| 旌德县| 荥经县| 玉溪市| 巫山县| 汝阳县| 富源县| 汉源县| 崇仁县| 邓州市| 北安市| 图木舒克市| 红桥区| 韩城市| 孝感市| 凌源市|