Skip to content

Commit 1d1851f

Browse files
committed
Refine RestOperations Kotlin extensions nullability
This commit aligns RestOperationsExtensions.kt nullability with the Java APIs one, like what has been done in gh-35846 for JdbcOperations. Closes gh-35852
1 parent 23f0cfb commit 1d1851f

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

spring-web/src/main/kotlin/org/springframework/web/client/RestOperationsExtensions.kt

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import org.springframework.http.HttpEntity
2121
import org.springframework.http.HttpMethod
2222
import org.springframework.http.RequestEntity
2323
import org.springframework.http.ResponseEntity
24-
import java.lang.Class
2524
import java.net.URI
26-
import kotlin.reflect.KClass
2725

2826
/**
2927
* Extension for [RestOperations.getForObject] providing a `getForObject<Foo>(...)`
@@ -36,8 +34,8 @@ import kotlin.reflect.KClass
3634
* @since 5.0
3735
*/
3836
@Throws(RestClientException::class)
39-
inline fun <reified T> RestOperations.getForObject(url: String, vararg uriVariables: Any?): T =
40-
getForObject(url, T::class.java as Class<*>, *uriVariables) as T
37+
inline fun <reified T : Any> RestOperations.getForObject(url: String, vararg uriVariables: Any?): T? =
38+
getForObject(url, T::class.java, *uriVariables)
4139

4240
/**
4341
* Extension for [RestOperations.getForObject] providing a `getForObject<Foo>(...)`
@@ -50,8 +48,8 @@ inline fun <reified T> RestOperations.getForObject(url: String, vararg uriVariab
5048
* @since 5.0
5149
*/
5250
@Throws(RestClientException::class)
53-
inline fun <reified T> RestOperations.getForObject(url: String, uriVariables: Map<String, Any?>): T =
54-
getForObject(url, T::class.java as Class<*>, uriVariables) as T
51+
inline fun <reified T : Any> RestOperations.getForObject(url: String, uriVariables: Map<String, Any?>): T? =
52+
getForObject(url, T::class.java, uriVariables)
5553

5654
/**
5755
* Extension for [RestOperations.getForObject] providing a `getForObject<Foo>(...)`
@@ -64,8 +62,8 @@ inline fun <reified T> RestOperations.getForObject(url: String, uriVariables: Ma
6462
* @since 5.0
6563
*/
6664
@Throws(RestClientException::class)
67-
inline fun <reified T> RestOperations.getForObject(url: URI): T =
68-
getForObject(url, T::class.java as Class<*>) as T
65+
inline fun <reified T : Any> RestOperations.getForObject(url: URI): T? =
66+
getForObject(url, T::class.java)
6967

7068
/**
7169
* Extension for [RestOperations.getForEntity] providing a `getForEntity<Foo>(...)`
@@ -118,9 +116,9 @@ inline fun <reified T: Any> RestOperations.getForEntity(url: String, uriVariable
118116
* @since 5.0.2
119117
*/
120118
@Throws(RestClientException::class)
121-
inline fun <reified T> RestOperations.patchForObject(url: String, request: Any? = null,
122-
vararg uriVariables: Any?): T =
123-
patchForObject(url, request, T::class.java as Class<*>, *uriVariables) as T
119+
inline fun <reified T : Any> RestOperations.patchForObject(url: String, request: Any? = null,
120+
vararg uriVariables: Any?): T? =
121+
patchForObject(url, request, T::class.java, *uriVariables)
124122

125123
/**
126124
* Extension for [RestOperations.patchForObject] providing a `patchForObject<Foo>(...)`
@@ -132,9 +130,9 @@ inline fun <reified T> RestOperations.patchForObject(url: String, request: Any?
132130
* @since 5.0.2
133131
*/
134132
@Throws(RestClientException::class)
135-
inline fun <reified T> RestOperations.patchForObject(url: String, request: Any? = null,
136-
uriVariables: Map<String, *>): T =
137-
patchForObject(url, request, T::class.java as Class<*>, uriVariables) as T
133+
inline fun <reified T : Any> RestOperations.patchForObject(url: String, request: Any? = null,
134+
uriVariables: Map<String, *>): T? =
135+
patchForObject(url, request, T::class.java, uriVariables)
138136

139137
/**
140138
* Extension for [RestOperations.patchForObject] providing a `patchForObject<Foo>(...)`
@@ -146,8 +144,8 @@ inline fun <reified T> RestOperations.patchForObject(url: String, request: Any?
146144
* @since 5.0.2
147145
*/
148146
@Throws(RestClientException::class)
149-
inline fun <reified T> RestOperations.patchForObject(url: URI, request: Any? = null): T =
150-
patchForObject(url, request, T::class.java as Class<*>) as T
147+
inline fun <reified T : Any> RestOperations.patchForObject(url: URI, request: Any? = null): T? =
148+
patchForObject(url, request, T::class.java)
151149

152150
/**
153151
* Extension for [RestOperations.postForObject] providing a `postForObject<Foo>(...)`
@@ -160,9 +158,9 @@ inline fun <reified T> RestOperations.patchForObject(url: URI, request: Any? = n
160158
* @since 5.0
161159
*/
162160
@Throws(RestClientException::class)
163-
inline fun <reified T> RestOperations.postForObject(url: String, request: Any? = null,
164-
vararg uriVariables: Any?): T =
165-
postForObject(url, request, T::class.java as Class<*>, *uriVariables) as T
161+
inline fun <reified T : Any> RestOperations.postForObject(url: String, request: Any? = null,
162+
vararg uriVariables: Any?): T? =
163+
postForObject(url, request, T::class.java, *uriVariables)
166164

167165
/**
168166
* Extension for [RestOperations.postForObject] providing a `postForObject<Foo>(...)`
@@ -175,9 +173,9 @@ inline fun <reified T> RestOperations.postForObject(url: String, request: Any? =
175173
* @since 5.0
176174
*/
177175
@Throws(RestClientException::class)
178-
inline fun <reified T> RestOperations.postForObject(url: String, request: Any? = null,
179-
uriVariables: Map<String, *>): T =
180-
postForObject(url, request, T::class.java as Class<*>, uriVariables) as T
176+
inline fun <reified T : Any> RestOperations.postForObject(url: String, request: Any? = null,
177+
uriVariables: Map<String, *>): T? =
178+
postForObject(url, request, T::class.java, uriVariables)
181179

182180
/**
183181
* Extension for [RestOperations.postForObject] providing a `postForObject<Foo>(...)`
@@ -190,8 +188,8 @@ inline fun <reified T> RestOperations.postForObject(url: String, request: Any? =
190188
* @since 5.0
191189
*/
192190
@Throws(RestClientException::class)
193-
inline fun <reified T> RestOperations.postForObject(url: URI, request: Any? = null): T =
194-
postForObject(url, request, T::class.java as Class<*>) as T
191+
inline fun <reified T : Any> RestOperations.postForObject(url: URI, request: Any? = null): T? =
192+
postForObject(url, request, T::class.java)
195193

196194
/**
197195
* Extension for [RestOperations.postForEntity] providing a `postForEntity<Foo>(...)`

spring-web/src/test/kotlin/org/springframework/web/client/RestOperationsExtensionsTests.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ class RestOperationsExtensionsTests {
270270
}
271271

272272
@Test
273-
@Disabled("May require Kotlin 2") // TODO Enable after Kotlin 2 upgrade
274273
fun `RestOperations are available`() {
275274
val extensions = Class.forName("org.springframework.web.client.RestOperationsExtensionsKt")
276275
ReflectionUtils.doWithMethods(RestOperations::class.java) { method ->
@@ -281,7 +280,7 @@ class RestOperationsExtensionsTests {
281280
assertThat(f.typeParameters.size).isEqualTo(1)
282281
val type = f.typeParameters[0].upperBounds.first()
283282
assertThat(type.classifier).isEqualTo(Any::class)
284-
assertThat(type.isMarkedNullable).isTrue()
283+
assertThat(type.isMarkedNullable).isFalse()
285284
}
286285
}
287286
}

0 commit comments

Comments
 (0)