构造函数上的 @Builder(toBuilder = true):错误“具有私有访问权限”

发布于 2025-01-20 02:34:08 字数 1167 浏览 0 评论 0原文

我有以下2个父母的课程。

@AllArgsConstructor
public abstract class Data {

    @Getter
    private final String name;

    @Getter
    private final String source;

    @Getter
    private final String message;
}

而且

public class EventData extends Data {

    @Getter
    private String errorCode;

    @Getter
    private String errorMessage;

    @Getter
    private String api;


    @Builder(toBuilder = true)
    public EventData(String eventName, String errorCode, String eventMessage, String api, String errorMessage) {
        super(eventName, EventSource.BARCLAYS_GIL_PLUGIN_A_REST_SERVICE, eventMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.api = api;
    }

}

我会收到以下错误:

    [javac] /local/home/vishivan/workplace/xyx/src/ABC/src/com/LOL/mno/models/EventData.java:36: error: eventMessage has private access in Data
    [javac]     @Builder(toBuilder = true)
    [javac]     ^
    [javac] 1 error

因此,这是我的约束

,我需要一个构建器,我还需要一个模仿者来创建EventData对象。.我尝试添加setter@ and and and and and和由于最终变量无法定义设置器,因此导致错误...我该如何解决?

I have the following 2 parent-child classes..

@AllArgsConstructor
public abstract class Data {

    @Getter
    private final String name;

    @Getter
    private final String source;

    @Getter
    private final String message;
}

And

public class EventData extends Data {

    @Getter
    private String errorCode;

    @Getter
    private String errorMessage;

    @Getter
    private String api;


    @Builder(toBuilder = true)
    public EventData(String eventName, String errorCode, String eventMessage, String api, String errorMessage) {
        super(eventName, EventSource.BARCLAYS_GIL_PLUGIN_A_REST_SERVICE, eventMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.api = api;
    }

}

I am getting the following error:

    [javac] /local/home/vishivan/workplace/xyx/src/ABC/src/com/LOL/mno/models/EventData.java:36: error: eventMessage has private access in Data
    [javac]     @Builder(toBuilder = true)
    [javac]     ^
    [javac] 1 error

So here are my constraints

i need a builder in EventData and i need a copyconstructor to create the EventData object as well.. I tried adding setter@ and it resulted in error as final variables cant have setter defined... how can i solve this?

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

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

发布评论

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

评论(1

唠甜嗑 2025-01-27 02:34:08

您使用的是tobuilder = true,并且具有构造函数参数,其名称与超级类中的相应字段不同。因此,Lombok不知道如何从tobuilder()方法中填充构建器时从实例中提取字段值。

要解决此问题,您可以使用与构造函数参数完全相同的名称(如果可以从您的子类访问字段,例如受保护),或给Lombok一个提示,以解决该字段的哪种方法价值。可以使用@builder.obtainvia注释来实现后者:

public EventData(@Builder.ObtainVia(method = "getName") String eventName, String errorCode, @Builder.ObtainVia(method = "getMessage") String eventMessage, String api, String errorMessage) {
    super(eventName, EventSource.BARCLAYS_GIL_PLUGIN_A_REST_SERVICE, eventMessage);
    this.errorCode = errorCode;
    this.errorMessage = errorMessage;
    this.api = api;
}

You are using toBuilder = true, and you have constructor parameters whose names differ from the corresponding fields in your superclass. Thus, lombok does not know how to extract the field values from an instance when populating a builder in the toBuilder() method.

To solve this problem, you can either use exactly the same names as your constructor parameters (if the fields are accessible from your subclass, e.g. protected), or give lombok a hint in which way to retrieve the field value. The latter can be achieved using the @Builder.ObtainVia annotation:

public EventData(@Builder.ObtainVia(method = "getName") String eventName, String errorCode, @Builder.ObtainVia(method = "getMessage") String eventMessage, String api, String errorMessage) {
    super(eventName, EventSource.BARCLAYS_GIL_PLUGIN_A_REST_SERVICE, eventMessage);
    this.errorCode = errorCode;
    this.errorMessage = errorMessage;
    this.api = api;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文