使用 @Builder.default的Java记录

发布于 2025-02-07 12:58:32 字数 744 浏览 2 评论 0原文

我想知道有什么方法可以将Java记录与Lombok的@builder.default相结合? 让我们考虑一个具有新文件创建的属性对象的示例。

在Java 14

@Value
@Builder
public class FileProperties {
    @Builder.Default
    String directory = System.getProperty("user.home");
    @Builder.Default
    String name = "New file";
    @Builder.Default
    String extension = ".txt";
}

Java 14

@Builder
public record FileProperties (
        String directory,
        String name,
        String extension
) {}

之前,但是如果我尝试使用诸如

@Builder
public record FileProperties (
        @Builder.Default
        String directory = System.getProperty("user.home")
) {}

编译器之类的内容,则会出错,揭示不允许使用这种语法。我们对这个问题有任何解决方案吗?

I'm wondering is there any way to combine java record with lombok's @Builder.Default?
Let's consider an example with properties object for new file creation.

Before java 14

@Value
@Builder
public class FileProperties {
    @Builder.Default
    String directory = System.getProperty("user.home");
    @Builder.Default
    String name = "New file";
    @Builder.Default
    String extension = ".txt";
}

Java 14

@Builder
public record FileProperties (
        String directory,
        String name,
        String extension
) {}

But in case if I try to use something like

@Builder
public record FileProperties (
        @Builder.Default
        String directory = System.getProperty("user.home")
) {}

Compiler will fail with an error, revealing that such syntax is not allowed. Do we have any solution to this problem?

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

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

发布评论

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

评论(3

初心未许 2025-02-14 12:58:32

您可以通过@builder.default来实现相同

@Builder
public record FileProperties (
        String directory,
        String name,
        String extension
) {
    public static class FilePropertiesBuilder {
        FilePropertiesBuilder() {
            directory = System.getProperty("user.home");
            name = "New file";
            extension = ".txt";
        }
    }
}

public static void main(String[] args) {
    System.out.println(FileProperties.builder().build());
}

结果

FileProperties[directory=/home/me, name=New file, extension=.txt]

You can achieve the same outcome not by @Builder.Default but by defining the builder itself with defaults for Lombok:

@Builder
public record FileProperties (
        String directory,
        String name,
        String extension
) {
    public static class FilePropertiesBuilder {
        FilePropertiesBuilder() {
            directory = System.getProperty("user.home");
            name = "New file";
            extension = ".txt";
        }
    }
}

Then to test it:

public static void main(String[] args) {
    System.out.println(FileProperties.builder().build());
}

Output:

FileProperties[directory=/home/me, name=New file, extension=.txt]
感情废物 2025-02-14 12:58:32

此功能目前尚不可用。根据伦波克作者Reinier Zwitserloot的问题的第一个评论:

现在,我没有考虑到将@builder扩展到记录时。

赖尼尔继续解释了为什么在伦波克实施这会有问题:

不幸的是,我无法考虑一些简单解决这个问题的简单解决方案 - 您不能将任意表达式贴在注释中,因此例如@builder.default(system.getProperty(“ user)。主页”))不编译 - Lombok不能那样做。

This functionality is not available at the moment. Per the first comment under the question by Lombok author Reinier Zwitserloot:

Not right now, I hadn't considered that when extending support of @Builder to records.

Reinier goes on to explain why this would be problematic to implement in Lombok:

Unfortunately, I cannot think off of the top of my head of some simple solution to this problem - you can't stick arbitrary expressions in annotations, so e.g. @Builder.Default(System.getProperty("user.home")) doesn't compile - lombok can't do it that way.

脸赞 2025-02-14 12:58:32

如果您想做更多的事情,请不要使用记录,而不是在容器中记录值。在这种情况下,您不应该使用记录,因为它不允许您指导与简单录制参数不同的东西。

如果您要为任何实例创建此类默认值,则需要一个“正常”的构造函数。

这就是为什么您无法在记录中(再次)设置值的原因。

Don't use a record if you want to do more things than recording values in a container. In this case you shouldn't use a record because it doesn't allow you to instruct something different than simple recording your arguments.

If you want a default value for any instance you are creating of this class you need a "normal" constructor.

That's why you can't set values (again) in records.

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