Lombok: Code with @Builder annotation fails to compile in Java 9 (works in Java 8)

Created on 7 Aug 2018  ·  3Comments  ·  Source: projectlombok/lombok

Using Lombok 1.18.2. The following class compiles with Java 8 but not with Java 9:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.Optional;

import lombok.Builder;
import lombok.Value;

public class TestLombok {
    @Value
    @Builder
    static class MyClass {
        private final int dayOfWeek;

        static class MyClassBuilder {
            private Optional<LocalDate> date = Optional.empty();

            MyClassBuilder dayOfWeek(int date) {
                if (!this.date.isPresent()) {
                    this.date = Optional.of(LocalDate.parse(String.valueOf(date),
                                                                   DateTimeFormatter.ofPattern("yyyyMMdd") // doesn't compile
                                                                    // DateTimeFormatter.BASIC_ISO_DATE // compiles
                    ));
                }

                this.dayOfWeek = this.date.get().getDayOfWeek().getValue();
                return this;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(MyClass.builder().dayOfWeek(20181013).build());
    }
}

The following is the error output:

TestLombok.java:11: error: incompatible types: String cannot be converted to DateTimeFormatter
    @Builder
    ^

If the DateTimeFormatter.ofPattern("yyyyMMdd") line is changed to DateTimeFormatter.BASIC_ISO_DATE, then the code compiles fine (even though both methods return the same type).

I'm attaching a minimal gradle project that compiles just this file. Use gradle build to see the error.

lombok-test.zip

Most helpful comment

I had similar issue, when using @JsonCreator and @JsonProperty on constructor along with @Builder annotation,
Order of field names in constructor annotated with @JsonProperty needs to be the same as declared in the class, not sure if your problem is related

@Getter
@ToString
@Builder
@JsonDeserialize
@EqualsAndHashCode
public class DataClass {

    private final long id;

    private final String name;

    private final String date;

// Order of this constructor parameters needs to be in the same order as declared in class, not sure why?
    @JsonCreator
    public ClearingFlowCounts(@JsonProperty("name") String name,
                                   @JsonProperty("id") long id, @JsonProperty("date") String date) {

        this.id = id;
        this.name = name;
        this.date = date;
    }
}

All 3 comments

I have no idea what causes this problem. I can reproduce it.

My suggestion is to modify your code and assign the formatter to a local variable (or a static final field). That takes care of this instance of the problem.

I'm closing this issue, even though there is an underlying unknown bug. Based on our experience chasing similar bugs, this can take weeks to find, and it not a common use case.

Why we closed this issue

I had similar issue, when using @JsonCreator and @JsonProperty on constructor along with @Builder annotation,
Order of field names in constructor annotated with @JsonProperty needs to be the same as declared in the class, not sure if your problem is related

@Getter
@ToString
@Builder
@JsonDeserialize
@EqualsAndHashCode
public class DataClass {

    private final long id;

    private final String name;

    private final String date;

// Order of this constructor parameters needs to be in the same order as declared in class, not sure why?
    @JsonCreator
    public ClearingFlowCounts(@JsonProperty("name") String name,
                                   @JsonProperty("id") long id, @JsonProperty("date") String date) {

        this.id = id;
        this.name = name;
        this.date = date;
    }
}

Can confirm @blackuprise 's way to reproduce it and the workaround.

Was this page helpful?
0 / 5 - 0 ratings