Modelmapper: How to apply custom mapping when source object is null?
<p>Lets assume I have class <code>MySource</code>:</p>

<pre><code>public class MySource {
    public String fieldA;
    public String fieldB;

    public MySource(String A, String B) {
       this.fieldA = A;
       this.fieldB = B;
    }
}
</code></pre>

<p>and I would like to translate it to object <code>MyTarget</code>:</p>

<pre><code>public class MyTarget {
    public String fieldA;
    public String fieldB;
}
</code></pre>

<p>using default ModelMapper settings I can achieve it in the following way: </p>

<pre><code>ModelMapper modelMapper = new ModelMapper();
MySource src = new MySource("A field", "B field");
MyTarget trg = modelMapper.map(src, MyTarget.class); //success! fields are copied
</code></pre>

<p>It can happen however, that <code>MySource</code> object will be <code>null</code>. In this case, MyTarget will be also <code>null</code>:</p>

<pre><code>    ModelMapper modelMapper = new ModelMapper();
    MySource src = null;
    MyTarget trg = modelMapper.map(src, MyTarget.class); //trg = null
</code></pre>

<p>I would like to specify custom mapping in such a way, that (pseudo code): </p>

<p><code>MySource src != null ? [perform default mapping] : [return new MyTarget()]</code></p>

<p>Does anybody have idea how to write an appropriate converter to achieve that?</p>