亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Java中Lombok @Value注解導致的variable not been initialized問題

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

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

Java中Lombok @Value注解導致的variable not been initialized問題

該類已經存在一個構造函數,為了不破壞該類原來的使用方式,于是重新寫了一個構造方法,之前的構造函數未改動。 該類被Lombok的@Value注解修飾解決 報錯信息顯示,變量未被初始化。于是主要排查是否有被初始化。 在重寫的構造方法中,我已經對該變量進行了初始化。 不明所以,開始找不同,這個類中,唯一不熟悉的就是@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='http://www.aoyou183.cn/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 '';}

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

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

/** * Adds modifiers to each field in the type with this annotation. *<p>* Complete documentation is found at<a href='http://www.aoyou183.cn/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,則每個實例字段(被@NonFinal注解修飾的除外)都會被final修飾符修飾。若level屬性有值,那么每個包私有訪問的(即沒有訪問修飾符修飾)和沒有被@PackagePrivate注解修飾的實例字段都會被添加一個與屬性值對應的修飾符。

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

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

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實例變量必須在聲明它的類的每個構造函數中確定賦值;同樣,空白final靜態變量必須在聲明它的類的靜態初始化器中明確賦值;否則,以上兩種情況下都會發生編譯錯誤。

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

到此這篇關于Java中Lombok @Value注解導致的variable not been initialized問題的文章就介紹到這了,更多相關Java Lombok @Value注解導致問題內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 91精品在线播放 | 国产91香蕉在线精品 | 四色婷婷婷婷色婷婷开心网 | 国产丰满主播丝袜勾搭秀 | 美国黄色一级毛片 | 午夜精品aaa国产福利 | 国产成人青青热久免费精品 | 亚色综合| 欧美大片全黄在线观看 | 一级毛片日韩a欧美 | 九九在线免费视频 | 免费观看性欧美一级 | 亚洲精品色综合区 | 天天久久 | 9191精品国产免费不久久 | 特级淫片aaaa毛片aa视频 | 国产精品美女久久久久久 | 久久青草免费线观最新 | 久久五月激情婷婷日韩 | 欧美—级v免费大片 | 亚洲高清视频在线 | 国产91区精品福利在线社区 | 天干天干夜天干天天爽 | 免费毛片网站 | 久久青青草视频 | 一级午夜a毛片免费视频 | 91精品视频网站 | 在线观看精品国语偷拍 | 久久久一区二区三区不卡 | 亚洲女人毛片 | 亚洲国产成人综合精品2020 | 国产成人久久一区二区三区 | 日韩爽爽视频爽爽 | 黄视频在线免费 | 看一级片| 亚洲欧美日韩在线观看你懂的 | 青草在线观看 | 国产美女久久久 | 免费看片免费播放国产 | 国产精品短视频免费观看 | 亚洲国产人久久久成人精品网站 |