Skip to content

Commit 95f1813

Browse files
committed
Defensively check MethodParameter.getMethod() in KotlinDelegate
Closes gh-33609
1 parent e32a2f3 commit 95f1813

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.lang.reflect.Constructor;
2020
import java.lang.reflect.Method;
2121
import java.util.Map;
22-
import java.util.Objects;
2322
import java.util.concurrent.ConcurrentHashMap;
2423

2524
import jakarta.servlet.ServletException;
@@ -107,9 +106,9 @@ public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAn
107106

108107
NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);
109108
MethodParameter nestedParameter = parameter.nestedIfOptional();
110-
boolean hasDefaultValue = KotlinDetector.isKotlinReflectPresent()
111-
&& KotlinDetector.isKotlinType(parameter.getDeclaringClass())
112-
&& KotlinDelegate.hasDefaultValue(nestedParameter);
109+
boolean hasDefaultValue = KotlinDetector.isKotlinReflectPresent() &&
110+
KotlinDetector.isKotlinType(parameter.getDeclaringClass()) &&
111+
KotlinDelegate.hasDefaultValue(nestedParameter);
113112

114113
Object resolvedName = resolveEmbeddedValuesAndExpressions(namedValueInfo.name);
115114
if (resolvedName == null) {
@@ -336,6 +335,7 @@ public NamedValueInfo(String name, boolean required, @Nullable String defaultVal
336335
}
337336
}
338337

338+
339339
/**
340340
* Inner class to avoid a hard dependency on Kotlin at runtime.
341341
*/
@@ -346,7 +346,10 @@ private static class KotlinDelegate {
346346
* or an optional parameter (with a default value in the Kotlin declaration).
347347
*/
348348
public static boolean hasDefaultValue(MethodParameter parameter) {
349-
Method method = Objects.requireNonNull(parameter.getMethod());
349+
Method method = parameter.getMethod();
350+
if (method == null) {
351+
return false;
352+
}
350353
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
351354
if (function != null) {
352355
int index = 0;
@@ -359,4 +362,5 @@ public static boolean hasDefaultValue(MethodParameter parameter) {
359362
return false;
360363
}
361364
}
365+
362366
}

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.lang.reflect.Constructor;
2020
import java.lang.reflect.Method;
2121
import java.util.Map;
22-
import java.util.Objects;
2322
import java.util.concurrent.ConcurrentHashMap;
2423

2524
import kotlin.reflect.KFunction;
@@ -227,9 +226,9 @@ private Mono<Object> getDefaultValue(NamedValueInfo namedValueInfo, MethodParame
227226

228227
return Mono.fromSupplier(() -> {
229228
Object value = null;
230-
boolean hasDefaultValue = KotlinDetector.isKotlinReflectPresent()
231-
&& KotlinDetector.isKotlinType(parameter.getDeclaringClass())
232-
&& KotlinDelegate.hasDefaultValue(parameter);
229+
boolean hasDefaultValue = KotlinDetector.isKotlinReflectPresent() &&
230+
KotlinDetector.isKotlinType(parameter.getDeclaringClass()) &&
231+
KotlinDelegate.hasDefaultValue(parameter);
233232
if (namedValueInfo.defaultValue != null) {
234233
value = resolveEmbeddedValuesAndExpressions(namedValueInfo.defaultValue);
235234
}
@@ -328,6 +327,7 @@ public NamedValueInfo(String name, boolean required, @Nullable String defaultVal
328327
}
329328
}
330329

330+
331331
/**
332332
* Inner class to avoid a hard dependency on Kotlin at runtime.
333333
*/
@@ -338,7 +338,10 @@ private static class KotlinDelegate {
338338
* or an optional parameter (with a default value in the Kotlin declaration).
339339
*/
340340
public static boolean hasDefaultValue(MethodParameter parameter) {
341-
Method method = Objects.requireNonNull(parameter.getMethod());
341+
Method method = parameter.getMethod();
342+
if (method == null) {
343+
return false;
344+
}
342345
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
343346
if (function != null) {
344347
int index = 0;

0 commit comments

Comments
 (0)