【问题标题】:Is it possible to automatically generate constructors based on sub-interfaces? (with or without Lombok)是否可以根据子接口自动生成构造函数? (有或没有龙目岛)
【发布时间】:2021-03-29 21:58:33
【问题描述】:

以下面的代码为例。

可以自动生成MySuperInterfaceImpl(MyInterface myInterface, double myDouble)吗?

我更喜欢避免使用lombok.Builder,因为它会引入 NullPointerException 问题。

另外提到的是使用@lombok.experimental.Delegate@lombok.AllArgsConstructor会交换构造函数,所以在这种情况下我会问如何生成MySuperInterfaceImpl(String myString, int myInt, long myLong, double myDouble)的构造函数。老实说,我更喜欢这种实现方式。

interface MyInterface {
    String getMyString();
    int getMyInt();
    long getMyLong();
}

@lombok.Value
class MyInterfaceImpl implements MyInterface {
    String myString;
    int myInt;
    long myLong;
}

interface MySuperInterface extends MyInterface {
    double getMyDouble();
}

@lombok.Value
@lombok.AllArgsConstructor
class MySuperInterfaceImpl implements MySuperInterface {
    String myString;
    int myInt;
    long myLong;
    double myDouble;

    /**
    * It would be great if I could hide/automatically generate this constructor with an annotation
    */
    MySuperInterfaceImpl(MyInterface myInterface, double myDouble) {
        this(myInterface.getMyString(), myInterface.getMyInt(), myInterface.getMyLong(), myDouble);
    }
}

@lombok.Value
@lombok.AllArgsConstructor
class MySuperInterfaceImpl2 implements MySuperInterface {
    @Delegate MyInterface myInterface;
    double myDouble;

    /**
    * It would be great if I could hide/automatically generate this constructor with an annotation
    */
    MySuperInterfaceImpl(String myString, int myInt, long myLong, double myDouble)
        this(new MyInterface() {
            String myString() { return myString; }
            int myInt() { return myInt; }
            long myLong() { return myLong; }
        }, myDouble);
    }
}

【问题讨论】:

  • 知道要使用哪个 MyInterface 实例作为委托意味着什么?
  • @MrR 完全是MyInterface,而不是任何其他具体实现,如第二个MySuperInterfaceImpl 构造函数所示

标签: java constructor interface annotations lombok


【解决方案1】:

目前没有不使用@SuperBuilder 的干净方法来做到这一点 见问题2646

下面是不需要显式构造函数定义的最干净的@SuperBuilder 示例。

请注意,我还在 lombok.config 中设置了以下内容:


lombok.equalsAndHashCode.callSuper = CALL
lombok.toString.callSuper = CALL


import lombok.Value;
import lombok.experimental.NonFinal;
import lombok.experimental.SuperBuilder;

public class So66862187 {

    
    /**
     * The Interface MyInterface.
     */
    interface MyInterface {

        /**
         * Gets the my string.
         *
         * @return the my string
         */
        String getMyString();

        /**
         * Gets the my int.
         *
         * @return the my int
         */
        int getMyInt();

        /**
         * Gets the my long.
         *
         * @return the my long
         */
        long getMyLong();
    }

    /**
     * Since we want our implementations to be final we need a common abstract super class
     */
    @Value
    @NonFinal
    @SuperBuilder(toBuilder = true)
    public static abstract class AbstractMyInterface implements MyInterface {
        
        /** The my string. */
        String myString;

        /** The my int. */
        int myInt;

        /** The my long. */
        long myLong;
    }

    /**
     * The Class MyInterfaceImpl.
     */
    @Value
    @SuperBuilder(toBuilder = true)
    public static class MyInterfaceImpl extends AbstractMyInterface {}


    /**
     * The Interface MySuperInterface.
     */
    interface MySuperInterface extends MyInterface {

        /**
         * Gets the my double.
         *
         * @return the my double
         */
        double getMyDouble();
    }

    /**
     * <p>This class is optional. If there is only going to be on implementation of {@link MySuperInterface} you don't need it.
     */
    @Value
    @NonFinal
    @SuperBuilder(toBuilder = true)
    public static abstract class AbstractMySuperInterface extends AbstractMyInterface implements MySuperInterface {

        /** The my double. */
        double myDouble;

    }

    /**
     * The Class MySuperInterfaceImpl.
     */
    @Value
    @SuperBuilder
    public static class MySuperInterfaceImpl extends AbstractMySuperInterface {}

}

【讨论】:

  • 这似乎正是我想要的,谢谢♥
  • @Leobastiani 太棒了!你介意接受答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 2015-06-26
  • 2021-09-08
  • 1970-01-01
  • 2019-03-29
  • 1970-01-01
相关资源
最近更新 更多