DEV Community

Discussion on: ☕️ Immutables/AutoValue/Lombok 🔥 Which One?

Collapse
 
cchacin profile image
Carlos Chacin ☕👽

I just checked the Lombok generated code and I cannot see the utility method for copying/cloning:

$ javap MyModel$MyModelBuilder
public class lombok2.MyModel$MyModelBuilder {
  lombok2.MyModel$MyModelBuilder();
  public lombok2.MyModel$MyModelBuilder myOptional(java.util.Optional<java.lang.Integer>);
  public lombok2.MyModel$MyModelBuilder myString(java.lang.String);
  public lombok2.MyModel$MyModelBuilder myList(java.util.List<java.lang.String>);
  public lombok2.MyModel build();
  public java.lang.String toString();
}

Can you point me to an example or documentation around that?

Thread Thread
 
mmiikkkkaa profile image
Mikka

Its some time now, but maybe it still helps. The copy-function of lombok is @With, which creates a copy with one property changed.

For your example it might be something like
myModel1.withMyString("other string")
which will create a copy of myModel1 with the changed myString value.

Thats what I also like about lombok, that you can use just the parts you need. Immutable is hard to use for legacy code, since you need to adapt all at once, while lombok leaves you with the chance to enhance classes with just the stuff you need. Copy a class? Add @With to it. Want a builder? Add @Builder to a class. Get rid of existing getters and setters? Just add @Getter and/or @Setter to it and delete whats already there.