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

Generated copy() method does not copy all fields #176

Open
jza70 opened this issue Sep 8, 2022 · 0 comments
Open

Generated copy() method does not copy all fields #176

jza70 opened this issue Sep 8, 2022 · 0 comments

Comments

@jza70
Copy link

jza70 commented Sep 8, 2022

PojoBuilder generates copy() method with annotation @GeneratePojoBuilder(withCopyMethod = true). It may skip generation of this method if all pojo properties are final, but should work for all non-final properties. Right? Wrong.

If your class defines custom accessor methods with Optionals, like this one:

@GeneratePojoBuilder(withCopyMethod = true)
public class Flight {
    private String flightNumber;

    private Integer journeyMins;

    private Integer numStops;

    void setFlightNumber(String flightNumber) {
        this.flightNumber = flightNumber;
    }

    void setJourneyMins(int journeyMins) {
        this.journeyMins = journeyMins;
    }

    void setNumStops(int numStops) {
        this.numStops = numStops;
    }

    public String getFlightNumber() {
        return flightNumber;
    }

    public Optional<Integer> getJourneyMinsOptional() {
        return Optional.ofNullable(journeyMins);
    }

    public Integer getJourneyMins() {
        return journeyMins;
    }

    public Optional<Integer> getNumStopsOptional() {
        return Optional.ofNullable(numStops);
    }
}

PojoBuilder will generate this copy method:

 /**
     * Copies the values from the given pojo into this builder.
     *
     * @param pojo
     * @return this builder
     */
    public FlightBuilder copy(Flight pojo) {
        withFlightNumber(pojo.getFlightNumber());
        return self;
    }

Two properties are missing even though "with" methods are generated properly.

Please fix it.

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

1 participant