pull/31928/head
Phillip Webb 2 years ago
parent 41e8697445
commit e08c16dfd6

@ -16,11 +16,11 @@
package org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.reactivestreams.Publisher;
@ -44,6 +44,7 @@ import org.springframework.context.aot.BindingReflectionHintsRegistrar;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping;
@ -159,8 +160,10 @@ class CloudFoundryWebFluxEndpointHandlerMapping extends AbstractWebFluxEndpointH
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerMethod(Objects.requireNonNull(
ReflectionUtils.findMethod(CloudFoundryLinksHandler.class, "links", ServerWebExchange.class)));
Method linksMethod = ReflectionUtils.findMethod(CloudFoundryLinksHandler.class, "links",
ServerWebExchange.class);
Assert.state(linksMethod != null, "Unable to find 'links' method");
hints.reflection().registerMethod(linksMethod);
this.bindingRegistrar.registerReflectionHints(hints.reflection(), Link.class);
}

@ -16,11 +16,11 @@
package org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import jakarta.servlet.http.HttpServletRequest;
@ -45,6 +45,7 @@ import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.context.aot.BindingReflectionHintsRegistrar;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.cors.CorsConfiguration;
@ -161,9 +162,10 @@ class CloudFoundryWebEndpointServletHandlerMapping extends AbstractWebMvcEndpoin
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection()
.registerMethod(Objects.requireNonNull(ReflectionUtils.findMethod(CloudFoundryLinksHandler.class,
"links", HttpServletRequest.class, HttpServletResponse.class)));
Method linksMethod = ReflectionUtils.findMethod(CloudFoundryLinksHandler.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);
}

@ -40,7 +40,6 @@ class ActuatorAnnotationsRuntimeHints implements RuntimeHintsRegistrar {
Stream.of(Endpoint.class, ReadOperation.class, WriteOperation.class, DeleteOperation.class,
EndpointExtension.class)
.forEach((annotationType) -> RuntimeHintsUtils.registerAnnotation(hints, annotationType));
// TODO: See https://github.com/spring-projects/spring-framework/issues/28767
Stream.of(Endpoint.class, EndpointExtension.class).forEach(
(annotationType) -> hints.proxies().registerJdkProxy(annotationType, SynthesizedAnnotation.class));
}

@ -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);
}
}

@ -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);
}
}

@ -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);
}

@ -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);
}
}

@ -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);
}

@ -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);

@ -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));
}
}

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

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

@ -22,6 +22,7 @@ import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.attributes.LibraryElements;
import org.gradle.api.file.Directory;
import org.gradle.api.plugins.JavaPlugin;
@ -77,12 +78,17 @@ public class SpringBootAotPlugin implements Plugin<Project> {
aotImplementation.extendsFrom(configurations.getByName(main.getImplementationConfigurationName()));
aotImplementation.extendsFrom(configurations.getByName(main.getRuntimeOnlyConfigurationName()));
configurations.getByName(aot.getCompileClasspathConfigurationName())
.attributes((attributes) -> attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
project.getObjects().named(LibraryElements.class, LibraryElements.CLASSES_AND_RESOURCES)));
.attributes((attributes) -> addLibraryElementsAttribute(project, attributes));
});
return aotSourceSet;
}
private AttributeContainer addLibraryElementsAttribute(Project project, AttributeContainer attributes) {
LibraryElements libraryElements = project.getObjects().named(LibraryElements.class,
LibraryElements.CLASSES_AND_RESOURCES);
return attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, libraryElements);
}
private void registerGenerateAotSourcesTask(Project project, SourceSet aotSourceSet) {
TaskProvider<ResolveMainClassName> resolveMainClassName = project.getTasks()
.named(SpringBootPlugin.RESOLVE_MAIN_CLASS_NAME_TASK_NAME, ResolveMainClassName.class);

@ -157,9 +157,9 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
return cleaned.toString();
}
static class TestArtifactFilter extends AbstractArtifactFeatureFilter {
protected static class TestScopeArtifactFilter extends AbstractArtifactFeatureFilter {
TestArtifactFilter() {
TestScopeArtifactFilter() {
super("", Artifact.SCOPE_TEST);
}

@ -371,7 +371,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
private void addDependencies(List<URL> urls) throws MalformedURLException, MojoExecutionException {
Set<Artifact> artifacts = (this.useTestClasspath) ? filterDependencies(this.project.getArtifacts())
: filterDependencies(this.project.getArtifacts(), new TestArtifactFilter());
: filterDependencies(this.project.getArtifacts(), new TestScopeArtifactFilter());
for (Artifact artifact : artifacts) {
if (artifact.getFile() != null) {
urls.add(artifact.getFile().toURI().toURL());

@ -154,6 +154,18 @@ public class AotGenerateMojo extends AbstractDependencyFilterMojo {
private void generateAotAssets() throws MojoExecutionException {
String applicationClass = (this.mainClass != null) ? this.mainClass
: SpringBootApplicationClassFinder.findSingleClass(this.classesDirectory);
List<String> command = CommandLineBuilder.forMainClass(AOT_PROCESSOR_CLASS_NAME)
.withSystemProperties(this.systemPropertyVariables)
.withJvmArguments(new RunArguments(this.jvmArguments).asArray()).withClasspath(getClassPathUrls())
.withArguments(getAotArguments(applicationClass)).build();
if (getLog().isDebugEnabled()) {
getLog().debug("Generating AOT assets using command: " + command);
}
JavaProcessExecutor processExecutor = new JavaProcessExecutor(this.session, this.toolchainManager);
processExecutor.run(this.project.getBasedir(), command, Collections.emptyMap());
}
private String[] getAotArguments(String applicationClass) {
List<String> aotArguments = new ArrayList<>();
aotArguments.add(applicationClass);
aotArguments.add(this.generatedSources.toString());
@ -164,25 +176,13 @@ public class AotGenerateMojo extends AbstractDependencyFilterMojo {
if (!ObjectUtils.isEmpty(this.profiles)) {
aotArguments.add("--spring.profiles.active=" + String.join(",", this.profiles));
}
// @formatter:off
List<String> args = CommandLineBuilder.forMainClass(AOT_PROCESSOR_CLASS_NAME)
.withSystemProperties(this.systemPropertyVariables)
.withJvmArguments(new RunArguments(this.jvmArguments).asArray())
.withClasspath(getClassPathUrls())
.withArguments(aotArguments.toArray(String[]::new))
.build();
// @formatter:on
if (getLog().isDebugEnabled()) {
getLog().debug("Generating AOT assets using command: " + args);
}
JavaProcessExecutor processExecutor = new JavaProcessExecutor(this.session, this.toolchainManager);
processExecutor.run(this.project.getBasedir(), args, Collections.emptyMap());
return aotArguments.toArray(String[]::new);
}
private URL[] getClassPathUrls() throws MojoExecutionException {
List<URL> urls = new ArrayList<>();
urls.add(toURL(this.classesDirectory));
urls.addAll(getDependencyURLs(new TestArtifactFilter()));
urls.addAll(getDependencyURLs(new TestScopeArtifactFilter()));
return urls.toArray(URL[]::new);
}

@ -109,11 +109,14 @@ public final class ConfigurationPropertiesReflectionHintsProcessor {
private void handleConstructor(ReflectionHints reflectionHints) {
if (this.bindConstructor != null) {
reflectionHints.registerConstructor(this.bindConstructor);
return;
}
else {
Arrays.stream(this.type.getDeclaredConstructors()).filter((candidate) -> candidate.getParameterCount() == 0)
.findFirst().ifPresent(reflectionHints::registerConstructor);
}
Arrays.stream(this.type.getDeclaredConstructors()).filter(this::hasNoParameters).findFirst()
.ifPresent(reflectionHints::registerConstructor);
}
private boolean hasNoParameters(Constructor<?> candidate) {
return candidate.getParameterCount() == 0;
}
private void handleValueObjectProperties(ReflectionHints reflectionHints) {
@ -138,6 +141,17 @@ public final class ConfigurationPropertiesReflectionHintsProcessor {
}
}
private boolean isSetterMandatory(String propertyName, ResolvableType propertyType) {
Class<?> propertyClass = propertyType.resolve();
if (propertyClass == null) {
return true;
}
if (isContainer(propertyType)) {
return false;
}
return !isNestedType(propertyName, propertyClass);
}
private void handleProperty(ReflectionHints reflectionHints, String propertyName, ResolvableType propertyType) {
Class<?> propertyClass = propertyType.resolve();
if (propertyClass == null) {
@ -146,7 +160,7 @@ public final class ConfigurationPropertiesReflectionHintsProcessor {
if (propertyClass.equals(this.type)) {
return; // Prevent infinite recursion
}
Class<?> componentType = getComponentType(propertyType);
Class<?> componentType = getComponentClass(propertyType);
if (componentType != null) {
// Can be a list of simple types
if (!isJavaType(componentType)) {
@ -158,50 +172,41 @@ public final class ConfigurationPropertiesReflectionHintsProcessor {
}
}
private boolean isSetterMandatory(String propertyName, ResolvableType propertyType) {
Class<?> propertyClass = propertyType.resolve();
if (propertyClass == null) {
return true;
}
if (isContainer(propertyType)) {
return false;
}
return !isNestedType(propertyName, propertyClass);
}
private Class<?> getComponentType(ResolvableType propertyType) {
Class<?> propertyClass = propertyType.toClass();
ResolvableType componentType = null;
if (propertyType.isArray()) {
componentType = propertyType.getComponentType();
}
else if (Collection.class.isAssignableFrom(propertyClass)) {
componentType = propertyType.asCollection().getGeneric(0);
}
else if (Map.class.isAssignableFrom(propertyClass)) {
componentType = propertyType.asMap().getGeneric(1);
}
private Class<?> getComponentClass(ResolvableType type) {
ResolvableType componentType = getComponentType(type);
if (componentType == null) {
return null;
}
if (isContainer(componentType)) {
// Resolve nested generics like Map<String, List<SomeType>>
return getComponentType(componentType);
return getComponentClass(componentType);
}
return componentType.toClass();
}
private boolean isContainer(ResolvableType type) {
private ResolvableType getComponentType(ResolvableType type) {
if (type.isArray()) {
return true;
return type.getComponentType();
}
if (Collection.class.isAssignableFrom(type.toClass())) {
return true;
if (isCollection(type)) {
return type.asCollection().getGeneric();
}
else if (Map.class.isAssignableFrom(type.toClass())) {
return true;
if (isMap(type)) {
return type.asMap().getGeneric(1);
}
return false;
return null;
}
private boolean isContainer(ResolvableType type) {
return type.isArray() || isCollection(type) || isMap(type);
}
private boolean isCollection(ResolvableType type) {
return Collection.class.isAssignableFrom(type.toClass());
}
private boolean isMap(ResolvableType type) {
return Map.class.isAssignableFrom(type.toClass());
}
/**

Loading…
Cancel
Save