3333import org .jetbrains .annotations .NotNull ;
3434import org .jetbrains .annotations .Nullable ;
3535import org .mapstruct .intellij .MapStructBundle ;
36+ import org .mapstruct .intellij .settings .ProjectSettings ;
3637import org .mapstruct .intellij .util .MapStructVersion ;
3738import org .mapstruct .intellij .util .MapstructUtil ;
3839import org .mapstruct .intellij .util .TargetUtils ;
@@ -241,6 +242,25 @@ public void invoke(@NotNull Project project,
241242
242243 }
243244
245+ private static class UnmappedTargetPropertyFixAnnotationSupplier implements Supplier <Collection <PsiAnnotation >> {
246+ private final PsiMethod method ;
247+ private final String target ;
248+
249+ private UnmappedTargetPropertyFixAnnotationSupplier (PsiMethod method , String target ) {
250+ this .method = method ;
251+ this .target = target ;
252+ }
253+
254+ @ Override
255+ public Collection <PsiAnnotation > get () {
256+ String annotationText = ProjectSettings .isPreferSourceBeforeTargetInMapping ( method .getProject () ) ?
257+ "@" + MapstructUtil .MAPPING_ANNOTATION_FQN + "(source = \" \" , target = \" " + target + "\" )" :
258+ "@" + MapstructUtil .MAPPING_ANNOTATION_FQN + "(target = \" " + target + "\" , source = \" \" )" ;
259+ return Collections .singleton ( JavaPsiFacade .getElementFactory ( method .getProject () )
260+ .createAnnotationFromText ( annotationText , null ) );
261+ }
262+ }
263+
244264 /**
245265 * Add unmapped property fix. Property fix that adds a {@link org.mapstruct.Mapping} annotation with the
246266 * given {@code target}
@@ -251,17 +271,12 @@ public void invoke(@NotNull Project project,
251271 * @return the Local Quick fix
252272 */
253273 private static UnmappedTargetPropertyFix createAddUnmappedTargetPropertyFix (PsiMethod method , String target ) {
254- String fqn = MapstructUtil .MAPPING_ANNOTATION_FQN ;
255- Supplier <Collection <PsiAnnotation >> annotationSupplier =
256- () -> Collections .singleton ( JavaPsiFacade .getElementFactory (
257- method .getProject () )
258- .createAnnotationFromText ( "@" + fqn + "(target = \" " + target + "\" , source=\" \" )" , null ) );
259274 String message = MapStructBundle .message ( "inspection.add.unmapped.target.property" , target );
260275 return new UnmappedTargetPropertyFix (
261276 method ,
262277 message ,
263278 MapStructBundle .message ( "intention.add.unmapped.target.property" ),
264- annotationSupplier
279+ new UnmappedTargetPropertyFixAnnotationSupplier ( method , target )
265280 );
266281 }
267282
0 commit comments