DATACMNS-1322 - Add support for Kotlin's copy(…) method.

We now support updating of immutable Kotlin data objects by creating new copies through Kotlin's copy method that is generated along with data classes and immutable properties.

data class DataClassKt(val id: String) {
}

data class ExtendedDataClassKt(val id: String, val name: String) {
}
This commit is contained in:
Mark Paluch
2018-05-22 16:08:49 +02:00
committed by Oliver Gierke
parent 80db17d6ba
commit 79119000bb
8 changed files with 559 additions and 97 deletions

View File

@@ -84,6 +84,34 @@ public class PersistentPropertyAccessorTests {
assertThat(accessor.getProperty(property)).isEqualTo("value");
}
@Test // DATACMNS-1322
public void shouldSetKotlinDataClassProperty() {
DataClassKt bean = new DataClassKt("foo");
PersistentPropertyAccessor accessor = propertyAccessorFunction.apply(bean);
SamplePersistentProperty property = getProperty(bean, "id");
accessor.setProperty(property, "value");
assertThat(accessor.getBean()).hasFieldOrPropertyWithValue("id", "value");
assertThat(accessor.getBean()).isNotSameAs(bean);
assertThat(accessor.getProperty(property)).isEqualTo("value");
}
@Test // DATACMNS-1322
public void shouldSetExtendedKotlinDataClassProperty() {
ExtendedDataClassKt bean = new ExtendedDataClassKt("foo", "bar");
PersistentPropertyAccessor accessor = propertyAccessorFunction.apply(bean);
SamplePersistentProperty property = getProperty(bean, "id");
accessor.setProperty(property, "value");
assertThat(accessor.getBean()).hasFieldOrPropertyWithValue("id", "value");
assertThat(accessor.getBean()).isNotSameAs(bean);
assertThat(accessor.getProperty(property)).isEqualTo("value");
}
@Test // DATACMNS-1322
public void shouldWitherProperty() {
@@ -108,6 +136,16 @@ public class PersistentPropertyAccessorTests {
assertThatThrownBy(() -> accessor.setProperty(property, "value")).isInstanceOf(UnsupportedOperationException.class);
}
@Test // DATACMNS-1322
public void shouldRejectImmutableKotlinClassPropertyUpdate() {
ValueClassKt bean = new ValueClassKt("foo");
PersistentPropertyAccessor accessor = propertyAccessorFunction.apply(bean);
SamplePersistentProperty property = getProperty(bean, "immutable");
assertThatThrownBy(() -> accessor.setProperty(property, "value")).isInstanceOf(UnsupportedOperationException.class);
}
private static SamplePersistentProperty getProperty(Object bean, String propertyName) {
return mappingContext.getRequiredPersistentEntity(bean.getClass()).getRequiredPersistentProperty(propertyName);