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

Fluent method not generated when POJO's constructor formal parameter uses boxed type while field uses primitive #387

Open
k-wall opened this issue Jun 6, 2023 · 1 comment

Comments

@k-wall
Copy link
Contributor

k-wall commented Jun 6, 2023

I'm generating builders for some existing code with POJOs that looks like this:

@Buildable(editableEnabled = false)
public class Boxy {
    private final int p1;
    private final int p2;

    public Boxy(int p1, Integer p2) {
        this.p1 = p1;
        // some business logic to default p2 in the p2 == null case.
        this.p2 = p2;
    }

    public int getP1() {
        return p1;
    }

    public int getP2() {
        return p2;
    }
}

The generated code fails to compile.

[ERROR] /Users/kwall/src/sundrio/examples/builder/hello-inheritance/target/generated-sources/annotations/io/sundr/examples/BoxyFluent.java:[19,11] cannot find symbol
[ERROR]   symbol: method withP2(int)
[ERROR] /Users/kwall/src/sundrio/examples/builder/hello-inheritance/target/generated-sources/annotations/io/sundr/examples/BoxyBuilder.java:[25,13] cannot find symbol
[ERROR]   symbol:   method withP2(int)
[ERROR]   location: variable fluent of type io.sundr.examples.BoxyFluent<?>
[ERROR] /Users/kwall/src/sundrio/examples/builder/hello-inheritance/target/generated-sources/annotations/io/sundr/examples/BoxyBuilder.java:[36,11] cannot find symbol
[ERROR]   symbol: method withP2(int)
[ERROR] /Users/kwall/src/sundrio/examples/builder/hello-inheritance/target/generated-sources/annotations/io/sundr/examples/BoxyBuilder.java:[43,52] cannot find symbol
[ERROR]   symbol:   method getP2()

The code gen has omitted to generate a withP2() method (or the field) in the fluent, yet the builder implementation assumes it will be present. This suggests an inconsistency in the sundrio implementation.

I can imagine to support this use-case, sundrio would need to special casing to use the boxed type in the fluent rather than the primitive. I can well imagine there are gotchas in the detail..

I want to know if this is considered a defect, or an unsupported use-case? If the latter, I wonder if calling this out in the docs might help users, especially those applying builders to existing code.

Thank you for the attention.

@iocanel
Copy link
Collaborator

iocanel commented Jun 14, 2023

@k-wall: I think that the provided code is problematic and will lead to an NullPointerException in case p2 is actually null. I would suggest to switch the type of p2 property to Integer then both will work as expected.

Sundrio, assumes that properties, constructor arguments and getters match. In some cases we do check for boxing (so this might as well be a bug, but not sure if we do it for all cases). I'll need to check and get back to you.

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