Java中Lombok @Value注解导致的variable not been initialized问题

来自:网络
时间:2021-08-09
阅读:
免费资源网 - https://freexyz.cn/
目录
背景
解决

背景

想要修改一个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="<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="<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静态变量必须在声明它的类的静态初始化器中明确赋值;否则,以上两种情况下都会发生编译错误。

真相大白,原因是在原来的构造器中没有对新加入的字段进行初始化。至此,问题解决。

免费资源网 - https://freexyz.cn/
返回顶部
顶部