1111import com .intellij .psi .PsiElement ;
1212import com .intellij .psi .PsiLiteralExpression ;
1313import com .intellij .psi .PsiMethod ;
14+ import com .intellij .psi .PsiNameValuePair ;
1415import com .intellij .psi .impl .source .tree .java .PsiAnnotationParamListImpl ;
1516import org .jetbrains .annotations .NotNull ;
1617import org .jetbrains .annotations .Nullable ;
1718import org .mapstruct .intellij .MapStructBundle ;
1819import org .mapstruct .intellij .util .MapstructUtil ;
20+ import org .mapstruct .intellij .util .SourceUtils ;
1921
2022/**
2123 * Inspection that checks if inside a @Mapping at least one source property is defined
@@ -27,17 +29,26 @@ public class NoSourcePropertyDefinedInspection extends MappingAnnotationInspecti
2729 @ Override
2830 void visitMappingAnnotation ( @ NotNull ProblemsHolder problemsHolder , @ NotNull PsiAnnotation psiAnnotation ,
2931 @ NotNull MappingAnnotation mappingAnnotation ) {
30- if (mappingAnnotation .hasNoSourceProperties () && !isIgnoreByDefaultEnabled ( psiAnnotation ) ) {
32+ PsiNameValuePair targetProperty = mappingAnnotation .getTargetProperty ();
33+ PsiMethod annotatedMethod = getAnnotatedMethod ( psiAnnotation );
34+ if (targetProperty != null && annotatedMethod != null && mappingAnnotation .hasNoSourceProperties ()
35+ && !isIgnoreByDefaultEnabled ( annotatedMethod ) &&
36+ !hasMatchingSourceProperty ( annotatedMethod , targetProperty ) ) {
3137 problemsHolder .registerProblem ( psiAnnotation ,
3238 MapStructBundle .message ( "inspection.no.source.property" ) );
3339 }
3440 }
3541
36- private static boolean isIgnoreByDefaultEnabled (@ NotNull PsiAnnotation psiAnnotation ) {
37- PsiMethod annotatedMethod = getAnnotatedMethod ( psiAnnotation );
38- if (annotatedMethod == null ) {
42+ private boolean hasMatchingSourceProperty ( @ NotNull PsiMethod annotatedMethod ,
43+ @ NotNull PsiNameValuePair targetProperty ) {
44+ String targetValue = targetProperty .getLiteralValue ();
45+ if (targetValue == null ) {
3946 return false ;
4047 }
48+ return SourceUtils .findAllSourceProperties ( annotatedMethod ).contains ( targetValue );
49+ }
50+
51+ private static boolean isIgnoreByDefaultEnabled ( @ NotNull PsiMethod annotatedMethod ) {
4152 PsiAnnotation beanMappingAnnotation = annotatedMethod .getAnnotation ( MapstructUtil .BEAN_MAPPING_FQN );
4253 if (beanMappingAnnotation == null ) {
4354 return false ;
0 commit comments