From 95f181352f674422255ab5b32b878fd81760aa4b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 30 Sep 2024 11:13:26 +0200 Subject: [PATCH 1/2] Defensively check MethodParameter.getMethod() in KotlinDelegate Closes gh-33609 --- .../AbstractNamedValueMethodArgumentResolver.java | 14 +++++++++----- .../AbstractNamedValueArgumentResolver.java | 13 ++++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java index b3d2ce7cd1..131446e8bf 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java @@ -19,7 +19,6 @@ package org.springframework.web.method.annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.Map; -import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import jakarta.servlet.ServletException; @@ -107,9 +106,9 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle NamedValueInfo namedValueInfo = getNamedValueInfo(parameter); MethodParameter nestedParameter = parameter.nestedIfOptional(); - boolean hasDefaultValue = KotlinDetector.isKotlinReflectPresent() - && KotlinDetector.isKotlinType(parameter.getDeclaringClass()) - && KotlinDelegate.hasDefaultValue(nestedParameter); + boolean hasDefaultValue = KotlinDetector.isKotlinReflectPresent() && + KotlinDetector.isKotlinType(parameter.getDeclaringClass()) && + KotlinDelegate.hasDefaultValue(nestedParameter); Object resolvedName = resolveEmbeddedValuesAndExpressions(namedValueInfo.name); if (resolvedName == null) { @@ -336,6 +335,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle } } + /** * Inner class to avoid a hard dependency on Kotlin at runtime. */ @@ -346,7 +346,10 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle * or an optional parameter (with a default value in the Kotlin declaration). */ public static boolean hasDefaultValue(MethodParameter parameter) { - Method method = Objects.requireNonNull(parameter.getMethod()); + Method method = parameter.getMethod(); + if (method == null) { + return false; + } KFunction function = ReflectJvmMapping.getKotlinFunction(method); if (function != null) { int index = 0; @@ -359,4 +362,5 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle return false; } } + } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueArgumentResolver.java index fb98c07905..48650437d1 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueArgumentResolver.java @@ -19,7 +19,6 @@ package org.springframework.web.reactive.result.method.annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.Map; -import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import kotlin.reflect.KFunction; @@ -227,9 +226,9 @@ public abstract class AbstractNamedValueArgumentResolver extends HandlerMethodAr return Mono.fromSupplier(() -> { Object value = null; - boolean hasDefaultValue = KotlinDetector.isKotlinReflectPresent() - && KotlinDetector.isKotlinType(parameter.getDeclaringClass()) - && KotlinDelegate.hasDefaultValue(parameter); + boolean hasDefaultValue = KotlinDetector.isKotlinReflectPresent() && + KotlinDetector.isKotlinType(parameter.getDeclaringClass()) && + KotlinDelegate.hasDefaultValue(parameter); if (namedValueInfo.defaultValue != null) { value = resolveEmbeddedValuesAndExpressions(namedValueInfo.defaultValue); } @@ -328,6 +327,7 @@ public abstract class AbstractNamedValueArgumentResolver extends HandlerMethodAr } } + /** * Inner class to avoid a hard dependency on Kotlin at runtime. */ @@ -338,7 +338,10 @@ public abstract class AbstractNamedValueArgumentResolver extends HandlerMethodAr * or an optional parameter (with a default value in the Kotlin declaration). */ public static boolean hasDefaultValue(MethodParameter parameter) { - Method method = Objects.requireNonNull(parameter.getMethod()); + Method method = parameter.getMethod(); + if (method == null) { + return false; + } KFunction function = ReflectJvmMapping.getKotlinFunction(method); if (function != null) { int index = 0; From 87157d3200cdbd4b5a89be57f241b5ca3df79ea4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 30 Sep 2024 11:13:50 +0200 Subject: [PATCH 2/2] Polishing --- .../springframework/http/codec/ServerSentEvent.java | 10 +++++----- .../invoker/AbstractNamedValueArgumentResolver.java | 4 +--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/ServerSentEvent.java b/spring-web/src/main/java/org/springframework/http/codec/ServerSentEvent.java index b2bf41162d..af85c2c6a7 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/ServerSentEvent.java +++ b/spring-web/src/main/java/org/springframework/http/codec/ServerSentEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2024 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. @@ -104,13 +104,13 @@ public final class ServerSentEvent { @Override public String toString() { - return ("ServerSentEvent [id = '" + this.id + "\', event='" + this.event + "\', retry=" + + return ("ServerSentEvent [id = '" + this.id + "', event='" + this.event + "', retry=" + this.retry + ", comment='" + this.comment + "', data=" + this.data + ']'); } /** - * Return a builder for a {@code SseEvent}. + * Return a builder for a {@code ServerSentEvent}. * @param the type of data that this event contains * @return the builder */ @@ -119,7 +119,7 @@ public final class ServerSentEvent { } /** - * Return a builder for a {@code SseEvent}, populated with the given {@linkplain #data() data}. + * Return a builder for a {@code ServerSentEvent}, populated with the given {@linkplain #data() data}. * @param the type of data that this event contains * @return the builder */ @@ -129,7 +129,7 @@ public final class ServerSentEvent { /** - * A mutable builder for a {@code SseEvent}. + * A mutable builder for a {@code ServerSentEvent}. * * @param the type of data that this event contains */ diff --git a/spring-web/src/main/java/org/springframework/web/service/invoker/AbstractNamedValueArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/service/invoker/AbstractNamedValueArgumentResolver.java index 0aaa91ac19..62b25d70a7 100644 --- a/spring-web/src/main/java/org/springframework/web/service/invoker/AbstractNamedValueArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/service/invoker/AbstractNamedValueArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -47,7 +47,6 @@ public abstract class AbstractNamedValueArgumentResolver implements HttpServiceA protected final Log logger = LogFactory.getLog(getClass()); - @Nullable private final ConversionService conversionService; @@ -248,7 +247,6 @@ public abstract class AbstractNamedValueArgumentResolver implements HttpServiceA public NamedValueInfo update(String name, boolean required, @Nullable String defaultValue) { return new NamedValueInfo(name, required, defaultValue, this.label, this.multiValued); } - } }