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 a class with generic types #3641

Open
cjp0tter opened this issue Mar 26, 2024 · 3 comments
Open

[BUG] @SuperBuilder with a class with generic types #3641

cjp0tter opened this issue Mar 26, 2024 · 3 comments

Comments

@cjp0tter
Copy link

cjp0tter commented Mar 26, 2024

Describe the bug
I'm running into an issue attempting to use the @SuperBuilder annotation with a class with generic types.

To Reproduce

Works .. Allows me to provide my own customized constructor.

Sample Code...

@SuperBuilder
@Getter
public abstract class ShapeClass {
   private final String name;
   protected ShapeClass(ShapeClassBuilder<?, ?> b) {
      this.name = b.name;
      validate();
   }
   private void validate() {
      if (name == null || name.isEmpty()) {
         throw new IllegalArgumentException("Name must not be empty");
      }
   }
}

@SuperBuilder
@Getter
public class RectangleClass extends ShapeClass {
   private final int width;
   private final int height;
   protected RectangleClass(RectangleClassBuilder<?, ?> b) {
      super(b);
      this.width = b.width;
      this.height = b.height;
      validate();
   }
   private void validate() {
      if (width <= 0 || height <= 0) {
         throw new IllegalArgumentException("Width and height must be positive integers");
      }
   }
}

Fails (The constructor was taken from de-lomboked code). Error message is...

 "/Users/potterc/vcs/nexus/couchbase-core/src/main/java/com/vu/core/couchbase/context/RectangleClass.java:8: error: constructor RectangleClass(RectangleClassBuilder<T,?,?>) is already defined in class RectangleClass
@SuperBuilder
^
  where T is a type-variable:
    T extends Serializable declared in class RectangleClass
1 error".

Sample Code...

@SuperBuilder
@Getter
public abstract class ShapeClass<T extends Serializable> {
   private final String name;
   protected ShapeClass(ShapeClassBuilder<T, ?, ?> b) {
      this.name = b.name;
      validate();
   }
   private void validate() {
      if (name == null || name.isEmpty()) {
         throw new IllegalArgumentException("Name must not be empty");
      }
   }
}

@SuperBuilder
@Getter
public class RectangleClass<T extends Serializable> extends ShapeClass<T> {
   private final int width;
   private final int height;
   protected RectangleClass(RectangleClassBuilder<T, ?, ?> b) {
      super(b);
      this.width = b.width;
      this.height = b.height;
      validate();
   }
   private void validate() {
      if (width <= 0 || height <= 0) {
         throw new IllegalArgumentException("Width and height must be positive integers");
      }
   }
}

Version info (please complete the following information):

  • version 1.18.32
  • javac 11.0.7
@cjp0tter cjp0tter changed the title [BUG] [BUG] @SuperBuilder with a class with generic types Mar 27, 2024
@aforslow-cubist
Copy link

I noticed this bug yesterday as well and made an SO post on it: https://stackoverflow.com/q/78264233/8353218. Basically, the default constructor works for regular classes, but not generic classes.

Works:

@SuperBuilder
public class MyRegularSuperObject {
    private final String someString;
    
    protected MyRegularSuperObject(MyRegularSuperObject.MyRegularSuperObjectBuilder<?,?> b) {
        someString = b.someString;
    }
}

Does not work:

@SuperBuilder
public class MyGenericSuperObject<T> {
    private final String someString;
    
    protected MyGenericSuperObject(MyGenericSuperObjectBuilder<T,?,?> b) {
        someString = b.someString;
    }
}

The following error is thrown:

error: constructor MyGenericSuperObject(MyGenericSuperObjectBuilder<T,?,?>) is already defined in class MyGenericSuperObject @SuperBuilder ^ where T is a type-variable: T extends Object declared in class MyGenericSuperObject

Hope someone takes a look at this, as this is a very important use case for me and probably many others. Right now, I have to delombok in order to get this working, but it creates so much boiler plate and makes the code so much harder to understand...

@janrieke
Copy link
Contributor

janrieke commented Apr 3, 2024

There is already a PR in the works: #3646

@cjp0tter
Copy link
Author

This is great news. Thanks.

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