Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Delombok: Combination of @SuperBuilder, @NoArgsConstructur and @Builder.Default does not set default value in generated no-args constructor #3056

Closed
riemenschneider opened this issue Dec 2, 2021 · 1 comment

Comments

@riemenschneider
Copy link

riemenschneider commented Dec 2, 2021

Describe the bug
When using the @Builder/@SuperBuilder annotation default field values can be specified directly on the field by using the @Builder.Default annotation. In this case a generated no-args constructor should respect that default. This is working when using @Builder, @NoArgsConstructor and @Builder.Default annotations in combination. When when replacing the @Builder annotation by a @SuperBuilder annotation the default is missing in the generated no-args constructor.

To Reproduce
Download latest and greatest Lombok 1.18.22 jar and delombok the following piece of code:

import lombok.*;
import lombok.experimental.*;

@NoArgsConstructor
@SuperBuilder
public class Test {

	@Builder.Default
	private int index = 1;
	
}

(Store the code in a Java file Test.java and execute java -jar lombok.jar delombok -p Test.java.)

This will produce a delomboked Java class with an empty no-args constructor.

λ java -jar lombok.jar delombok -p Test.java
// Generated by delombok at Thu Dec 02 16:02:34 CET 2021

import lombok.*;

public class Test {
        private int index;

        @java.lang.SuppressWarnings("all")
        private static int $default$index() {
                return 0;
        }


        @java.lang.SuppressWarnings("all")
        public static abstract class TestBuilder<C extends Test, B extends Test.TestBuilder<C, B>> {
                @java.lang.SuppressWarnings("all")
                private boolean index$set;
                @java.lang.SuppressWarnings("all")
                private int index$value;

                @java.lang.SuppressWarnings("all")
                protected abstract B self();

                @java.lang.SuppressWarnings("all")
                public abstract C build();

                /**
                 * @return {@code this}.
                 */
                @java.lang.SuppressWarnings("all")
                public B index(final int index) {
                        this.index$value = index;
                        index$set = true;
                        return self();
                }

                @java.lang.Override
                @java.lang.SuppressWarnings("all")
                public java.lang.String toString() {
                        return "Test.TestBuilder(index$value=" + this.index$value + ")";
                }
        }


        @java.lang.SuppressWarnings("all")
        private static final class TestBuilderImpl extends Test.TestBuilder<Test, Test.TestBuilderImpl> {
                @java.lang.SuppressWarnings("all")
                private TestBuilderImpl() {
                }

                @java.lang.Override
                @java.lang.SuppressWarnings("all")
                protected Test.TestBuilderImpl self() {
                        return this;
                }

                @java.lang.Override
                @java.lang.SuppressWarnings("all")
                public Test build() {
                        return new Test(this);
                }
        }

        @java.lang.SuppressWarnings("all")
        protected Test(final Test.TestBuilder<?, ?> b) {
                if (b.index$set) this.index = b.index$value;
                 else this.index = Test.$default$index();
        }

        @java.lang.SuppressWarnings("all")
        public static Test.TestBuilder<?, ?> builder() {
                return new Test.TestBuilderImpl();
        }

        @java.lang.SuppressWarnings("all")
        public Test() {
        }
}

Expected behavior
The generated no-args constructor should set the field to it's default value (like it is done when using the @Builder annotation).

        @java.lang.SuppressWarnings("all")
        public Test() {
                this.index = Test.$default$index();
        }

Version info (please complete the following information):

  • Lombok version
    1.18.22
  • Platform
    • Azul Zulu 8 on Windows 10
      λ java -version
      openjdk version "1.8.0_302"
      OpenJDK Runtime Environment (Zulu 8.56.0.21-CA-win64) (build 1.8.0_302-b08)
      OpenJDK 64-Bit Server VM (Zulu 8.56.0.21-CA-win64) (build 25.302-b08, mixed mode)
      
    • Azul Zulu 8 on RHEL 8.5
      $ /opt/CBMS/tools/java/zulu8/bin/java -version
      openjdk version "1.8.0_222"
      OpenJDK Runtime Environment (Zulu 8.40.0.20-SA-linux64) (build 1.8.0_222-b10)
      OpenJDK 64-Bit Server VM (Zulu 8.40.0.20-SA-linux64) (build 25.222-b10, mixed mode)
      
    • Oracle JDK 1.8 on RHEL 8.5
      $ /opt/CBMS/tools/java/jdk1.8/bin/java -version
      java version "1.8.0_181"
      Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
      

Additional context
When using Lombok as a Java agent the generated code is perfectly fine.

Additionally I have tested with different versions of Lombok. Lombok 1.18.4, 1.18.12 and 1.18.20 produce the same wrong no-args constructor.

@riemenschneider
Copy link
Author

riemenschneider commented Apr 25, 2022

Closing this issue since it is a duplicate of #3053 which is resolved in 1.18.24.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant