Skip to content

Commit 33135b9

Browse files
authored
#232: Do not resolve public setters and public fields for records (#233)
1 parent 5fe4afe commit 33135b9

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

src/main/java/org/mapstruct/intellij/util/TargetUtils.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ public static PsiType getRelevantType(@NotNull PsiMethod mappingMethod) {
9090
}
9191

9292
/**
93-
* Extract all public write accessors (public setters and fields)
94-
* with their psi substitutors from the given {@code psiType}
93+
* Extract all public write accessors with their psi substitutors from the given {@code psiType}.
94+
* These accessors are constructor parameters and, if it is not a record, also public fields and setters.
9595
*
9696
* @param psiType to use to extract the accessors
9797
* @param mapStructVersion the MapStruct project version
@@ -101,11 +101,8 @@ public static PsiType getRelevantType(@NotNull PsiMethod mappingMethod) {
101101
*/
102102
public static Map<String, Pair<? extends PsiElement, PsiSubstitutor>> publicWriteAccessors(@NotNull PsiType psiType,
103103
MapStructVersion mapStructVersion, MapstructUtil mapstructUtil, PsiMethod mappingMethod) {
104-
boolean builderSupportPresent = mapStructVersion.isBuilderSupported();
105-
Pair<PsiClass, TargetType> classAndType = resolveBuilderOrSelfClass(
106-
psiType,
107-
builderSupportPresent && isBuilderEnabled( mappingMethod )
108-
);
104+
boolean builderPresent = mapStructVersion.isBuilderSupported() && isBuilderEnabled( mappingMethod );
105+
Pair<PsiClass, TargetType> classAndType = resolveBuilderOrSelfClass( psiType, builderPresent );
109106
if ( classAndType == null ) {
110107
return Collections.emptyMap();
111108
}
@@ -116,9 +113,10 @@ builderSupportPresent && isBuilderEnabled( mappingMethod )
116113
TargetType targetType = classAndType.getSecond();
117114
PsiType typeToUse = targetType.type();
118115

119-
publicWriteAccessors.putAll( publicSetters( psiClass, typeToUse, mapstructUtil,
120-
builderSupportPresent && isBuilderEnabled( mappingMethod ) ) );
121-
publicWriteAccessors.putAll( publicFields( psiClass ) );
116+
if ( !psiClass.isRecord() ) {
117+
publicWriteAccessors.putAll( publicSetters( psiClass, typeToUse, mapstructUtil, builderPresent ) );
118+
publicWriteAccessors.putAll( publicFields( psiClass ) );
119+
}
122120

123121
if ( mapStructVersion.isConstructorSupported() && !targetType.builder() ) {
124122
publicWriteAccessors.putAll( constructorParameters( psiClass ) );

src/test/java/org/mapstruct/intellij/MapstructCompletionTestCase.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private void assertCarDtoWithConstructorAutoComplete() {
206206
assertThat( myItems )
207207
.extracting( LookupElementPresentation::renderElement )
208208
.usingRecursiveFieldByFieldElementComparator()
209-
.usingElementComparatorIgnoringFields( "myIcon", "myTail" )
209+
.usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" )
210210
.containsExactlyInAnyOrder(
211211
createParameter( "make", "String" ),
212212
createParameter( "seatCount", "int" ),
@@ -234,7 +234,7 @@ private void assertCarDtoWithConstructorAndSettersAutoComplete() {
234234
assertThat( myItems )
235235
.extracting( LookupElementPresentation::renderElement )
236236
.usingRecursiveFieldByFieldElementComparator()
237-
.usingElementComparatorIgnoringFields( "myIcon", "myTail" )
237+
.usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" )
238238
.containsExactlyInAnyOrder(
239239
createParameter( "make", "String" ),
240240
createParameter( "seatCount", "int" ),
@@ -327,7 +327,7 @@ public void testCarMapperReturnTargetCarDtoWithMultipleConstructorsAndAnnotatedW
327327
assertThat( myItems )
328328
.extracting( LookupElementPresentation::renderElement )
329329
.usingRecursiveFieldByFieldElementComparator()
330-
.usingElementComparatorIgnoringFields( "myIcon", "myTail" )
330+
.usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" )
331331
.containsExactlyInAnyOrder(
332332
createParameter( "make", "String" ),
333333
createParameter( "seatCount", "int" ),
@@ -401,7 +401,7 @@ public void testNestedSecondLevelAutoCompleteConstructorTargetProperty() {
401401
assertThat( myItems )
402402
.extracting( LookupElementPresentation::renderElement )
403403
.usingRecursiveFieldByFieldElementComparator()
404-
.usingElementComparatorIgnoringFields( "myIcon", "myTail" )
404+
.usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" )
405405
.containsExactlyInAnyOrder(
406406
createParameter( "name", "String" )
407407
);
@@ -451,7 +451,7 @@ public void testMultipleSourceParametersUpdateMapping() {
451451
assertThat( myItems )
452452
.extracting( LookupElementPresentation::renderElement )
453453
//For some reason the icon is empty in the returned items. However, in actual completion it is OK
454-
.usingElementComparatorIgnoringFields( "myIcon" )
454+
.usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon" )
455455
.containsExactlyInAnyOrder(
456456
createParameter( "source1", "Car" ),
457457
createParameter( "source2", "Car" ),

testData/inspection/UnmappedRecordTargetProperties.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212

1313
record Source(String name, String matching, String moreSource, String onlyInSource) { }
1414

15-
record Target(String testName, String matching, String moreTarget) { }
15+
record Target(String testName, String matching, String moreTarget) {
16+
17+
public Target restrict(Target target) {
18+
return this;
19+
}
20+
21+
}
1622

1723
interface NotMapStructMapper {
1824

testData/inspection/UnmappedRecordTargetProperties_after.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212

1313
record Source(String name, String matching, String moreSource, String onlyInSource) { }
1414

15-
record Target(String testName, String matching, String moreTarget) { }
15+
record Target(String testName, String matching, String moreTarget) {
16+
17+
public Target restrict(Target target) {
18+
return this;
19+
}
20+
21+
}
1622

1723
interface NotMapStructMapper {
1824

0 commit comments

Comments
 (0)