如果使用 Lombok @Builder 进行初始化,则 Function 实现保持为 null

发布于 2025-01-09 23:40:00 字数 1547 浏览 0 评论 0原文

我正在使用 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?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2025-01-16 23:40:00

您可以通过在函数声明中添加修饰符 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.

如果没有你 2025-01-16 23:40:00

解决了这个问题

@Builder(toBuilder = true)

并将初始化从 修改

Vehicle vehicle = Vehicle.builder()

Vehicle vehicle = new Vehicle().toBuilder()

这对我来说似乎很容易。

此处阅读

Solved this problem with

@Builder(toBuilder = true)

and modifying the initialization from

Vehicle vehicle = Vehicle.builder()

to

Vehicle vehicle = new Vehicle().toBuilder()

This seems to be quite easy for me.

Read it from here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文