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] SuperBuilder with generics #3510

Open
hamidsultanzadeh opened this issue Sep 24, 2023 · 4 comments
Open

[BUG] SuperBuilder with generics #3510

hamidsultanzadeh opened this issue Sep 24, 2023 · 4 comments

Comments

@hamidsultanzadeh
Copy link

I have been trying to make an architecture for my database models. and I'm using BaseEntity and IsDeletedEntity for storing common fields. and BaseEntity accepts a generic but IsDeletedEntity doesn't.
So let me give you an example from my models:

It is my child model which extends BaseEntity model with generic Long type

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Employee extends BaseEntity<Long> {

    Long userId;

}

The BaseEntity

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@FieldDefaults(level = AccessLevel.PRIVATE)
public class BaseEntity<ID> extends IsDeletedEntity {

    ID id;

}

IsDeletedEntity

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class IsDeletedEntity {

    boolean isDeleted;

}

And finally, the error is

java: name clash: <ID>builder() in com.webperside.courseerpbackend.models.mybatis.base.BaseEntity and builder() in com.webperside.courseerpbackend.models.mybatis.base.IsDeletedEntity have the same erasure, yet neither hides the other

Actually, there is a solution to fix it. If you just add a generic to IsDeletedEntity, and pass it from BaseEntity, it will work. But I want to follow that way, otherwise, I have to create a dummy generic for IsDeletedEntity. which I don't need it in this model.

Version info (please complete the following information):

  • Lombok version: 1.18.28

  • Platform
    IntelliJ IDEA 2023.1 (Community Edition)
    Build #IC-231.8109.175, built on March 28, 2023
    Runtime version: 17.0.6+10-b829.5 aarch64
    VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
    macOS 13.5.1
    GC: G1 Young Generation, G1 Old Generation
    Memory: 2048M
    Cores: 8
    Metal Rendering is ON
    Registry:
    debugger.new.tool.window.layout=true
    ide.experimental.ui=true

    Non-Bundled Plugins:
    Docker (231.8109.217)
    com.liuzhihang.toolkit.mybatis-jump (1.0.1)
    GsonFormatPlus (1.6.1)
    com.intellij.lang.jsgraphql (4.0.0)
    com.bruce.intellijplugin.generatesetter (2.8.3)
    wl-spring-assistant (1.2.222.231)

    Kotlin: 231-1.8.20-IJ8109.175

@gadirovakhadija
Copy link

The error is occurring because both BaseEntity and IsDeletedEntity have a method named builder() that causes a name clash due to erasure when you use a generic type for BaseEntity.
Create a separate builder for BaseEntity and specify the type parameter there:
@DaTa
@SuperBuilder(builderMethodName = "baseEntityBuilder")
Use baseEntityBuilder() instead of builder() when constructing instances of BaseEntity:
BaseEntity entity = BaseEntity.baseEntityBuilder().id(1L).isDeleted(false).build();

@hamidsultanzadeh
Copy link
Author

It works!

Thank you

@janrieke
Copy link
Contributor

That's a limitation due to static method "overriding" in Java (which is not a real overriding).
If feasable, you could make IsDeletedEntity abstract. That would suppress the generation of the builder method.

@hamidsultanzadeh
Copy link
Author

thanks @janrieke I appreciate your help
Yeah, it is about static methods. but I am not sure why I getting clash after putting generic type on BaseEntity.

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

3 participants