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

Code with @Builder annotation fails to compile in Java 9 (works in Java 8) #1807

Closed
garfieldnate opened this issue Aug 7, 2018 · 4 comments

Comments

@garfieldnate
Copy link

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

@rspilker
Copy link
Collaborator

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

@blackuprise
Copy link

blackuprise commented Sep 3, 2020

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;
    }
}

@fmcato
Copy link

fmcato commented Mar 5, 2021

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

@bystrzakk
Copy link

it works! Thanks! <3

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

5 participants