diff --git a/src/main/java/org/mapstruct/intellij/util/TargetUtils.java b/src/main/java/org/mapstruct/intellij/util/TargetUtils.java index 4bbfe44..886ca17 100644 --- a/src/main/java/org/mapstruct/intellij/util/TargetUtils.java +++ b/src/main/java/org/mapstruct/intellij/util/TargetUtils.java @@ -90,8 +90,8 @@ public static PsiType getRelevantType(@NotNull PsiMethod mappingMethod) { } /** - * Extract all public write accessors (public setters and fields) - * with their psi substitutors from the given {@code psiType} + * Extract all public write accessors with their psi substitutors from the given {@code psiType}. + * These accessors are constructor parameters and, if it is not a record, also public fields and setters. * * @param psiType to use to extract the accessors * @param mapStructVersion the MapStruct project version @@ -101,11 +101,8 @@ public static PsiType getRelevantType(@NotNull PsiMethod mappingMethod) { */ public static Map> publicWriteAccessors(@NotNull PsiType psiType, MapStructVersion mapStructVersion, MapstructUtil mapstructUtil, PsiMethod mappingMethod) { - boolean builderSupportPresent = mapStructVersion.isBuilderSupported(); - Pair classAndType = resolveBuilderOrSelfClass( - psiType, - builderSupportPresent && isBuilderEnabled( mappingMethod ) - ); + boolean builderPresent = mapStructVersion.isBuilderSupported() && isBuilderEnabled( mappingMethod ); + Pair classAndType = resolveBuilderOrSelfClass( psiType, builderPresent ); if ( classAndType == null ) { return Collections.emptyMap(); } @@ -116,9 +113,10 @@ builderSupportPresent && isBuilderEnabled( mappingMethod ) TargetType targetType = classAndType.getSecond(); PsiType typeToUse = targetType.type(); - publicWriteAccessors.putAll( publicSetters( psiClass, typeToUse, mapstructUtil, - builderSupportPresent && isBuilderEnabled( mappingMethod ) ) ); - publicWriteAccessors.putAll( publicFields( psiClass ) ); + if ( !psiClass.isRecord() ) { + publicWriteAccessors.putAll( publicSetters( psiClass, typeToUse, mapstructUtil, builderPresent ) ); + publicWriteAccessors.putAll( publicFields( psiClass ) ); + } if ( mapStructVersion.isConstructorSupported() && !targetType.builder() ) { publicWriteAccessors.putAll( constructorParameters( psiClass ) ); diff --git a/src/test/java/org/mapstruct/intellij/MapstructCompletionTestCase.java b/src/test/java/org/mapstruct/intellij/MapstructCompletionTestCase.java index cee7c36..093b70d 100644 --- a/src/test/java/org/mapstruct/intellij/MapstructCompletionTestCase.java +++ b/src/test/java/org/mapstruct/intellij/MapstructCompletionTestCase.java @@ -206,7 +206,7 @@ private void assertCarDtoWithConstructorAutoComplete() { assertThat( myItems ) .extracting( LookupElementPresentation::renderElement ) .usingRecursiveFieldByFieldElementComparator() - .usingElementComparatorIgnoringFields( "myIcon", "myTail" ) + .usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" ) .containsExactlyInAnyOrder( createParameter( "make", "String" ), createParameter( "seatCount", "int" ), @@ -234,7 +234,7 @@ private void assertCarDtoWithConstructorAndSettersAutoComplete() { assertThat( myItems ) .extracting( LookupElementPresentation::renderElement ) .usingRecursiveFieldByFieldElementComparator() - .usingElementComparatorIgnoringFields( "myIcon", "myTail" ) + .usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" ) .containsExactlyInAnyOrder( createParameter( "make", "String" ), createParameter( "seatCount", "int" ), @@ -327,7 +327,7 @@ public void testCarMapperReturnTargetCarDtoWithMultipleConstructorsAndAnnotatedW assertThat( myItems ) .extracting( LookupElementPresentation::renderElement ) .usingRecursiveFieldByFieldElementComparator() - .usingElementComparatorIgnoringFields( "myIcon", "myTail" ) + .usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" ) .containsExactlyInAnyOrder( createParameter( "make", "String" ), createParameter( "seatCount", "int" ), @@ -401,7 +401,7 @@ public void testNestedSecondLevelAutoCompleteConstructorTargetProperty() { assertThat( myItems ) .extracting( LookupElementPresentation::renderElement ) .usingRecursiveFieldByFieldElementComparator() - .usingElementComparatorIgnoringFields( "myIcon", "myTail" ) + .usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" ) .containsExactlyInAnyOrder( createParameter( "name", "String" ) ); @@ -451,7 +451,7 @@ public void testMultipleSourceParametersUpdateMapping() { assertThat( myItems ) .extracting( LookupElementPresentation::renderElement ) //For some reason the icon is empty in the returned items. However, in actual completion it is OK - .usingElementComparatorIgnoringFields( "myIcon" ) + .usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon" ) .containsExactlyInAnyOrder( createParameter( "source1", "Car" ), createParameter( "source2", "Car" ), diff --git a/testData/inspection/UnmappedRecordTargetProperties.java b/testData/inspection/UnmappedRecordTargetProperties.java index 87fe14c..e459647 100644 --- a/testData/inspection/UnmappedRecordTargetProperties.java +++ b/testData/inspection/UnmappedRecordTargetProperties.java @@ -12,7 +12,13 @@ record Source(String name, String matching, String moreSource, String onlyInSource) { } -record Target(String testName, String matching, String moreTarget) { } +record Target(String testName, String matching, String moreTarget) { + + public Target restrict(Target target) { + return this; + } + +} interface NotMapStructMapper { diff --git a/testData/inspection/UnmappedRecordTargetProperties_after.java b/testData/inspection/UnmappedRecordTargetProperties_after.java index 0f1681d..545fc40 100644 --- a/testData/inspection/UnmappedRecordTargetProperties_after.java +++ b/testData/inspection/UnmappedRecordTargetProperties_after.java @@ -12,7 +12,13 @@ record Source(String name, String matching, String moreSource, String onlyInSource) { } -record Target(String testName, String matching, String moreTarget) { } +record Target(String testName, String matching, String moreTarget) { + + public Target restrict(Target target) { + return this; + } + +} interface NotMapStructMapper {