Skip to content

Commit cf87441

Browse files
committed
Remove unnecessary method.isAccessible() invocation
Closes gh-32548
1 parent dc982d0 commit cf87441

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

spring-core/src/main/java/org/springframework/core/CoroutinesUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static Publisher<?> invokeSuspendingFunction(
115115
Assert.isTrue(KotlinDetector.isSuspendingFunction(method), "Method must be a suspending function");
116116
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
117117
Assert.notNull(function, () -> "Failed to get Kotlin function for method: " + method);
118-
if (method.isAccessible() && !KCallablesJvm.isAccessible(function)) {
118+
if (!KCallablesJvm.isAccessible(function)) {
119119
KCallablesJvm.setAccessible(function, true);
120120
}
121121
Mono<Object> mono = MonoKt.mono(context, (scope, continuation) -> {

spring-core/src/test/kotlin/org/springframework/core/CoroutinesUtilsTests.kt

+16
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ class CoroutinesUtilsTests {
9393
}
9494
}
9595

96+
@Test
97+
fun invokePrivateSuspendingFunction() {
98+
val method = CoroutinesUtilsTests::class.java.getDeclaredMethod("privateSuspendingFunction", String::class.java, Continuation::class.java)
99+
val publisher = CoroutinesUtils.invokeSuspendingFunction(method, this, "foo")
100+
Assertions.assertThat(publisher).isInstanceOf(Mono::class.java)
101+
StepVerifier.create(publisher)
102+
.expectNext("foo")
103+
.expectComplete()
104+
.verify()
105+
}
106+
96107
@Test
97108
fun invokeNonSuspendingFunction() {
98109
val method = CoroutinesUtilsTests::class.java.getDeclaredMethod("nonSuspendingFunction", String::class.java)
@@ -252,6 +263,11 @@ class CoroutinesUtilsTests {
252263
return value
253264
}
254265

266+
private suspend fun privateSuspendingFunction(value: String): String {
267+
delay(1)
268+
return value
269+
}
270+
255271
suspend fun suspendingFunctionWithNullable(value: String?): String? {
256272
delay(1)
257273
return value

spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public static Object invokeFunction(Method method, Object target, Object[] args)
305305
if (function == null) {
306306
return method.invoke(target, args);
307307
}
308-
if (method.isAccessible() && !KCallablesJvm.isAccessible(function)) {
308+
if (!KCallablesJvm.isAccessible(function)) {
309309
KCallablesJvm.setAccessible(function, true);
310310
}
311311
Map<KParameter, Object> argMap = CollectionUtils.newHashMap(args.length + 1);

spring-web/src/test/kotlin/org/springframework/web/method/support/InvocableHandlerMethodKotlinTests.kt

+11
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ class InvocableHandlerMethodKotlinTests {
8484
Assertions.assertThat(value).isNull()
8585
}
8686

87+
@Test
88+
fun private() {
89+
composite.addResolver(StubArgumentResolver(Float::class.java, 1.2f))
90+
val value = getInvocable(Handler::class.java, Float::class.java).invokeForRequest(request, null)
91+
92+
Assertions.assertThat(getStubResolver(0).resolvedParameters).hasSize(1)
93+
Assertions.assertThat(value).isEqualTo("1.2")
94+
}
95+
8796
@Test
8897
fun valueClass() {
8998
composite.addResolver(StubArgumentResolver(Long::class.java, 1L))
@@ -182,6 +191,8 @@ class InvocableHandlerMethodKotlinTests {
182191
return null
183192
}
184193

194+
private fun private(value: Float) = value.toString()
195+
185196
}
186197

187198
private class ValueClassHandler {

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public static Object invokeFunction(Method method, Object target, Object[] args,
318318
if (function == null) {
319319
return method.invoke(target, args);
320320
}
321-
if (method.isAccessible() && !KCallablesJvm.isAccessible(function)) {
321+
if (!KCallablesJvm.isAccessible(function)) {
322322
KCallablesJvm.setAccessible(function, true);
323323
}
324324
Map<KParameter, Object> argMap = CollectionUtils.newHashMap(args.length + 1);

spring-webflux/src/test/kotlin/org/springframework/web/reactive/result/InvocableHandlerMethodKotlinTests.kt

+12-3
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,18 @@ class InvocableHandlerMethodKotlinTests {
112112
@Test
113113
fun privateController() {
114114
this.resolvers.add(stubResolver("foo"))
115-
val method = PrivateCoroutinesController::singleArg.javaMethod!!
116-
val result = invoke(PrivateCoroutinesController(), method,"foo")
115+
val method = PrivateController::singleArg.javaMethod!!
116+
val result = invoke(PrivateController(), method,"foo")
117117
assertHandlerResultValue(result, "success:foo")
118118
}
119119

120+
@Test
121+
fun privateFunction() {
122+
val method = PrivateController::class.java.getDeclaredMethod("private")
123+
val result = invoke(PrivateController(), method)
124+
assertHandlerResultValue(result, "private")
125+
}
126+
120127
@Test
121128
fun defaultValue() {
122129
this.resolvers.add(stubResolver(null, String::class.java))
@@ -330,12 +337,14 @@ class InvocableHandlerMethodKotlinTests {
330337
}
331338
}
332339

333-
private class PrivateCoroutinesController {
340+
private class PrivateController {
334341

335342
suspend fun singleArg(q: String?): String {
336343
delay(1)
337344
return "success:$q"
338345
}
346+
347+
private fun private() = "private"
339348
}
340349

341350
class DefaultValueController {

0 commit comments

Comments
 (0)