1818import java .util .Set ;
1919import java .util .stream .Stream ;
2020
21+ import com .intellij .codeInsight .AnnotationUtil ;
22+ import com .intellij .codeInsight .MetaAnnotationUtil ;
2123import com .intellij .lang .jvm .JvmModifier ;
2224import com .intellij .openapi .util .Pair ;
23- import com .intellij .psi .ElementManipulators ;
2425import com .intellij .psi .PsiAnnotation ;
2526import com .intellij .psi .PsiAnnotationMemberValue ;
2627import com .intellij .psi .PsiArrayInitializerMemberValue ;
@@ -311,18 +312,16 @@ private static boolean hasBuildMethod(@Nullable PsiType builderType, @NotNull Ps
311312 * Find all defined {@link org.mapstruct.Mapping#target()} for the given method
312313 *
313314 * @param method that needs to be checked
315+ * @param mapStructVersion the MapStruct project version
314316 *
315317 * @return see description
316318 */
317- public static Stream <String > findAllDefinedMappingTargets (@ NotNull PsiMethod method ) {
319+ public static Stream <String > findAllDefinedMappingTargets (@ NotNull PsiMethod method ,
320+ MapStructVersion mapStructVersion ) {
318321 //TODO cache
319322 PsiAnnotation mappings = findAnnotation ( method , true , MapstructUtil .MAPPINGS_ANNOTATION_FQN );
320- Stream <PsiAnnotation > mappingsAnnotations ;
321- if ( mappings == null ) {
322- mappingsAnnotations = Stream .of ( method .getModifierList ().getAnnotations () )
323- .filter ( TargetUtils ::isMappingAnnotation );
324- }
325- else {
323+ Stream <PsiAnnotation > mappingsAnnotations = Stream .empty ();
324+ if ( mappings != null ) {
326325 //TODO maybe there is a better way to do this, but currently I don't have that much knowledge
327326 PsiNameValuePair mappingsValue = findDeclaredAttribute ( mappings , null );
328327 if ( mappingsValue != null && mappingsValue .getValue () instanceof PsiArrayInitializerMemberValue ) {
@@ -334,18 +333,26 @@ public static Stream<String> findAllDefinedMappingTargets(@NotNull PsiMethod met
334333 else if ( mappingsValue != null && mappingsValue .getValue () instanceof PsiAnnotation ) {
335334 mappingsAnnotations = Stream .of ( (PsiAnnotation ) mappingsValue .getValue () );
336335 }
337- else {
338- mappingsAnnotations = Stream .empty ();
339- }
340336 }
341337
342- return mappingsAnnotations
343- .map ( psiAnnotation -> psiAnnotation .findDeclaredAttributeValue ( "target" ) )
338+ Stream <PsiAnnotation > mappingAnnotations = findMappingAnnotations ( method , mapStructVersion );
339+
340+ return Stream .concat ( mappingAnnotations , mappingsAnnotations )
341+ .map ( psiAnnotation -> AnnotationUtil .getDeclaredStringAttributeValue ( psiAnnotation , "target" ) )
344342 .filter ( Objects ::nonNull )
345- .map ( ElementManipulators ::getValueText )
346343 .filter ( s -> !s .isEmpty () );
347344 }
348345
346+ private static Stream <PsiAnnotation > findMappingAnnotations (@ NotNull PsiMethod method ,
347+ MapStructVersion mapStructVersion ) {
348+ if ( mapStructVersion .isConstructorSupported () ) {
349+ // Meta annotations support was added when constructor support was added
350+ return MetaAnnotationUtil .findMetaAnnotations ( method , Collections .singleton ( MAPPING_ANNOTATION_FQN ) );
351+ }
352+ return Stream .of ( method .getModifierList ().getAnnotations () )
353+ .filter ( TargetUtils ::isMappingAnnotation );
354+ }
355+
349356 /**
350357 * @param memberValue that needs to be checked
351358 *
0 commit comments