构造函数上的 @Builder(toBuilder = true):错误“具有私有访问权限”
我有以下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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是
tobuilder = true
,并且具有构造函数参数,其名称与超级类中的相应字段不同。因此,Lombok不知道如何从tobuilder()
方法中填充构建器时从实例中提取字段值。要解决此问题,您可以使用与构造函数参数完全相同的名称(如果可以从您的子类访问字段,例如
受保护
),或给Lombok一个提示,以解决该字段的哪种方法价值。可以使用@builder.obtainvia
注释来实现后者: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 thetoBuilder()
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: