This commit is contained in:
Phillip Webb
2022-07-29 12:07:36 +01:00
parent 41e8697445
commit e08c16dfd6
17 changed files with 134 additions and 111 deletions

View File

@@ -49,17 +49,16 @@ class OperationReflectiveProcessor extends SimpleReflectiveProcessor {
private Type extractReturnType(Method method) {
ResolvableType returnType = ResolvableType.forMethodReturnType(method);
if (WebEndpointResponse.class.isAssignableFrom(method.getReturnType())) {
return returnType.as(WebEndpointResponse.class).getGeneric(0).getType();
if (!WebEndpointResponse.class.isAssignableFrom(method.getReturnType())) {
return returnType.getType();
}
return returnType.getType();
return returnType.as(WebEndpointResponse.class).getGeneric(0).getType();
}
private void registerReflectionHints(ReflectionHints hints, Type type) {
if (type.equals(Resource.class)) {
return;
if (!type.equals(Resource.class)) {
this.bindingRegistrar.registerReflectionHints(hints, type);
}
this.bindingRegistrar.registerReflectionHints(hints, type);
}
}

View File

@@ -23,7 +23,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
@@ -60,6 +59,7 @@ import org.springframework.security.access.vote.RoleVoter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
@@ -494,11 +494,13 @@ public abstract class AbstractWebFluxEndpointHandlerMapping extends RequestMappi
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection()
.registerMethod(Objects.requireNonNull(ReflectionUtils.findMethod(WriteOperationHandler.class,
"handle", ServerWebExchange.class, Map.class)))
.registerMethod(Objects.requireNonNull(
ReflectionUtils.findMethod(ReadOperationHandler.class, "handle", ServerWebExchange.class)));
Method writeOperationHandleMethod = ReflectionUtils.findMethod(WriteOperationHandler.class, "handle",
ServerWebExchange.class, Map.class);
Assert.state(writeOperationHandleMethod != null, () -> "Unable to find write operation 'handle' method");
Method readOperationHandleMethod = ReflectionUtils.findMethod(ReadOperationHandler.class, "handle",
ServerWebExchange.class);
Assert.state(readOperationHandleMethod != null, () -> "Unable to find read operation 'handle' method");
hints.reflection().registerMethod(writeOperationHandleMethod).registerMethod(readOperationHandleMethod);
}
}

View File

@@ -16,10 +16,10 @@
package org.springframework.boot.actuate.endpoint.web.reactive;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
@@ -32,6 +32,7 @@ import org.springframework.boot.actuate.endpoint.web.Link;
import org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping.WebFluxEndpointHandlerMappingRuntimeHints;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.context.aot.BindingReflectionHintsRegistrar;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.cors.CorsConfiguration;
@@ -103,8 +104,10 @@ public class WebFluxEndpointHandlerMapping extends AbstractWebFluxEndpointHandle
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerMethod(Objects.requireNonNull(
ReflectionUtils.findMethod(WebFluxLinksHandler.class, "links", ServerWebExchange.class)));
Method linksMethod = ReflectionUtils.findMethod(WebFluxLinksHandler.class, "links",
ServerWebExchange.class);
Assert.state(linksMethod != null, "Unable to find 'links' method");
hints.reflection().registerMethod(linksMethod);
this.bindingRegistrar.registerReflectionHints(hints.reflection(), Link.class);
}

View File

@@ -26,7 +26,6 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import jakarta.servlet.http.HttpServletRequest;
@@ -486,8 +485,10 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerMethod(Objects.requireNonNull(
ReflectionUtils.findMethod(OperationHandler.class, "handle", HttpServletRequest.class, Map.class)));
Method handlerMethod = ReflectionUtils.findMethod(OperationHandler.class, "handle",
HttpServletRequest.class, Map.class);
Assert.state(handlerMethod != null, "Unable to find 'handler' method");
hints.reflection().registerMethod(handlerMethod);
}
}

View File

