Java Lombok自定义生成方法的可选项表示不能引用非静态变量
给定以下代码Java Lombok自定义生成方法的可选项表示不能引用非静态变量,java,lombok,intellij-lombok-plugin,Java,Lombok,Intellij Lombok Plugin,给定以下代码 @Getter @Builder(builderClassName = "Builder", buildMethodName = "build") public final class BusinessEvent implements BusinessPayload { private String action, duration, outcome, provider, trackId, sequence; @lombok.Builder.Default private
@Getter
@Builder(builderClassName = "Builder", buildMethodName = "build")
public final class BusinessEvent implements BusinessPayload {
private String action, duration, outcome, provider, trackId, sequence;
@lombok.Builder.Default private Optional<String> brand, vin, externalTrackId, code = Optional.empty();
@lombok.Builder.Default private Optional<BusinessEventError> eventError = Optional.empty();
static class Builder {
BusinessEvent build() throws MissingRequiredValueException {
// Custom validation
return new BusinessEvent(action, duration, outcome, provider, trackId, sequence, brand, vin, externalTrackId, code, eventError);
}
}
}
@Getter
@生成器(builderClassName=“Builder”,buildMethodName=“build”)
公共最终类BusinessEvent实现BusinessPayload{
私有字符串操作、持续时间、结果、提供程序、trackId、序列;
@lombok.Builder.Default私有可选品牌,vin,externalTrackId,code=Optional.empty();
@lombok.Builder.Default private Optional eventError=Optional.empty();
静态类生成器{
BusinessEvent build()引发MissingRequiredValueException{
//自定义验证
返回新的BusinessEvent(操作、持续时间、结果、提供者、trackId、序列、品牌、vin、externalTrackId、代码、eventError);
}
}
}
我得到了错误
java:无法从中引用非静态变量eventError
静态上下文
在这种情况下,lombok无法正确处理可选值?我在我所有的建设者身上都看到了同样的问题。intellij插件并没有显示这是一个问题,但只有在我尝试构建时才会显示
我知道您不应该使用可选字段值,但在这种情况下,它会使API更加清晰,而且构建器无论如何也不会序列化,为此我们有DTO。总结一下您想要做的事情:
- 您需要由lombok生成的生成器
- 您希望在生成器中使用
@default
- 您希望在生成方法中进行自定义验证
@lombok.Builder.Default
注释:
@Getter
@Builder(builderClassName = "Builder", buildMethodName = "build")
public final class BusinessEvent {
private String action, duration, outcome, provider, trackId, sequence;
private Optional<String> brand, vin, externalTrackId, code = Optional.empty();
private Optional<Object> eventError = Optional.empty();
static class Builder {
BusinessEvent build() {
if (brand == null) {
throw new RuntimeException("brand not set");
}
// Custom validation
return new BusinessEvent(action, duration, outcome, provider, trackId, sequence, brand, vin, externalTrackId, code, eventError);
}
}
public static void main(String[] args) {
BusinessEvent eventWithBrand = BusinessEvent.builder().brand(Optional.of("brand")).build();
// will throw exception
BusinessEvent event = BusinessEvent.builder().build();
}
}
@Getter
@生成器(builderClassName=“Builder”,buildMethodName=“build”)
公共期末商务活动{
私有字符串操作、持续时间、结果、提供程序、trackId、序列;
私有可选品牌,vin,externalTrackId,code=Optional.empty();
private Optional eventError=Optional.empty();
静态类生成器{
BusinessEvent构建(){
如果(品牌==null){
抛出新的RuntimeException(“品牌未设置”);
}
//自定义验证
返回新的BusinessEvent(操作、持续时间、结果、提供者、trackId、序列、品牌、vin、externalTrackId、代码、eventError);
}
}
公共静态void main(字符串[]args){
BusinessEvent eventWithBrand=BusinessEvent.builder().brand(可选)of(“brand”).build();
//将抛出异常
BusinessEvent=BusinessEvent.builder().build();
}
}
您确定这与Lombok有关吗?您有一个静态内部生成器类。此类没有BusinessEvent的封闭实例。但您正在尝试访问该类的实例字段。无论您使用可选或Lombok,这都不会起作用。非可选字段没有此问题。是的,它与lombok和可选的有关。你试过删除最后一个构造函数参数吗?然后,您可能会发现编译器开始抱怨变量code
,这意味着它只给出关于最后一个参数的错误。试着把action
(非可选)作为最后一个参数,看看会发生什么。我想你误解了lombok的工作原理。静态类中的值是构造函数中引用的值(但是它们不可见,因为这是lombok magic),而不是BusinessEventError中的值。