Fix Kotlin copy method assignability check.
We now resolve only the raw class when checking if a primary constructor argument is assignable to method parameters of the synthetic copy method. Previously we used ResolvableType's assignability check which considered generic type arguments. As the type resolution between the KType and copy method type is not symmetric, the check only succeeded in cases where both types could be resolved to the same type/assignable type. Using projections or Any caused asymmetric resolution and therefor the assignability check returned non-assignable. Closes #2324.
This commit is contained in:
@@ -278,7 +278,8 @@ class KotlinCopyMethod {
|
||||
|
||||
Type parameterType = ReflectJvmMapping.getJavaType(source);
|
||||
|
||||
return ResolvableType.forClass(target).isAssignableFrom(ResolvableType.forType(parameterType));
|
||||
Class<?> rawClass = ResolvableType.forType(parameterType).getRawClass();
|
||||
return rawClass == null || target.isAssignableFrom(rawClass);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -73,11 +73,10 @@ class KotlinCopyMethodUnitTests {
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test // #2324
|
||||
@Test // #2324, #2336
|
||||
void shouldDetermineCopyMethodForParametrizedType() {
|
||||
|
||||
Optional<KotlinCopyMethod> copyMethod = KotlinCopyMethod.findCopyMethod(ImmutableKotlinPerson.class);
|
||||
|
||||
assertThat(copyMethod).isPresent();
|
||||
assertThat(KotlinCopyMethod.findCopyMethod(ImmutableKotlinPerson.class)).isPresent();
|
||||
assertThat(KotlinCopyMethod.findCopyMethod(DataClassWithParametrizedCollections.class)).isPresent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,3 +65,12 @@ data class ImmutableKotlinPerson(
|
||||
@Id val name: String,
|
||||
val wasOnboardedBy: List<ImmutableKotlinPerson>
|
||||
)
|
||||
|
||||
data class DataClassWithParametrizedCollections<T>(
|
||||
val id: String? = null,
|
||||
val flags: Map<out String, Any>,
|
||||
val stringStringFlags: Map<in String, String>,
|
||||
val parametrized: List<T>,
|
||||
val anyList: List<*>
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user