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:
Sebastien Deleuze 2019-03-27 08:50:42 +01:00
parent 68a529b915
commit cbb5a78aa0
6 changed files with 175 additions and 124 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * @author Sebastien Deleuze
* @since 5.1 * @since 5.1
*/ */
inline fun <reified T: Any?> PropertyResolver.getProperty(key: String) : T? = inline fun <reified T> PropertyResolver.getProperty(key: String) : T =
getProperty(key, T::class.java) getProperty(key, T::class.java) as T
/** /**
* Extension for [PropertyResolver.getRequiredProperty] providing a * Extension for [PropertyResolver.getRequiredProperty] providing a
@ -49,5 +49,5 @@ inline fun <reified T: Any?> PropertyResolver.getProperty(key: String) : T? =
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @since 5.1 * @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) getRequiredProperty(key, T::class.java)

View File

@ -16,6 +16,7 @@
package org.springframework.core.env package org.springframework.core.env
import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify import io.mockk.verify
import org.junit.Test import org.junit.Test
@ -27,22 +28,32 @@ import org.junit.Test
*/ */
class PropertyResolverExtensionsTests { class PropertyResolverExtensionsTests {
val propertyResolver = mockk<PropertyResolver>(relaxed = true) val propertyResolver = mockk<PropertyResolver>()
@Test @Test
fun `get operator`() { fun `get operator`() {
every { propertyResolver.getProperty("name") } returns "foo"
propertyResolver["name"] propertyResolver["name"]
verify { propertyResolver.getProperty("name") } verify { propertyResolver.getProperty("name") }
} }
@Test @Test
fun `getProperty extension`() { fun `getProperty extension`() {
every { propertyResolver.getProperty("name", String::class.java) } returns "foo"
propertyResolver.getProperty<String>("name") propertyResolver.getProperty<String>("name")
verify { propertyResolver.getProperty("name", String::class.java) } 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 @Test
fun `getRequiredProperty extension`() { fun `getRequiredProperty extension`() {
every { propertyResolver.getRequiredProperty("name", String::class.java) } returns "foo"
propertyResolver.getRequiredProperty<String>("name") propertyResolver.getRequiredProperty<String>("name")
verify { propertyResolver.getRequiredProperty("name", String::class.java) } verify { propertyResolver.getRequiredProperty("name", String::class.java) }
} }

View File

@ -24,8 +24,8 @@ import java.sql.ResultSet
* @author Mario Arias * @author Mario Arias
* @since 5.0 * @since 5.0
*/ */
inline fun <reified T : Any> JdbcOperations.queryForObject(sql: String): T? = inline fun <reified T> JdbcOperations.queryForObject(sql: String): T =
queryForObject(sql, T::class.java) queryForObject(sql, T::class.java) as T
/** /**
* Extensions for [JdbcOperations.queryForObject] providing a RowMapper-like function * Extensions for [JdbcOperations.queryForObject] providing a RowMapper-like function
@ -34,8 +34,8 @@ inline fun <reified T : Any> JdbcOperations.queryForObject(sql: String): T? =
* @author Mario Arias * @author Mario Arias
* @since 5.0 * @since 5.0
*/ */
fun <T : Any?> JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T? = inline fun <reified T> JdbcOperations.queryForObject(sql: String, vararg args: Any, crossinline function: (ResultSet, Int) -> T): T =
queryForObject(sql, RowMapper { resultSet, i -> function(resultSet, i) }, *args) queryForObject(sql, RowMapper { resultSet, i -> function(resultSet, i) }, *args) as T
/** /**
* Extension for [JdbcOperations.queryForObject] providing a * Extension for [JdbcOperations.queryForObject] providing a
@ -44,8 +44,8 @@ fun <T : Any?> JdbcOperations.queryForObject(sql: String, vararg args: Any, func
* @author Mario Arias * @author Mario Arias
* @since 5.0 * @since 5.0
*/ */
inline fun <reified T : Any> JdbcOperations.queryForObject(sql: String, args: Array<out Any>, argTypes: IntArray): T? = inline fun <reified T> JdbcOperations.queryForObject(sql: String, args: Array<out Any>, argTypes: IntArray): T? =
queryForObject(sql, args, argTypes, T::class.java) queryForObject(sql, args, argTypes, T::class.java) as T
/** /**
* Extension for [JdbcOperations.queryForObject] providing a * Extension for [JdbcOperations.queryForObject] providing a
@ -54,8 +54,8 @@ inline fun <reified T : Any> JdbcOperations.queryForObject(sql: String, args: Ar
* @author Mario Arias * @author Mario Arias
* @since 5.0 * @since 5.0
*/ */
inline fun <reified T : Any> JdbcOperations.queryForObject(sql: String, args: Array<out Any>): T? = inline fun <reified T> JdbcOperations.queryForObject(sql: String, args: Array<out Any>): T? =
queryForObject(sql, args, T::class.java) queryForObject(sql, args, T::class.java) as T
/** /**
* Extension for [JdbcOperations.queryForList] providing a `queryForList<Foo>("...")` variant. * Extension for [JdbcOperations.queryForList] providing a `queryForList<Foo>("...")` variant.
@ -64,7 +64,7 @@ inline fun <reified T : Any> JdbcOperations.queryForObject(sql: String, args: Ar
* @since 5.0 * @since 5.0
*/ */
@Suppress("EXTENSION_SHADOWED_BY_MEMBER") @Suppress("EXTENSION_SHADOWED_BY_MEMBER")
inline fun <reified T : Any> JdbcOperations.queryForList(sql: String): List<T> = inline fun <reified T> JdbcOperations.queryForList(sql: String): List<T> =
queryForList(sql, T::class.java) queryForList(sql, T::class.java)
/** /**
@ -75,7 +75,7 @@ inline fun <reified T : Any> JdbcOperations.queryForList(sql: String): List<T> =
* @since 5.0 * @since 5.0
*/ */
@Suppress("EXTENSION_SHADOWED_BY_MEMBER") @Suppress("EXTENSION_SHADOWED_BY_MEMBER")
inline fun <reified T : Any> JdbcOperations.queryForList(sql: String, args: Array<out Any>, inline fun <reified T> JdbcOperations.queryForList(sql: String, args: Array<out Any>,
argTypes: IntArray): List<T> = argTypes: IntArray): List<T> =
queryForList(sql, args, argTypes, T::class.java) queryForList(sql, args, argTypes, T::class.java)
@ -86,7 +86,7 @@ inline fun <reified T : Any> JdbcOperations.queryForList(sql: String, args: Arra
* @author Mario Arias * @author Mario Arias
* @since 5.0 * @since 5.0
*/ */
inline fun <reified T : Any> JdbcOperations.queryForList(sql: String, args: Array<out Any>): List<T> = inline fun <reified T> JdbcOperations.queryForList(sql: String, args: Array<out Any>): List<T> =
queryForList(sql, args, T::class.java) queryForList(sql, args, T::class.java)
/** /**
@ -96,9 +96,9 @@ inline fun <reified T : Any> JdbcOperations.queryForList(sql: String, args: Arra
* @author Mario Arias * @author Mario Arias
* @since 5.0 * @since 5.0
*/ */
inline fun <reified T : Any?> JdbcOperations.query(sql: String, vararg args: Any, inline fun <reified T> JdbcOperations.query(sql: String, vararg args: Any,
crossinline function: (ResultSet) -> T): T? = crossinline function: (ResultSet) -> T): T =
query(sql, ResultSetExtractor { function(it) }, *args) query(sql, ResultSetExtractor { function(it) }, *args) as T
/** /**
* Extension for [JdbcOperations.query] providing a RowCallbackHandler-like function * Extension for [JdbcOperations.query] providing a RowCallbackHandler-like function
@ -117,5 +117,5 @@ fun JdbcOperations.query(sql: String, vararg args: Any, function: (ResultSet) ->
* @author Mario Arias * @author Mario Arias
* @since 5.0 * @since 5.0
*/ */
fun <T : Any> JdbcOperations.query(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): List<T> = fun <T> JdbcOperations.query(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): List<T> =
query(sql, RowMapper { rs, i -> function(rs, i) }, *args) query(sql, RowMapper { rs, i -> function(rs, i) }, *args)

View File

@ -16,9 +16,11 @@
package org.springframework.jdbc.core package org.springframework.jdbc.core
import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify import io.mockk.verify
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test import org.junit.Test
import java.sql.* import java.sql.*
@ -30,91 +32,96 @@ import java.sql.*
*/ */
class JdbcOperationsExtensionsTests { class JdbcOperationsExtensionsTests {
val template = mockk<JdbcTemplate>(relaxed = true) val template = mockk<JdbcTemplate>()
val sql = "select age from customer where id = 3"
@Test @Test
fun `queryForObject with reified type parameters`() { fun `queryForObject with reified type parameters`() {
val sql = "select age from customer where id = 3" every { template.queryForObject(sql, any<Class<Int>>()) } returns 2
template.queryForObject<Int>(sql) assertEquals(2, template.queryForObject<Int>(sql))
verify { template.queryForObject(sql, Integer::class.java) } verify { template.queryForObject(sql, any<Class<Int>>()) }
} }
@Test @Test
fun `queryForObject with RowMapper-like function`() { fun `queryForObject with RowMapper-like function`() {
val sql = "select age from customer where id = ?" every { template.queryForObject(sql, any<RowMapper<Int>>(), any<Int>()) } returns 2
template.queryForObject(sql, 3) { rs: ResultSet, _: Int -> rs.getInt(1) } assertEquals(2, template.queryForObject(sql, 3) { rs: ResultSet, _: Int -> rs.getInt(1) })
verify { template.queryForObject(eq(sql), any<RowMapper<Int>>(), eq(3)) } verify { template.queryForObject(eq(sql), any<RowMapper<Int>>(), eq(3)) }
} }
@Test // gh-22682 @Test // gh-22682
fun `queryForObject with nullable RowMapper-like function`() { fun `queryForObject with nullable RowMapper-like function`() {
val sql = "select age from customer where id = ?" every { template.queryForObject(sql, any<RowMapper<Int>>(), 3) } returns null
template.queryForObject(sql, 3) { _, _ -> null as Int? } assertNull(template.queryForObject(sql, 3) { _, _ -> null as Int? })
verify { template.queryForObject(eq(sql), any<RowMapper<Int?>>(), eq(3)) } verify { template.queryForObject(eq(sql), any<RowMapper<Int?>>(), eq(3)) }
} }
@Test @Test
fun `queryForObject with reified type parameters and argTypes`() { fun `queryForObject with reified type parameters and argTypes`() {
val sql = "select age from customer where id = ?"
val args = arrayOf(3) val args = arrayOf(3)
val argTypes = intArrayOf(JDBCType.INTEGER.vendorTypeNumber) val argTypes = intArrayOf(JDBCType.INTEGER.vendorTypeNumber)
template.queryForObject<Int>(sql, args, argTypes) every { template.queryForObject(sql, args, argTypes, any<Class<Int>>()) } returns 2
verify { template.queryForObject(sql, args, argTypes, Integer::class.java) } assertEquals(2, template.queryForObject<Int>(sql, args, argTypes))
verify { template.queryForObject(sql, args, argTypes, any<Class<Int>>()) }
} }
@Test @Test
fun `queryForObject with reified type parameters and args`() { fun `queryForObject with reified type parameters and args`() {
val sql = "select age from customer where id = ?"
val args = arrayOf(3) val args = arrayOf(3)
template.queryForObject<Int>(sql, args) every { template.queryForObject(sql, args, any<Class<Int>>()) } returns 2
verify { template.queryForObject(sql, args, Integer::class.java) } assertEquals(2, template.queryForObject<Int>(sql, args))
verify { template.queryForObject(sql, args, any<Class<Int>>()) }
} }
@Test @Test
fun `queryForList with reified type parameters`() { fun `queryForList with reified type parameters`() {
val sql = "select age from customer where id = 3" val list = listOf(1, 2, 3)
template.queryForList<Int>(sql) every { template.queryForList(sql, any<Class<Int>>()) } returns list
verify { template.queryForList(sql, Integer::class.java) } assertEquals(list, template.queryForList<Int>(sql))
verify { template.queryForList(sql, any<Class<Int>>()) }
} }
@Test @Test
fun `queryForList with reified type parameters and argTypes`() { fun `queryForList with reified type parameters and argTypes`() {
val sql = "select age from customer where id = ?" val list = listOf(1, 2, 3)
val args = arrayOf(3) val args = arrayOf(3)
val argTypes = intArrayOf(JDBCType.INTEGER.vendorTypeNumber) val argTypes = intArrayOf(JDBCType.INTEGER.vendorTypeNumber)
template.queryForList<Int>(sql, args, argTypes) every { template.queryForList(sql, args, argTypes, any<Class<Int>>()) } returns list
verify { template.queryForList(sql, args, argTypes, Integer::class.java) } assertEquals(list, template.queryForList<Int>(sql, args, argTypes))
verify { template.queryForList(sql, args, argTypes, any<Class<Int>>()) }
} }
@Test @Test
fun `queryForList with reified type parameters and args`() { fun `queryForList with reified type parameters and args`() {
val sql = "select age from customer where id = ?" val list = listOf(1, 2, 3)
val args = arrayOf(3) val args = arrayOf(3)
every { template.queryForList(sql, args, any<Class<Int>>()) } returns list
template.queryForList<Int>(sql, args) template.queryForList<Int>(sql, args)
verify { template.queryForList(sql, args, Integer::class.java) } verify { template.queryForList(sql, args, any<Class<Int>>()) }
} }
@Test @Test
fun `query with ResultSetExtractor-like function`() { fun `query with ResultSetExtractor-like function`() {
val sql = "select age from customer where id = ?" every { template.query(eq(sql), any<ResultSetExtractor<Int>>(), eq(3)) } returns 2
template.query<Int>(sql, 3) { rs -> assertEquals(2, template.query<Int>(sql, 3) { rs ->
rs.next() rs.next()
rs.getInt(1) rs.getInt(1)
} })
verify { template.query(eq(sql), any<ResultSetExtractor<Int>>(), eq(3)) } verify { template.query(eq(sql), any<ResultSetExtractor<Int>>(), eq(3)) }
} }
@Test // gh-22682 @Test // gh-22682
fun `query with nullable ResultSetExtractor-like function`() { fun `query with nullable ResultSetExtractor-like function`() {
val sql = "select age from customer where id = ?" every { template.query(eq(sql), any<ResultSetExtractor<Int?>>(), eq(3)) } returns null
template.query<Int?>(sql, 3) { _ -> null } assertNull(template.query<Int?>(sql, 3) { _ -> null })
verify { template.query(eq(sql), any<ResultSetExtractor<Int?>>(), eq(3)) } verify { template.query(eq(sql), any<ResultSetExtractor<Int?>>(), eq(3)) }
} }
@Suppress("RemoveExplicitTypeArguments") @Suppress("RemoveExplicitTypeArguments")
@Test @Test
fun `query with RowCallbackHandler-like function`() { fun `query with RowCallbackHandler-like function`() {
val sql = "select age from customer where id = ?" every { template.query(sql, ofType<RowCallbackHandler>(), 3) } returns Unit
template.query(sql, 3) { rs -> template.query(sql, 3) { rs ->
assertEquals(22, rs.getInt(1)) assertEquals(22, rs.getInt(1))
} }
@ -123,10 +130,11 @@ class JdbcOperationsExtensionsTests {
@Test @Test
fun `query with RowMapper-like function`() { fun `query with RowMapper-like function`() {
val sql = "select age from customer where id = ?" val list = listOf(1, 2, 3)
template.query(sql, 3) { rs, _ -> every { template.query(sql, ofType<RowMapper<*>>(), 3) } returns list
assertEquals(list, template.query(sql, 3) { rs, _ ->
rs.getInt(1) rs.getInt(1)
} })
verify { template.query(sql, ofType<RowMapper<*>>(), 3) } verify { template.query(sql, ofType<RowMapper<*>>(), 3) }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,8 +34,8 @@ import java.net.URI
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.getForObject(url: String, vararg uriVariables: Any): T? = inline fun <reified T> RestOperations.getForObject(url: String, vararg uriVariables: Any): T =
getForObject(url, T::class.java, *uriVariables) getForObject(url, T::class.java, *uriVariables) as T
/** /**
* Extension for [RestOperations.getForObject] providing a `getForObject<Foo>(...)` * Extension for [RestOperations.getForObject] providing a `getForObject<Foo>(...)`
@ -48,8 +48,8 @@ inline fun <reified T: Any> RestOperations.getForObject(url: String, vararg uriV
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.getForObject(url: String, uriVariables: Map<String, Any?>): T? = inline fun <reified T> RestOperations.getForObject(url: String, uriVariables: Map<String, Any?>): T =
getForObject(url, T::class.java, uriVariables) getForObject(url, T::class.java, uriVariables) as T
/** /**
* Extension for [RestOperations.getForObject] providing a `getForObject<Foo>(...)` * Extension for [RestOperations.getForObject] providing a `getForObject<Foo>(...)`
@ -62,8 +62,8 @@ inline fun <reified T: Any> RestOperations.getForObject(url: String, uriVariable
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.getForObject(url: URI): T? = inline fun <reified T> RestOperations.getForObject(url: URI): T =
getForObject(url, T::class.java) getForObject(url, T::class.java) as T
/** /**
* Extension for [RestOperations.getForEntity] providing a `getForEntity<Foo>(...)` * Extension for [RestOperations.getForEntity] providing a `getForEntity<Foo>(...)`
@ -75,7 +75,7 @@ inline fun <reified T: Any> RestOperations.getForObject(url: URI): T? =
* @since 5.0.2 * @since 5.0.2
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.getForEntity(url: URI): ResponseEntity<T> = inline fun <reified T> RestOperations.getForEntity(url: URI): ResponseEntity<T> =
getForEntity(url, T::class.java) getForEntity(url, T::class.java)
/** /**
@ -89,7 +89,7 @@ inline fun <reified T: Any> RestOperations.getForEntity(url: URI): ResponseEntit
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.getForEntity(url: String, vararg uriVariables: Any): ResponseEntity<T> = inline fun <reified T> RestOperations.getForEntity(url: String, vararg uriVariables: Any): ResponseEntity<T> =
getForEntity(url, T::class.java, *uriVariables) getForEntity(url, T::class.java, *uriVariables)
/** /**
@ -102,7 +102,7 @@ inline fun <reified T: Any> RestOperations.getForEntity(url: String, vararg uriV
* @since 5.0.2 * @since 5.0.2
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.getForEntity(url: String, uriVariables: Map<String, *>): ResponseEntity<T> = inline fun <reified T> RestOperations.getForEntity(url: String, uriVariables: Map<String, *>): ResponseEntity<T> =
getForEntity(url, T::class.java, uriVariables) getForEntity(url, T::class.java, uriVariables)
/** /**
@ -115,9 +115,9 @@ inline fun <reified T: Any> RestOperations.getForEntity(url: String, uriVariable
* @since 5.0.2 * @since 5.0.2
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.patchForObject(url: String, request: Any? = null, inline fun <reified T> RestOperations.patchForObject(url: String, request: Any? = null,
vararg uriVariables: Any): T? = vararg uriVariables: Any): T =
patchForObject(url, request, T::class.java, *uriVariables) patchForObject(url, request, T::class.java, *uriVariables) as T
/** /**
* Extension for [RestOperations.patchForObject] providing a `patchForObject<Foo>(...)` * Extension for [RestOperations.patchForObject] providing a `patchForObject<Foo>(...)`
@ -129,9 +129,9 @@ inline fun <reified T: Any> RestOperations.patchForObject(url: String, request:
* @since 5.0.2 * @since 5.0.2
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.patchForObject(url: String, request: Any? = null, inline fun <reified T> RestOperations.patchForObject(url: String, request: Any? = null,
uriVariables: Map<String, *>): T? = uriVariables: Map<String, *>): T =
patchForObject(url, request, T::class.java, uriVariables) patchForObject(url, request, T::class.java, uriVariables) as T
/** /**
* Extension for [RestOperations.patchForObject] providing a `patchForObject<Foo>(...)` * Extension for [RestOperations.patchForObject] providing a `patchForObject<Foo>(...)`
@ -143,8 +143,8 @@ inline fun <reified T: Any> RestOperations.patchForObject(url: String, request:
* @since 5.0.2 * @since 5.0.2
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.patchForObject(url: URI, request: Any? = null): T? = inline fun <reified T> RestOperations.patchForObject(url: URI, request: Any? = null): T =
patchForObject(url, request, T::class.java) patchForObject(url, request, T::class.java) as T
/** /**
* Extension for [RestOperations.postForObject] providing a `postForObject<Foo>(...)` * Extension for [RestOperations.postForObject] providing a `postForObject<Foo>(...)`
@ -157,9 +157,9 @@ inline fun <reified T: Any> RestOperations.patchForObject(url: URI, request: Any
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.postForObject(url: String, request: Any? = null, inline fun <reified T> RestOperations.postForObject(url: String, request: Any? = null,
vararg uriVariables: Any): T? = vararg uriVariables: Any): T =
postForObject(url, request, T::class.java, *uriVariables) postForObject(url, request, T::class.java, *uriVariables) as T
/** /**
* Extension for [RestOperations.postForObject] providing a `postForObject<Foo>(...)` * Extension for [RestOperations.postForObject] providing a `postForObject<Foo>(...)`
@ -172,9 +172,9 @@ inline fun <reified T: Any> RestOperations.postForObject(url: String, request: A
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.postForObject(url: String, request: Any? = null, inline fun <reified T> RestOperations.postForObject(url: String, request: Any? = null,
uriVariables: Map<String, *>): T? = uriVariables: Map<String, *>): T =
postForObject(url, request, T::class.java, uriVariables) postForObject(url, request, T::class.java, uriVariables) as T
/** /**
* Extension for [RestOperations.postForObject] providing a `postForObject<Foo>(...)` * Extension for [RestOperations.postForObject] providing a `postForObject<Foo>(...)`
@ -187,8 +187,8 @@ inline fun <reified T: Any> RestOperations.postForObject(url: String, request: A
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.postForObject(url: URI, request: Any? = null): T? = inline fun <reified T> RestOperations.postForObject(url: URI, request: Any? = null): T =
postForObject(url, request, T::class.java) postForObject(url, request, T::class.java) as T
/** /**
* Extension for [RestOperations.postForEntity] providing a `postForEntity<Foo>(...)` * Extension for [RestOperations.postForEntity] providing a `postForEntity<Foo>(...)`
@ -201,7 +201,7 @@ inline fun <reified T: Any> RestOperations.postForObject(url: URI, request: Any?
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: Any? = null, inline fun <reified T> RestOperations.postForEntity(url: String, request: Any? = null,
vararg uriVariables: Any): ResponseEntity<T> = vararg uriVariables: Any): ResponseEntity<T> =
postForEntity(url, request, T::class.java, *uriVariables) postForEntity(url, request, T::class.java, *uriVariables)
@ -216,7 +216,7 @@ inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: A
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: Any? = null, inline fun <reified T> RestOperations.postForEntity(url: String, request: Any? = null,
uriVariables: Map<String, *>): ResponseEntity<T> = uriVariables: Map<String, *>): ResponseEntity<T> =
postForEntity(url, request, T::class.java, uriVariables) postForEntity(url, request, T::class.java, uriVariables)
@ -231,7 +231,7 @@ inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: A
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.postForEntity(url: URI, request: Any? = null): ResponseEntity<T> = inline fun <reified T> RestOperations.postForEntity(url: URI, request: Any? = null): ResponseEntity<T> =
postForEntity(url, request, T::class.java) postForEntity(url, request, T::class.java)
/** /**
@ -244,7 +244,7 @@ inline fun <reified T: Any> RestOperations.postForEntity(url: URI, request: Any?
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.exchange(url: String, method: HttpMethod, inline fun <reified T> RestOperations.exchange(url: String, method: HttpMethod,
requestEntity: HttpEntity<*>? = null, vararg uriVariables: Any): ResponseEntity<T> = requestEntity: HttpEntity<*>? = null, vararg uriVariables: Any): ResponseEntity<T> =
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, *uriVariables) exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, *uriVariables)
@ -258,7 +258,7 @@ inline fun <reified T: Any> RestOperations.exchange(url: String, method: HttpMet
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.exchange(url: String, method: HttpMethod, inline fun <reified T> RestOperations.exchange(url: String, method: HttpMethod,
requestEntity: HttpEntity<*>? = null, uriVariables: Map<String, *>): ResponseEntity<T> = requestEntity: HttpEntity<*>? = null, uriVariables: Map<String, *>): ResponseEntity<T> =
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, uriVariables) exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, uriVariables)
@ -272,7 +272,7 @@ inline fun <reified T: Any> RestOperations.exchange(url: String, method: HttpMet
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.exchange(url: URI, method: HttpMethod, inline fun <reified T> RestOperations.exchange(url: URI, method: HttpMethod,
requestEntity: HttpEntity<*>? = null): ResponseEntity<T> = requestEntity: HttpEntity<*>? = null): ResponseEntity<T> =
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}) exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {})
@ -286,5 +286,5 @@ inline fun <reified T: Any> RestOperations.exchange(url: URI, method: HttpMethod
* @since 5.0 * @since 5.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> RestOperations.exchange(requestEntity: RequestEntity<*>): ResponseEntity<T> = inline fun <reified T> RestOperations.exchange(requestEntity: RequestEntity<*>): ResponseEntity<T> =
exchange(requestEntity, object : ParameterizedTypeReference<T>() {}) exchange(requestEntity, object : ParameterizedTypeReference<T>() {})

View File

@ -16,14 +16,14 @@
package org.springframework.web.client package org.springframework.web.client
import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify import io.mockk.verify
import org.junit.Assert import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Test import org.junit.Test
import org.springframework.core.ParameterizedTypeReference import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.HttpEntity import org.springframework.http.*
import org.springframework.http.HttpMethod
import org.springframework.http.RequestEntity
import org.springframework.util.ReflectionUtils import org.springframework.util.ReflectionUtils
import java.net.URI import java.net.URI
import kotlin.reflect.full.createType import kotlin.reflect.full.createType
@ -36,14 +36,19 @@ import kotlin.reflect.jvm.kotlinFunction
*/ */
class RestOperationsExtensionsTests { class RestOperationsExtensionsTests {
val template = mockk<RestOperations>(relaxed = true) val template = mockk<RestOperations>()
val foo = mockk<Foo>()
val entity = mockk<ResponseEntity<Foo>>()
@Test @Test
fun `getForObject with reified type parameters, String and varargs`() { fun `getForObject with reified type parameters, String and varargs`() {
val url = "https://spring.io" val url = "https://spring.io"
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.getForObject<Foo>(url, var1, var2) every { template.getForObject(url, Foo::class.java, var1, var2) } returns foo
assertEquals(foo, template.getForObject<Foo>(url, var1, var2))
verify { template.getForObject(url, Foo::class.java, var1, var2) } verify { template.getForObject(url, Foo::class.java, var1, var2) }
} }
@ -51,21 +56,24 @@ class RestOperationsExtensionsTests {
fun `getForObject with reified type parameters, String and Map`() { fun `getForObject with reified type parameters, String and Map`() {
val url = "https://spring.io" val url = "https://spring.io"
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.getForObject<Foo>(url, vars) every { template.getForObject(url, Foo::class.java, vars) } returns foo
assertEquals(foo, template.getForObject<Foo>(url, vars))
verify { template.getForObject(url, Foo::class.java, vars) } verify { template.getForObject(url, Foo::class.java, vars) }
} }
@Test @Test
fun `getForObject with reified type parameters and URI`() { fun `getForObject with reified type parameters and URI`() {
val url = URI("https://spring.io") val url = URI("https://spring.io")
template.getForObject<Foo>(url) every { template.getForObject(url, Foo::class.java) } returns foo
assertEquals(foo, template.getForObject<Foo>(url))
verify { template.getForObject(url, Foo::class.java) } verify { template.getForObject(url, Foo::class.java) }
} }
@Test @Test
fun `getForEntity with reified type parameters, String and URI`() { fun `getForEntity with reified type parameters, String and URI`() {
val url = URI("https://spring.io") val url = URI("https://spring.io")
template.getForEntity<Foo>(url) every { template.getForEntity(url, Foo::class.java) } returns entity
assertEquals(entity, template.getForEntity<Foo>(url))
verify { template.getForEntity(url, Foo::class.java) } verify { template.getForEntity(url, Foo::class.java) }
} }
@ -74,7 +82,8 @@ class RestOperationsExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.getForEntity<Foo>(url, var1, var2) every { template.getForEntity(url, Foo::class.java, var1, var2) } returns entity
assertEquals(entity, template.getForEntity<Foo>(url, var1, var2))
verify { template.getForEntity(url, Foo::class.java, var1, var2) } verify { template.getForEntity(url, Foo::class.java, var1, var2) }
} }
@ -82,7 +91,8 @@ class RestOperationsExtensionsTests {
fun `getForEntity with reified type parameters and Map`() { fun `getForEntity with reified type parameters and Map`() {
val url = "https://spring.io" val url = "https://spring.io"
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.getForEntity<Foo>(url, vars) every { template.getForEntity(url, Foo::class.java, vars) } returns entity
assertEquals(entity, template.getForEntity<Foo>(url, vars))
verify { template.getForEntity(url, Foo::class.java, vars) } verify { template.getForEntity(url, Foo::class.java, vars) }
} }
@ -92,7 +102,8 @@ class RestOperationsExtensionsTests {
val body: Any = "body" val body: Any = "body"
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.patchForObject<Foo>(url, body, var1, var2) every { template.patchForObject(url, body, Foo::class.java, var1, var2) } returns foo
assertEquals(foo, template.patchForObject<Foo>(url, body, var1, var2))
verify { template.patchForObject(url, body, Foo::class.java, var1, var2) } verify { template.patchForObject(url, body, Foo::class.java, var1, var2) }
} }
@ -101,7 +112,8 @@ class RestOperationsExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val body: Any = "body" val body: Any = "body"
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.patchForObject<Foo>(url, body, vars) every { template.patchForObject(url, body, Foo::class.java, vars) } returns foo
assertEquals(foo, template.patchForObject<Foo>(url, body, vars))
verify { template.patchForObject(url, body, Foo::class.java, vars) } verify { template.patchForObject(url, body, Foo::class.java, vars) }
} }
@ -109,14 +121,16 @@ class RestOperationsExtensionsTests {
fun `patchForObject with reified type parameters and String`() { fun `patchForObject with reified type parameters and String`() {
val url = "https://spring.io" val url = "https://spring.io"
val body: Any = "body" val body: Any = "body"
template.patchForObject<Foo>(url, body) every { template.patchForObject(url, body, Foo::class.java) } returns foo
assertEquals(foo, template.patchForObject<Foo>(url, body))
verify { template.patchForObject(url, body, Foo::class.java) } verify { template.patchForObject(url, body, Foo::class.java) }
} }
@Test @Test
fun `patchForObject with reified type parameters`() { fun `patchForObject with reified type parameters`() {
val url = "https://spring.io" val url = "https://spring.io"
template.patchForObject<Foo>(url) every { template.patchForObject(url, null, Foo::class.java) } returns foo
assertEquals(foo, template.patchForObject<Foo>(url))
verify { template.patchForObject(url, null, Foo::class.java) } verify { template.patchForObject(url, null, Foo::class.java) }
} }
@ -126,7 +140,8 @@ class RestOperationsExtensionsTests {
val body: Any = "body" val body: Any = "body"
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.postForObject<Foo>(url, body, var1, var2) every { template.postForObject(url, body, Foo::class.java, var1, var2) } returns foo
assertEquals(foo, template.postForObject<Foo>(url, body, var1, var2))
verify { template.postForObject(url, body, Foo::class.java, var1, var2) } verify { template.postForObject(url, body, Foo::class.java, var1, var2) }
} }
@ -135,7 +150,8 @@ class RestOperationsExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val body: Any = "body" val body: Any = "body"
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.postForObject<Foo>(url, body, vars) every { template.postForObject(url, body, Foo::class.java, vars) } returns foo
assertEquals(foo, template.postForObject<Foo>(url, body, vars))
verify { template.postForObject(url, body, Foo::class.java, vars) } verify { template.postForObject(url, body, Foo::class.java, vars) }
} }
@ -143,14 +159,16 @@ class RestOperationsExtensionsTests {
fun `postForObject with reified type parameters and String`() { fun `postForObject with reified type parameters and String`() {
val url = "https://spring.io" val url = "https://spring.io"
val body: Any = "body" val body: Any = "body"
template.postForObject<Foo>(url, body) every { template.postForObject(url, body, Foo::class.java) } returns foo
assertEquals(foo, template.postForObject<Foo>(url, body))
verify { template.postForObject(url, body, Foo::class.java) } verify { template.postForObject(url, body, Foo::class.java) }
} }
@Test @Test
fun `postForObject with reified type parameters`() { fun `postForObject with reified type parameters`() {
val url = "https://spring.io" val url = "https://spring.io"
template.postForObject<Foo>(url) every { template.postForObject(url, null, Foo::class.java) } returns foo
assertEquals(foo, template.postForObject<Foo>(url))
verify { template.postForObject(url, null, Foo::class.java) } verify { template.postForObject(url, null, Foo::class.java) }
} }
@ -160,7 +178,8 @@ class RestOperationsExtensionsTests {
val body: Any = "body" val body: Any = "body"
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.postForEntity<Foo>(url, body, var1, var2) every { template.postForEntity(url, body, Foo::class.java, var1, var2) } returns entity
assertEquals(entity, template.postForEntity<Foo>(url, body, var1, var2))
verify { template.postForEntity(url, body, Foo::class.java, var1, var2) } verify { template.postForEntity(url, body, Foo::class.java, var1, var2) }
} }
@ -169,7 +188,8 @@ class RestOperationsExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val body: Any = "body" val body: Any = "body"
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.postForEntity<Foo>(url, body, vars) every { template.postForEntity(url, body, Foo::class.java, vars) } returns entity
assertEquals(entity, template.postForEntity<Foo>(url, body, vars))
verify { template.postForEntity(url, body, Foo::class.java, vars) } verify { template.postForEntity(url, body, Foo::class.java, vars) }
} }
@ -177,14 +197,16 @@ class RestOperationsExtensionsTests {
fun `postForEntity with reified type parameters and String`() { fun `postForEntity with reified type parameters and String`() {
val url = "https://spring.io" val url = "https://spring.io"
val body: Any = "body" val body: Any = "body"
template.postForEntity<Foo>(url, body) every { template.postForEntity(url, body, Foo::class.java) } returns entity
assertEquals(entity, template.postForEntity<Foo>(url, body))
verify { template.postForEntity(url, body, Foo::class.java) } verify { template.postForEntity(url, body, Foo::class.java) }
} }
@Test @Test
fun `postForEntity with reified type parameters`() { fun `postForEntity with reified type parameters`() {
val url = "https://spring.io" val url = "https://spring.io"
template.postForEntity<Foo>(url) every { template.postForEntity(url, null, Foo::class.java) } returns entity
assertEquals(entity, template.postForEntity<Foo>(url))
verify { template.postForEntity(url, null, Foo::class.java) } verify { template.postForEntity(url, null, Foo::class.java) }
} }
@ -192,12 +214,13 @@ class RestOperationsExtensionsTests {
fun `exchange with reified type parameters, String, HttpMethod, HttpEntity and varargs`() { fun `exchange with reified type parameters, String, HttpMethod, HttpEntity and varargs`() {
val url = "https://spring.io" val url = "https://spring.io"
val method = HttpMethod.GET val method = HttpMethod.GET
val entity = mockk<HttpEntity<Foo>>()
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.exchange<List<Foo>>(url, method, entity, var1, var2) val entityList = mockk<ResponseEntity<List<Foo>>>()
verify { template.exchange(url, method, entity, val responseType = object : ParameterizedTypeReference<List<Foo>>() {}
object : ParameterizedTypeReference<List<Foo>>() {}, var1, var2) } every { template.exchange(url, method, entity, responseType, var1, var2) } returns entityList
assertEquals(entityList, template.exchange<List<Foo>>(url, method, entity, var1, var2))
verify { template.exchange(url, method, entity, responseType, var1, var2) }
} }
@Test @Test
@ -206,9 +229,11 @@ class RestOperationsExtensionsTests {
val method = HttpMethod.GET val method = HttpMethod.GET
val entity = mockk<HttpEntity<Foo>>() val entity = mockk<HttpEntity<Foo>>()
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.exchange<List<Foo>>(url, method, entity, vars) val entityList = mockk<ResponseEntity<List<Foo>>>()
verify { template.exchange(url, method, entity, val responseType = object : ParameterizedTypeReference<List<Foo>>() {}
object : ParameterizedTypeReference<List<Foo>>() {}, vars) } every { template.exchange(url, method, entity, responseType, vars) } returns entityList
assertEquals(entityList, template.exchange<List<Foo>>(url, method, entity, vars))
verify { template.exchange(url, method, entity, responseType, vars) }
} }
@Test @Test
@ -216,26 +241,32 @@ class RestOperationsExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val method = HttpMethod.GET val method = HttpMethod.GET
val entity = mockk<HttpEntity<Foo>>() val entity = mockk<HttpEntity<Foo>>()
template.exchange<List<Foo>>(url, method, entity) val entityList = mockk<ResponseEntity<List<Foo>>>()
verify { template.exchange(url, method, entity, val responseType = object : ParameterizedTypeReference<List<Foo>>() {}
object : ParameterizedTypeReference<List<Foo>>() {}) } every { template.exchange(url, method, entity, responseType) } returns entityList
assertEquals(entityList, template.exchange<List<Foo>>(url, method, entity))
verify { template.exchange(url, method, entity, responseType) }
} }
@Test @Test
fun `exchange with reified type parameters, String and HttpMethod`() { fun `exchange with reified type parameters, String and HttpMethod`() {
val url = "https://spring.io" val url = "https://spring.io"
val method = HttpMethod.GET val method = HttpMethod.GET
template.exchange<List<Foo>>(url, method) val entityList = mockk<ResponseEntity<List<Foo>>>()
verify { template.exchange(url, method, null, val responseType = object : ParameterizedTypeReference<List<Foo>>() {}
object : ParameterizedTypeReference<List<Foo>>() {}) } every { template.exchange(url, method, null, responseType) } returns entityList
assertEquals(entityList, template.exchange<List<Foo>>(url, method))
verify { template.exchange(url, method, null, responseType) }
} }
@Test @Test
fun `exchange with reified type parameters, String and HttpEntity`() { fun `exchange with reified type parameters, String and HttpEntity`() {
val entity = mockk<RequestEntity<Foo>>() val entity = mockk<RequestEntity<Foo>>()
template.exchange<List<Foo>>(entity) val entityList = mockk<ResponseEntity<List<Foo>>>()
verify { template.exchange(entity, val responseType = object : ParameterizedTypeReference<List<Foo>>() {}
object : ParameterizedTypeReference<List<Foo>>() {}) } every { template.exchange(entity, responseType) } returns entityList
assertEquals(entityList, template.exchange<List<Foo>>(entity))
verify { template.exchange(entity, responseType) }
} }
@Test @Test
@ -247,7 +278,8 @@ class RestOperationsExtensionsTests {
val parameters = mutableListOf<Class<*>>(RestOperations::class.java).apply { addAll(method.parameterTypes.filter { it != kClass.java }) } val parameters = mutableListOf<Class<*>>(RestOperations::class.java).apply { addAll(method.parameterTypes.filter { it != kClass.java }) }
val f = extensions.getDeclaredMethod(method.name, *parameters.toTypedArray()).kotlinFunction!! val f = extensions.getDeclaredMethod(method.name, *parameters.toTypedArray()).kotlinFunction!!
Assert.assertEquals(1, f.typeParameters.size) Assert.assertEquals(1, f.typeParameters.size)
Assert.assertEquals(listOf(Any::class.createType()), f.typeParameters[0].upperBounds) System.out.println(method.name + f.typeParameters)
Assert.assertEquals("Failed: " + method.name, listOf(Any::class.createType(nullable = true)), f.typeParameters[0].upperBounds)
} }
} }
} }