如果使用 Lombok @Builder 进行初始化,则 Function 实现保持为 null
我正在使用 Lombok 来初始化一个类。该类还定义了一些函数。当从上面初始化的对象调用时,这些函数保持为空。
VehicleTest Class:
public class VehicleTest {
public static void main(String...arg) {
Vehicle vehicle = Vehicle.builder()
.createdDateTime(DateUtil.getEpochTimeFromCurrentTimeZone())
.make("Toyota")
.year("2010")
.model("Fortunner")
.build();
System.out.println(vehicle.convertEpochToString.apply(DateUtil.getEpochTimeFromCurrentTimeZone()));
}
}
Vehicle Class:
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Vehicle {
Long createdDateTime;
String year;
String make;
String model;
public String getTime() {
return convertEpochToString.apply(createdDateTime);
}
public Function<Long,String> convertEpochToString = epochTime -> {
ZonedDateTime zonedDateTime = DateUtil.convertEpochToZonedDateTime(epochTime);
return DateUtil.formatZonedDateTime(zonedDateTime,"dd-MMM-yyyy");
};
}
正如您在该调试模式中所看到的,这个convertEpochToString 函数为null。
注意:这不是我在项目中使用的实际方式。这只是我用来描述我的问题的一个例子。
正如此链接中提到的,如果我使用
@Builder.Default
在上面的函数上,这似乎有效。但将其添加到每个类中的所有 100 多个函数中对我来说将是一项艰巨的任务。除了上述方法和静态函数之外,还有其他选择吗?
I am using Lombok to initialize a class. That class also has some Functions defined. Those Functions remain null when called from the above initialized object.
VehicleTest Class:
public class VehicleTest {
public static void main(String...arg) {
Vehicle vehicle = Vehicle.builder()
.createdDateTime(DateUtil.getEpochTimeFromCurrentTimeZone())
.make("Toyota")
.year("2010")
.model("Fortunner")
.build();
System.out.println(vehicle.convertEpochToString.apply(DateUtil.getEpochTimeFromCurrentTimeZone()));
}
}
Vehicle Class:
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Vehicle {
Long createdDateTime;
String year;
String make;
String model;
public String getTime() {
return convertEpochToString.apply(createdDateTime);
}
public Function<Long,String> convertEpochToString = epochTime -> {
ZonedDateTime zonedDateTime = DateUtil.convertEpochToZonedDateTime(epochTime);
return DateUtil.formatZonedDateTime(zonedDateTime,"dd-MMM-yyyy");
};
}
As you can see in this debug mode, this convertEpochToString Function is null.
Note: This is not the actual way I am using in my project. This is just an example I made to depict my problem.
As mentioned in this link, if I use
@Builder.Default
on above Function, this seems to be working. But adding it on all 100+ Functions in each class will be a huge task for me. Is there any other alternative apart from the above one and static Function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在函数声明中添加修饰符
static
来解决这种情况。但我不确定这种行为的原因,我认为如果适用于类,Lombok 代码生成可以做出决定,而如果适用于类实例和惰性函数则不能做出决定。
You can solve this case by adding modifier
static
to the function declaration.But I'm not sure about the reasons for such behavior, I think Lombok code generation can make decisions if works with class and can't if works with instance of class and lazy function.
解决了这个问题
并将初始化从 修改
为
这对我来说似乎很容易。
从此处阅读
Solved this problem with
and modifying the initialization from
to
This seems to be quite easy for me.
Read it from here