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

toBuilder-Method and primitive type boolean #18

Closed
radtke opened this issue May 16, 2024 · 4 comments
Closed

toBuilder-Method and primitive type boolean #18

radtke opened this issue May 16, 2024 · 4 comments

Comments

@radtke
Copy link

radtke commented May 16, 2024

Hi there,

i think maybe there's a bug generating the "toBuilder"-Method for Classes containing primitive type booleans.
The problem is in AbstractBuilderGenerator.accessAttributeOfTargetClass(..) which does not consider standard naming convention for primitive type boolean getter ("is" instead of "get").

Here is an example.

My Class:

@Builder(toBuilder = "from")
public class Subscription {
  private String label;
  private long number;
  private boolean active;

  public Subscription(String label, long number, boolean active) {
    super();
    this.label = label;
    this.number = number;
    this.active = active;
  }

  public String getLabel() {
    return label;
  }

  public void setLabel(String label) {
    this.label = label;
  }

  public long getNumber() {
    return number;
  }

  public void setNumber(long number) {
    this.number = number;
  }

  public boolean isActive() {
    return active;
  }

  public void setActive(boolean active) {
    this.active = active;
  }
}

Generated Builder Class:

@Generated("Jilt-1.5")
public class SubscriptionBuilder {
  private String label;

  private long number;

  private boolean active;

  public static SubscriptionBuilder subscription() {
    return new SubscriptionBuilder();
  }

  public static SubscriptionBuilder from(Subscription subscription) {
    SubscriptionBuilder subscriptionBuilder = new SubscriptionBuilder();
    subscriptionBuilder.label(subscription.getLabel());
    subscriptionBuilder.number(subscription.getNumber());
    subscriptionBuilder.active(subscription.active);
    return subscriptionBuilder;
  }

  public SubscriptionBuilder label(String label) {
    this.label = label;
    return this;
  }

  public SubscriptionBuilder number(long number) {
    this.number = number;
    return this;
  }

  public SubscriptionBuilder active(boolean active) {
    this.active = active;
    return this;
  }

  public Subscription build() {
    return new Subscription(label, number, active);
  }
}

And of course, subscriptionBuilder.active(subscription.active); does not compile.

@skinny85
Copy link
Owner

skinny85 commented May 16, 2024

Hey @radtke,

thanks for opening the issue! That's a great catch, yes, the logic of figuring out the accessor should definitely also try the is<PropertyName> variant as well.

I'll work on a fix soon.

Thanks,
Adam

@radtke
Copy link
Author

radtke commented May 16, 2024

Thanks for your quick reply and your effort!

@skinny85
Copy link
Owner

skinny85 commented Jun 9, 2024

Ok, this has been now fixed on develop (027dbd5), and will be released with the next version of Jilt (1.6).

@skinny85
Copy link
Owner

This bug has been fixed in Jilt version 1.6.

Let me know if you run into any problems related to this, and I'd be happy to reopen this issue.

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

2 participants