DATACMNS-1126 - Add class-generating entity instantiation support for Kotlin.
We now discover Kotlin constructors and apply parameter defaulting to allow construction of immutable value objects. Constructor discovery uses primary constructors by default and considers PersistenceConstructor annotations.
KotlinClassGeneratingEntityInstantiator can instantiate Kotlin classes with default parameters by resolving the synthetic constructor. Null values translate to parameter defaults.
class Person(val firstname: String = "Walter") {
}
class Address(val street: String, val city: String) {
@PersistenceConstructor
constructor(street: String = "Unknown", city: String = "Unknown", country: String) : this(street, city)
}
Original pull request: #233.
This commit is contained in:
committed by
Oliver Gierke
parent
e4e677d6b1
commit
22b37800ec
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert
|
||||
|
||||
import com.nhaarman.mockito_kotlin.any
|
||||
import com.nhaarman.mockito_kotlin.doReturn
|
||||
import com.nhaarman.mockito_kotlin.whenever
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
import org.springframework.data.mapping.PersistentEntity
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty
|
||||
import org.springframework.data.mapping.model.ParameterValueProvider
|
||||
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer
|
||||
|
||||
/**
|
||||
* Unit tests for [ClassGeneratingKotlinEntityInstantiator] creating instances using Kotlin data classes.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class ClassGeneratingKotlinEntityInstantiatorUnitTests {
|
||||
|
||||
@Mock lateinit var entity: PersistentEntity<*, *>
|
||||
@Mock lateinit var provider: ParameterValueProvider<SamplePersistentProperty>
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should create instance`() {
|
||||
|
||||
val entity = this.entity as PersistentEntity<Contact, SamplePersistentProperty>
|
||||
val constructor = PreferredConstructorDiscoverer.discover<Contact, SamplePersistentProperty>(Contact::class.java)
|
||||
|
||||
doReturn("Walter", "White").`when`(provider).getParameterValue<SamplePersistentProperty>(any())
|
||||
doReturn(constructor).whenever(entity).persistenceConstructor
|
||||
doReturn(constructor.constructor.declaringClass).whenever(entity).type
|
||||
|
||||
val instance: Contact = ClassGeneratingKotlinEntityInstantiator().createInstance(entity, provider)
|
||||
|
||||
Assertions.assertThat(instance.firstname).isEqualTo("Walter")
|
||||
Assertions.assertThat(instance.lastname).isEqualTo("White")
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should create instance and fill in defaults`() {
|
||||
|
||||
val entity = this.entity as PersistentEntity<ContactWithDefaulting, SamplePersistentProperty>
|
||||
val constructor = PreferredConstructorDiscoverer.discover<ContactWithDefaulting, SamplePersistentProperty>(ContactWithDefaulting::class.java)
|
||||
|
||||
doReturn("Walter", null, "Skyler", null, null, null, null, null, null, null, /* 0-9 */
|
||||
null, null, null, null, null, null, null, null, null, null, /* 10-19 */
|
||||
null, null, null, null, null, null, null, null, null, null, /* 20-29 */
|
||||
null, "Walter", null, "Junior", null).`when`(provider).getParameterValue<SamplePersistentProperty>(any())
|
||||
doReturn(constructor).whenever(entity).persistenceConstructor
|
||||
doReturn(constructor.constructor.declaringClass).whenever(entity).type
|
||||
|
||||
val instance: ContactWithDefaulting = ClassGeneratingKotlinEntityInstantiator().createInstance(entity, provider)
|
||||
|
||||
Assertions.assertThat(instance.prop0).isEqualTo("Walter")
|
||||
Assertions.assertThat(instance.prop2).isEqualTo("Skyler")
|
||||
Assertions.assertThat(instance.prop31).isEqualTo("Walter")
|
||||
Assertions.assertThat(instance.prop32).isEqualTo("White")
|
||||
Assertions.assertThat(instance.prop33).isEqualTo("Junior")
|
||||
Assertions.assertThat(instance.prop34).isEqualTo("White")
|
||||
}
|
||||
|
||||
data class Contact(val firstname: String, val lastname: String)
|
||||
|
||||
data class ContactWithDefaulting(val prop0: String, val prop1: String = "White", val prop2: String,
|
||||
val prop3: String = "White", val prop4: String = "White", val prop5: String = "White",
|
||||
val prop6: String = "White", val prop7: String = "White", val prop8: String = "White",
|
||||
val prop9: String = "White", val prop10: String = "White", val prop11: String = "White",
|
||||
val prop12: String = "White", val prop13: String = "White", val prop14: String = "White",
|
||||
val prop15: String = "White", val prop16: String = "White", val prop17: String = "White",
|
||||
val prop18: String = "White", val prop19: String = "White", val prop20: String = "White",
|
||||
val prop21: String = "White", val prop22: String = "White", val prop23: String = "White",
|
||||
val prop24: String = "White", val prop25: String = "White", val prop26: String = "White",
|
||||
val prop27: String = "White", val prop28: String = "White", val prop29: String = "White",
|
||||
val prop30: String = "White", val prop31: String = "White", val prop32: String = "White",
|
||||
val prop33: String, val prop34: String = "White"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert
|
||||
|
||||
import com.nhaarman.mockito_kotlin.any
|
||||
import com.nhaarman.mockito_kotlin.doReturn
|
||||
import com.nhaarman.mockito_kotlin.whenever
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
import org.springframework.data.mapping.PersistentEntity
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty
|
||||
import org.springframework.data.mapping.model.ParameterValueProvider
|
||||
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer
|
||||
|
||||
/**
|
||||
* Unit tests for [ReflectionEntityInstantiator] creating instances using Kotlin data classes.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class ReflectionEntityInstantiatorDataClassUnitTests {
|
||||
|
||||
@Mock lateinit var entity: PersistentEntity<*, *>
|
||||
@Mock lateinit var provider: ParameterValueProvider<SamplePersistentProperty>
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should create instance`() {
|
||||
|
||||
val entity = this.entity as PersistentEntity<Contact, SamplePersistentProperty>
|
||||
val constructor = PreferredConstructorDiscoverer.discover<Contact, SamplePersistentProperty>(Contact::class.java)
|
||||
|
||||
doReturn("Walter", "White").`when`(provider).getParameterValue<SamplePersistentProperty>(any())
|
||||
doReturn(constructor).whenever(entity).persistenceConstructor
|
||||
|
||||
val instance: Contact = ReflectionEntityInstantiator.INSTANCE.createInstance(entity, provider)
|
||||
|
||||
Assertions.assertThat(instance.firstname).isEqualTo("Walter")
|
||||
Assertions.assertThat(instance.lastname).isEqualTo("White")
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should create instance and fill in defaults`() {
|
||||
|
||||
val entity = this.entity as PersistentEntity<ContactWithDefaulting, SamplePersistentProperty>
|
||||
val constructor = PreferredConstructorDiscoverer.discover<ContactWithDefaulting, SamplePersistentProperty>(ContactWithDefaulting::class.java)
|
||||
|
||||
doReturn("Walter", null).`when`(provider).getParameterValue<SamplePersistentProperty>(any())
|
||||
doReturn(constructor).whenever(entity).persistenceConstructor
|
||||
|
||||
val instance: ContactWithDefaulting = ReflectionEntityInstantiator.INSTANCE.createInstance(entity, provider)
|
||||
|
||||
Assertions.assertThat(instance.firstname).isEqualTo("Walter")
|
||||
Assertions.assertThat(instance.lastname).isEqualTo("White")
|
||||
}
|
||||
|
||||
data class Contact(val firstname: String, val lastname: String)
|
||||
|
||||
data class ContactWithDefaulting(val firstname: String, val lastname: String = "White")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mapping.model
|
||||
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.Test
|
||||
import org.springframework.data.annotation.PersistenceConstructor
|
||||
import org.springframework.data.mapping.model.AbstractPersistentPropertyUnitTests.*
|
||||
|
||||
/**
|
||||
* Unit tests for [PreferredConstructorDiscoverer].
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class PreferredConstructorDiscovererUnitTests {
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should discover simple constructor`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<Simple, SamplePersistentProperty>(Simple::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should reject two constructors`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<TwoConstructors, SamplePersistentProperty>(TwoConstructors::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should discover annotated constructor`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<AnnotatedConstructors, SamplePersistentProperty>(AnnotatedConstructors::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(2)
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should discover default constructor`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<DefaultConstructor, SamplePersistentProperty>(DefaultConstructor::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should discover default annotated constructor`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<TwoDefaultConstructorsAnnotated, SamplePersistentProperty>(TwoDefaultConstructorsAnnotated::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(3)
|
||||
}
|
||||
|
||||
data class Simple(val firstname: String)
|
||||
|
||||
class TwoConstructors(val firstname: String) {
|
||||
constructor(firstname: String, lastname: String) : this(firstname)
|
||||
}
|
||||
|
||||
class AnnotatedConstructors(val firstname: String) {
|
||||
|
||||
@PersistenceConstructor
|
||||
constructor(firstname: String, lastname: String) : this(firstname)
|
||||
}
|
||||
|
||||
class DefaultConstructor(val firstname: String = "foo") {
|
||||
}
|
||||
|
||||
class TwoDefaultConstructorsAnnotated(val firstname: String = "foo", val lastname: String = "bar") {
|
||||
|
||||
@PersistenceConstructor
|
||||
constructor(firstname: String = "foo", lastname: String = "bar", age: Int) : this(firstname, lastname)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user