是否可以根据子接口自动生成构造函数? (有或没有龙目岛)

Is it possible to automatically generate constructors based on sub-interfaces? (with or without Lombok)

以下面的代码为例。

是否可以自动生成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);
    }
}

目前没有不使用 @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 {}

}