Lombok: subclass 's builder() can't override the parentclass 's builder()

Created on 24 May 2017  路  10Comments  路  Source: projectlombok/lombok

I meet a problem that subclass 's builder() can't override the parentclass 's builder() when i use the @Builder annotation.

First,I have a parentClass:

@Data
@Builder
public class Audio  {

    private String id;
    private String url;  
    private String name;  
    private String author;  
    private String category; 
}

And,There is a subClass:

@Data
@Builder
public class Music extends Audio  {

    private long length; 
    private String image; 
    private String lrcUrl; 
    private String timeLength; 
    private String wordsAuthor;
    private String songAuthor; 
}

And some errors occoured when i compile this ,and with a tip that Music 's builder() can't override the Audio 's builder() ,what should i do?

Most helpful comment

What is the status on this? Anything moving forward?

All 10 comments

I also encountered the same problem

You can use Builder.builderMethodName to change the method name, so you get rid of the error. However, the Music.Builder won't know the parent fields. Recently, there were some attempts here to allow it, but AFAIK they didn't make it to a release yet.

I had the same problem and found a workaround to fix this issue.

import lombok.Data;

@Data
public abstract class AbstractA {
    private final String a;
}
import lombok.Builder;

public class A extends AbstractA {

    @Builder
    public A(final String a) {
        super(a);
    }
}
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class B extends AbstractA {
    private final String b;

    @Builder
    public B(final String a, final String b) {
        super(a);
        this.b = b;
    }
}

What is the status on this? Anything moving forward?

This will be solved by the new experimental @SuperBuilder annotation, which will be available in the next lombok release.
You can already try it with the current snapshot release (https://projectlombok.org/download-edge).

Can not wait to see the enhancement

@kingraser Don't wait, download it.

As a quick fix, you can just name your builder with a parameter.
@Builder(builderMethodName = "builderName")

As a quick fix, you can just name your builder with a parameter.
@Builder(builderMethodName = "builderName")

Doesn't work if your parent class has a @Builder annotation used. Verified using IntelliJ 2019.1.

As a quick fix, you can just name your builder with a parameter.
@Builder(builderMethodName = "builderName")

Doesn't work if your parent class has a @builder annotation used. Verified using IntelliJ 2019.1.

Works for me by adding @EqualsAndHashCode(callSuper=true) to the child class.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lombokissues picture lombokissues  路  42Comments

lex-em picture lex-em  路  61Comments

kevcodez picture kevcodez  路  58Comments

eximius313 picture eximius313  路  45Comments

pbi-qfs picture pbi-qfs  路  37Comments