0

if we have two obejcts a and b of two classes A and B,

    public class A{
    String prop1;
    String prop2;
    String prop3;
    String prop4;
    
   public A(String prop1, String prop2, String prop3, String prop4) {
        this.prop1 = prop1;
        this.prop2 = prop2;
        this.prop3 = prop3;
        this.prop4 = prop4;
    }

    public A() {
    }
   //... setters and getters
    }




    public class B{
    String prop2;
    String prop3;

public B(String prop2, String prop3) {
        this.prop1 = prop1;
        this.prop2 = prop2;
        this.prop3 = prop3;
        this.prop4 = prop4;
    }

    public B() {
    }

    }

A a = new A("prop1","prop2","prop3","prop4");
B b = new B();
b.setProp3("p");

How can we copy not null properties from b to a ? in the example above, how we can copy prop3 which is not null and ignore prop2 which is null.

Med
  • 73
  • 9
  • 2
    There is not automatism to do so, although there might be libraries (mapstruct is one that comes to my mind) that could do (part of) the job. To give a more specific answer, please provide a clear problem statement. Do you want to convert an `B` into an `A`? Or do you want to update an existing `A` with information from `B`? – Turing85 Dec 29 '22 at 15:20
  • I added more explanations – Med Dec 29 '22 at 15:38
  • Why is the `null`-check needed at all? If the values are not initialized explicitly, they are implicitly initialized with `null`, so "overriding" `null` with `null` does no harm. – Turing85 Dec 29 '22 at 15:48
  • in the example all properties of a are not null, but if we copy b properties to a prop2 will be null, we would keep the original values of prop2 since the comming from b is null – Med Dec 29 '22 at 16:14
  • 1
    [This post and its answers](https://stackoverflow.com/questions/46669594/map-struct-when-source-is-null-target-should-not-be-set-to-null) shows how it can be done with MapStruct. – Turing85 Dec 29 '22 at 16:23

2 Answers2

2

You can use MapStruct.
It's a library that is very easy to use and you can copy all the properties from a class to another.

@Mapper
public interface AToBMapper {
    A toB(A a);
    B toA(B b);
}

A a = new A("prop1","prop2","prop3","prop4");
B = mapper.toB(a);

This is very useful when you need to copy the properties from a DTO to an Entity.

If you don't want to create a new object but update an existing one, it's also possible as you can see here : MapStruct: How to map to existing target?

More exemple here : https://www.baeldung.com/mapstruct

YLombardi
  • 1,755
  • 5
  • 24
  • 45
  • mapstruct will remove the properties that are not commmon – Med Dec 29 '22 at 16:16
  • This does not work if we want to update some `B` with information of some `A`, especially if we want to only consider values of the instance of `A` that are not-`null`. – Turing85 Dec 29 '22 at 16:20
  • 1
    @Med Of course it will, MapStruct maps "by convention". We can, however, always [define explicit mappings](https://mapstruct.org/documentation/stable/reference/html/#basic-mappings). – Turing85 Dec 29 '22 at 16:23
1

It would be possible to accomplish this with reflection or a library like BeanUtils but maybe consider using the KISS principle and just write a simple method for it:

public void copyToA(A a, B b) {
  if (b.getProp2() != null) a.setProp2(b.getProp2());
  if (b.getProp3() != null) a.setProp3(b.getProp3());
}

With BeanUtils you could extend BeanUtilsBean to overwrite copyProperty and check if the value is null and then just use it like this:

var beanUtilsBean = new YourBeanUtilsBean();
beanUtilsBean.copyProperties(a, b);

For more information check out this answer https://stackoverflow.com/a/3521314/11480721:

Ausgefuchster
  • 1,091
  • 5
  • 14