龙目岛自定义SuperBuilder [英] Lombok Customize SuperBuilder

查看:78
本文介绍了龙目岛自定义SuperBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个这样的课程:

@Builder
public class Parent {
    final int a;
    final int b;

    public class static ParentBuilder {
        public ParentBuilder setAllTo(final int value) {
           return a(value).b(value);
        }
    }
}

public class Child extends Parent {
   final in c;

   @Builder(builderMethodName = "childBuilder")
   public Child(final int a, final int b, final int c) {
      super(a, b);
      this.c = c;
   }
}

我的课程正在成长,并且有越来越多的领域.这是使用 @SuperBuilder 的原因.但是如何添加自定义的生成器方法?

My classes are growing up and got more and more fields. And this is a reson to use the @SuperBuilder. But how can I add customized builder methods?

以同样的方式工作.我这样尝试过:

The same way dosent work. I tried this way:

@SuperBuilder
public abstract class Parent { //yes, I want a abstract parent
    final int a;
    final int b;

    public class static ParentBuilder {
        public ParentBuilder setAllTo(final int value) {
           return a(value).b(value);
        }
    }
}


@SuperBuilder
public class Child extends Parent {
   final in c;

}

编辑

这还不可能.当我尝试以同样的方式进行操作时,我遇到了一个例外: @SuperBuilder不支持定制的构建器.改用@Builder.
重写是这样的内部类:

Edit

Its not possible yet. When I try to do it the same way, then I got a exception: @SuperBuilder does not support customized builders. Use @Builder instead.
The override is a inner class like this:

public abstract static class ParentBuilder<C extends ParentBuilder, B extends Parent.ParentBuilder<C, B>> {
    // custom imlementations here
}

推荐答案

我最近尝试使用Lombok 1.18.8和IntelliJ自定义 @SuperBuilder ,并且效果很好.我面临的唯一问题是,我失去了在SuperBuilder中使用 toBuilder 标志的功能- @SuperBuilder(toBuilder = true).

I recently tried customizing @SuperBuilder using Lombok 1.18.8 and IntelliJ, and it worked fine. The only issue I faced was, I lost ability to use toBuilder flag in SuperBuilder - @SuperBuilder(toBuilder=true).

下面是重写@SuperBuilder方法的代码.

Below is the code to override @SuperBuilder methods.

public static abstract class ChildBuilder<C extends Child, B extends ChildBuilder<C, B>>
        extends ParentBuilder<C, B> {

    private LocalDate date;

    public B date(String dateStr) {
        this.date = LocalDate.parse(dateStr);
        return self();
    }
}

我在这里添加了工作代码:在龙目岛自定义SuperBuilder

I added my working code here: Customize SuperBuilder in Lombok

这篇关于龙目岛自定义SuperBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