55 */
66package org .mapstruct .intellij .inspection ;
77
8+ import java .util .ArrayList ;
9+ import java .util .Collection ;
10+ import java .util .Collections ;
11+ import java .util .List ;
812import java .util .Set ;
913import java .util .function .Supplier ;
1014import java .util .stream .Collectors ;
@@ -90,18 +94,26 @@ public void visitMethod(PsiMethod method) {
9094 .sorted ()
9195 .collect ( Collectors .joining ( ", " ) )
9296 );
93- UnmappedTargetPropertyFix [] quickFixes = allTargetProperties .stream ()
97+ List <UnmappedTargetPropertyFix > quickFixes = new ArrayList <>( missingTargetProperties * 2 + 1 );
98+
99+ allTargetProperties .stream ()
94100 .sorted ()
95101 .flatMap ( property -> Stream .of (
96102 createAddIgnoreUnmappedTargetPropertyFix ( method , property ),
97103 createAddUnmappedTargetPropertyFix ( method , property )
98104 ) )
99- .toArray ( UnmappedTargetPropertyFix []::new );
105+ .forEach ( quickFixes ::add );
106+
107+ if ( missingTargetProperties > 1 ) {
108+ // If there is more than one add ignore all
109+ quickFixes .add ( createAddIgnoreAllUnmappedTargetPropertiesFix ( method , allTargetProperties ) );
110+ }
111+
100112 //noinspection ConstantConditions
101113 holder .registerProblem (
102114 method .getNameIdentifier (),
103115 descriptionTemplate ,
104- quickFixes
116+ quickFixes . toArray ( UnmappedTargetPropertyFix . EMPTY_ARRAY )
105117 );
106118 }
107119 }
@@ -137,14 +149,16 @@ private static PsiType getTargetType(PsiMethod method) {
137149
138150 private static class UnmappedTargetPropertyFix extends LocalQuickFixOnPsiElement {
139151
152+ private static final UnmappedTargetPropertyFix [] EMPTY_ARRAY = new UnmappedTargetPropertyFix [0 ];
153+
140154 private final String myText ;
141155 private final String myFamilyName ;
142- private final Supplier <PsiAnnotation > myAnnotationSupplier ;
156+ private final Supplier <Collection < PsiAnnotation > > myAnnotationSupplier ;
143157
144158 private UnmappedTargetPropertyFix (@ NotNull PsiMethod modifierListOwner ,
145159 @ NotNull String text ,
146160 @ NotNull String familyName ,
147- @ NotNull Supplier <PsiAnnotation > annotationSupplier ) {
161+ @ NotNull Supplier <Collection < PsiAnnotation > > annotationSupplier ) {
148162 super ( modifierListOwner );
149163 myText = text ;
150164 myFamilyName = familyName ;
@@ -186,7 +200,9 @@ public void invoke(@NotNull Project project,
186200 @ NotNull PsiElement endElement ) {
187201 PsiMethod mappingMethod = (PsiMethod ) startElement ;
188202
189- addMappingAnnotation ( project , mappingMethod , myAnnotationSupplier .get () );
203+ for ( PsiAnnotation annotation : myAnnotationSupplier .get () ) {
204+ addMappingAnnotation ( project , mappingMethod , annotation );
205+ }
190206 }
191207
192208 }
@@ -202,8 +218,10 @@ public void invoke(@NotNull Project project,
202218 */
203219 private static UnmappedTargetPropertyFix createAddUnmappedTargetPropertyFix (PsiMethod method , String target ) {
204220 String fqn = MapstructUtil .MAPPING_ANNOTATION_FQN ;
205- Supplier <PsiAnnotation > annotationSupplier = () -> JavaPsiFacade .getElementFactory ( method .getProject () )
206- .createAnnotationFromText ( "@" + fqn + "(target = \" " + target + "\" , source=\" \" )" , null );
221+ Supplier <Collection <PsiAnnotation >> annotationSupplier =
222+ () -> Collections .singleton ( JavaPsiFacade .getElementFactory (
223+ method .getProject () )
224+ .createAnnotationFromText ( "@" + fqn + "(target = \" " + target + "\" , source=\" \" )" , null ) );
207225 String message = MapStructBundle .message ( "inspection.add.unmapped.target.property" , target );
208226 return new UnmappedTargetPropertyFix (
209227 method ,
@@ -224,8 +242,10 @@ private static UnmappedTargetPropertyFix createAddUnmappedTargetPropertyFix(PsiM
224242 */
225243 private static UnmappedTargetPropertyFix createAddIgnoreUnmappedTargetPropertyFix (PsiMethod method , String target ) {
226244 String fqn = MapstructUtil .MAPPING_ANNOTATION_FQN ;
227- Supplier <PsiAnnotation > annotationSupplier = () -> JavaPsiFacade .getElementFactory ( method .getProject () )
228- .createAnnotationFromText ( "@" + fqn + "(target = \" " + target + "\" , ignore= true)" , null );
245+ Supplier <Collection <PsiAnnotation >> annotationSupplier =
246+ () -> Collections .singleton ( JavaPsiFacade .getElementFactory (
247+ method .getProject () )
248+ .createAnnotationFromText ( "@" + fqn + "(target = \" " + target + "\" , ignore= true)" , null ) );
229249 String message = MapStructBundle .message ( "inspection.add.ignore.unmapped.target.property" , target );
230250 return new UnmappedTargetPropertyFix (
231251 method ,
@@ -234,4 +254,37 @@ private static UnmappedTargetPropertyFix createAddIgnoreUnmappedTargetPropertyFi
234254 annotationSupplier
235255 );
236256 }
257+
258+ /**
259+ * Add ignore all unmapped properties fix. Property fix that adds {@link org.mapstruct.Mapping} annotations that
260+ * ignores all the given {@code targets}
261+ *
262+ * @param method the method to which the property needs to be added
263+ * @param targetProperties the names of the target properties that should be ignored
264+ *
265+ * @return the Local Quick fix
266+ */
267+ private static UnmappedTargetPropertyFix createAddIgnoreAllUnmappedTargetPropertiesFix (PsiMethod method ,
268+ Collection <String > targetProperties ) {
269+ String fqn = MapstructUtil .MAPPING_ANNOTATION_FQN ;
270+ Supplier <Collection <PsiAnnotation >> annotationSupplier = () -> {
271+ List <PsiAnnotation > annotations = new ArrayList <>( targetProperties .size () );
272+ targetProperties .stream ()
273+ .sorted ()
274+ .forEach ( targetProperty -> annotations .add ( JavaPsiFacade .getElementFactory ( method .getProject () )
275+ .createAnnotationFromText (
276+ "@" + fqn + "(target = \" " + targetProperty + "\" , ignore= true)" ,
277+ null
278+ ) ) );
279+ return annotations ;
280+ };
281+ String message = MapStructBundle .message ( "inspection.add.ignore.all.unmapped.target.properties" );
282+ return new UnmappedTargetPropertyFix (
283+ method ,
284+ message ,
285+ MapStructBundle .message ( "intention.add.ignore.all.unmapped.target.properties" ),
286+ annotationSupplier
287+ );
288+ }
289+
237290}
0 commit comments