Lombok's @Builder not detecting fields of the Java Record
Asked Answered
C

3

29

I am trying to implement the builder pattern using Lombok's @Builder but it does not detect any of the record fields:

@Builder(builderMethodName = "internalBuilder")
public record ApiError(String title, Map<String, String> errors) {

    public static ApiErrorBuilder builder(String title) {
        return internalBuilder().title(title); // Cannot resolve method 'title' in 'ApiErrorBuilder'
    }
}

When I turn record to a class, everything works as expected:

@Builder(builderMethodName = "internalBuilder")
public class ApiError {

private final String title;
private final Map<String, String> errors;

    public ApiError(String title, Map<String, String> errors) {
        this.title = title;
        this.errors = errors;
    }

    public static ApiErrorBuilder builder(String title) {
        return internalBuilder().title(title);
    }

    // getters

}

Is this happening because Lombok currently does not work well with records yet?

I am using IntelliJ and Lombok 1.18.22

Circumscribe answered 3/11, 2021 at 12:40 Comment(3)
Please try if you can use maven/gradle to build. In my case mvn clean compile is working fine and IntelliJ is having compile issue.Horacehoracio
@Horacehoracio yeah you were right. Tried with maven and build was successful. Seems like it's an IntelliJ issue.Circumscribe
What did you expect? They both generate code, and both are new(ish) features. One or the other has to generate its code first. Whichever one that is will not recognize the other... because the other will not yet exist at that point. The accepted answer makes a lot of sense. Most features would more or less implement themselves, and then there are wrinkles these.Hyetography
V
45

Post fix

It works again!

@Builder
public record MyRecord(String myField) {
}

Pre fix

It was a known IntelliJ bug. There was, however, a workaround:

public record MyRecord(String myField) {
    @Builder public MyRecord {}
} 

Important: Once you insert the @builder inside the record, you must remove the @builder above it

Viperine answered 12/1, 2022 at 8:37 Comment(0)
N
12

According to this, records are supported from Lombok version v1.18.20

@Builder on records is supported since the last release v1.18.20. Which version are you using?

Note that this may also be just an IDE issue. If you are using IntelliJ, it may not be supported, yet.

Probably an IntelliJ issue. Try writing the code without IntelliJ auto-complete, see if it compiles. If it does, it's an IntelliJ issue; if it does not, something is wrong with your code.

Nunnally answered 3/11, 2021 at 12:57 Comment(2)
The code itself is fine as I when change the record to class everything works as expected (@Builder detects and generates methods for the fields)Circumscribe
Just raised a ticket Unexpected compile error with @Builder on recordHoracehoracio
I
7

It's an IntelliJ issue. Upgrading IntelliJ version fixed this problem.

Insipience answered 13/1, 2023 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.