Infer Kotlin null-safety from type variables
This commit removes the constraint from type variables in PropertyResolver, JdbcOperations and RestOperations Kotlin extensions in order to get null-safety inferred from the type declared by the user. Closes gh-22687
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -39,8 +39,8 @@ operator fun PropertyResolver.get(key: String) : String? = getProperty(key)
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.1
|
||||
*/
|
||||
inline fun <reified T: Any?> PropertyResolver.getProperty(key: String) : T? =
|
||||
getProperty(key, T::class.java)
|
||||
inline fun <reified T> PropertyResolver.getProperty(key: String) : T =
|
||||
getProperty(key, T::class.java) as T
|
||||
|
||||
/**
|
||||
* Extension for [PropertyResolver.getRequiredProperty] providing a
|
||||
@@ -49,5 +49,5 @@ inline fun <reified T: Any?> PropertyResolver.getProperty(key: String) : T? =
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.1
|
||||
*/
|
||||
inline fun <reified T: Any> PropertyResolver.getRequiredProperty(key: String) : T =
|
||||
inline fun <reified T> PropertyResolver.getRequiredProperty(key: String) : T =
|
||||
getRequiredProperty(key, T::class.java)
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.core.env
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import org.junit.Test
|
||||
@@ -27,22 +28,32 @@ import org.junit.Test
|
||||
*/
|
||||
class PropertyResolverExtensionsTests {
|
||||
|
||||
val propertyResolver = mockk<PropertyResolver>(relaxed = true)
|
||||
val propertyResolver = mockk<PropertyResolver>()
|
||||
|
||||
@Test
|
||||
fun `get operator`() {
|
||||
every { propertyResolver.getProperty("name") } returns "foo"
|
||||
propertyResolver["name"]
|
||||
verify { propertyResolver.getProperty("name") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getProperty extension`() {
|
||||
every { propertyResolver.getProperty("name", String::class.java) } returns "foo"
|
||||
propertyResolver.getProperty<String>("name")
|
||||
verify { propertyResolver.getProperty("name", String::class.java) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getProperty extension with nullable type`() {
|
||||
every { propertyResolver.getProperty("name", String::class.java) } returns null
|
||||
propertyResolver.getProperty<String?>("name")
|
||||
verify { propertyResolver.getProperty("name", String::class.java) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getRequiredProperty extension`() {
|
||||
every { propertyResolver.getRequiredProperty("name", String::class.java) } returns "foo"
|
||||
propertyResolver.getRequiredProperty<String>("name")
|
||||
verify { propertyResolver.getRequiredProperty("name", String::class.java) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user