title: MapStruct: How to do an @AfterMapping with optional parameters?
body: <p>I have created an abstract mapper class which has the following to methods:</p>
  
  <pre><code>public abstract Dest mapSourceToDest(Source source);
  public abstract Dest mapSourceToDestWithParams(Source source, @Context Param1 param1; @Context Param2 param2);
  </code></pre>
  
  <p>There are two members in <code>Source</code> which are very expensive to convert when doing the conversion to <code>Dest</code> (in terms of performance). <br>
  The first above-mentioned method is supposed to be used when the conversion of all members of <code>source</code> should be done (including the two expensive conversions).<br> The second method is supposed to be used when the result of the conversion of the two "expensive" members is already known before the conversion from <code>source</code> to <code>dest</code> ist done. In this case, the conversion results for these two members are passed as parameters <code>param1</code> and <code>param2</code>. These parameters are supposed to be used to avoid the expensive conversion. <br>The conversion of the two members is done in a method annotated with <code>@AfterMapping</code>. What I basically want to do in the AfterMapping method is the following:</p>
  
  <ul>
  <li>if the two parameters were not provided, do the expensive conversion</li>
  <li>if the two parameters were provided, use them and do not do the conversion</li>
  </ul>
  
  <p>It should be basically like this:</p>
  
  <pre><code>@AfterMapping
  protected void setExpensiveParams(Source source, @MappingTarget Dest dest, 
                                     @Context Param1 param1, @Context Param2 param2) {
      if (param1 == null || param2 == null) {
          performExpensiveConversionForParam1AndParam2();
      } else {
         dest.setParam1(param1);
         dest.setParam2(param2); 
      } 
  }
  </code></pre>
  
  <p>This does not work in both cases though as this AfterMapping method only gets called when the two parameters annotated with @Context were provided. This is as expected as the MapStruct documentation states clearly that context parameters like these may not be null. </p>
  
  <p>I could not figure out a way to do perform these two alternative kinds of AfterMappings which depend on the existence of the optional parameters within one mapper class. My workaround for now is to use two mapper classes. I would really like to handle this with one mapper class though.</p>