@@ -16,10 +16,10 @@
package org.springframework.boot.actuate.endpoint.web.servlet;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@@ -34,6 +34,7 @@ import org.springframework.boot.actuate.endpoint.web.Link;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.WebMvcEndpointHandlerMappingRuntimeHints;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.context.aot.BindingReflectionHintsRegistrar;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.cors.CorsConfiguration;
@@ -100,9 +101,10 @@ public class WebMvcEndpointHandlerMapping extends AbstractWebMvcEndpointHandlerM
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection()
.registerMethod(Objects.requireNonNull(ReflectionUtils.findMethod(WebMvcLinksHandler.class, "links",
HttpServletRequest.class, HttpServletResponse.class)));
Method linksMethod = ReflectionUtils.findMethod(WebMvcLinksHandler.class, "links", HttpServletRequest.class,
HttpServletResponse.class);
Assert.state(linksMethod != null, "Unable to find 'links' method");
hints.reflection().registerMethod(linksMethod);
this.bindingRegistrar.registerReflectionHints(hints.reflection(), Link.class);
}

View File

@@ -16,8 +16,8 @@
package org.springframework.boot.actuate.metrics.cache;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Objects;
import com.hazelcast.spring.cache.HazelcastCache;
import io.micrometer.core.instrument.Tag;
@@ -28,6 +28,7 @@ import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.boot.actuate.metrics.cache.HazelcastCacheMeterBinderProvider.HazelcastCacheMeterBinderProviderRuntimeHints;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
@@ -67,10 +68,10 @@ public class HazelcastCacheMeterBinderProvider implements CacheMeterBinderProvid
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
try {
hints.reflection()
.registerMethod(Objects
.requireNonNull(ReflectionUtils.findMethod(HazelcastCache.class, "getNativeCache")))
.registerConstructor(HazelcastCacheMetrics.class.getConstructor(Object.class, Iterable.class));
Method getNativeCacheMethod = ReflectionUtils.findMethod(HazelcastCache.class, "getNativeCache");
Assert.state(getNativeCacheMethod != null, "Unable to find 'getNativeCache' method");
Constructor<?> constructor = HazelcastCacheMetrics.class.getConstructor(Object.class, Iterable.class);
hints.reflection().registerMethod(getNativeCacheMethod).registerConstructor(constructor);
}
catch (NoSuchMethodException ex) {
throw new IllegalStateException(ex);

View File

@@ -92,22 +92,24 @@ public class StartupEndpoint {
static class StartupEndpointRuntimeHints implements RuntimeHintsRegistrar {
private static final TypeReference DEFAULT_TAG = TypeReference
.of("org.springframework.boot.context.metrics.buffering.BufferedStartupStep$DefaultTag");
private static final TypeReference BUFFERED_STARTUP_STEP = TypeReference
.of("org.springframework.boot.context.metrics.buffering.BufferedStartupStep");
private static final TypeReference FLIGHT_RECORDER_TAG = TypeReference
.of("org.springframework.core.metrics.jfr.FlightRecorderStartupStep$FlightRecorderTag");
private static final TypeReference FLIGHT_RECORDER_STARTUP_STEP = TypeReference
.of("org.springframework.core.metrics.jfr.FlightRecorderStartupStep");
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerType(
TypeReference
.of("org.springframework.boot.context.metrics.buffering.BufferedStartupStep$DefaultTag"),
(hint) -> hint
.onReachableType(TypeReference
.of("org.springframework.boot.context.metrics.buffering.BufferedStartupStep"))
.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS));
hints.reflection().registerType(
TypeReference
.of("org.springframework.core.metrics.jfr.FlightRecorderStartupStep$FlightRecorderTag"),
(hint) -> hint
.onReachableType(
TypeReference.of("org.springframework.core.metrics.jfr.FlightRecorderStartupStep"))
.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS));
hints.reflection().registerType(DEFAULT_TAG, (hint) -> hint.onReachableType(BUFFERED_STARTUP_STEP)
.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS));
hints.reflection().registerType(FLIGHT_RECORDER_TAG, (hint) -> hint
.onReachableType(FLIGHT_RECORDER_STARTUP_STEP).withMembers(MemberCategory.INVOKE_PUBLIC_METHODS));
}
}

View File

@@ -40,7 +40,6 @@ class DispatcherHandlersMappingDescriptionProviderTests {
assertThat(RuntimeHintsPredicates.reflection().onType(DispatcherHandlerMappingDescription.class)
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS))
.accepts(runtimeHints);
}
}

View File

@@ -39,7 +39,6 @@ class FiltersMappingDescriptionProviderTests {
assertThat(RuntimeHintsPredicates.reflection().onType(FilterRegistrationMappingDescription.class)
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS))
.accepts(runtimeHints);
}
}