Add native support for @ExceptionHandler
This commit makes sure that `@ExceptionHandler`-annotated methods can be invoked via reflection in a native image. As most of the handling of the parameter and the return type is shared with our generic RequestMapping handling, the ReflectiveProcessor extends from it. An `@ExceptionHandler`-annotated method can return a `ProblemDetail`. If that's the case, reflection entries are contributed. Closes gh-29297
This commit is contained in:
parent
c94b676576
commit
45939720f2
|
|
@ -29,11 +29,12 @@ import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link ReflectiveProcessor} implementation for {@link RequestMapping}
|
* {@link ReflectiveProcessor} implementation for {@link Controller} and
|
||||||
* annotated types. On top of registering reflection hints for invoking
|
* controller-specific annotated methods. On top of registering reflection
|
||||||
* the annotated method, this implementation handles:
|
* hints for invoking the annotated method, this implementation handles:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>Return types annotated with {@link ResponseBody}.</li>
|
* <li>Return types annotated with {@link ResponseBody}.</li>
|
||||||
* <li>Parameters annotated with {@link RequestBody}.</li>
|
* <li>Parameters annotated with {@link RequestBody}.</li>
|
||||||
|
|
@ -44,7 +45,7 @@ import org.springframework.lang.Nullable;
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
* @since 6.0
|
* @since 6.0
|
||||||
*/
|
*/
|
||||||
class RequestMappingReflectiveProcessor implements ReflectiveProcessor {
|
class ControllerMappingReflectiveProcessor implements ReflectiveProcessor {
|
||||||
|
|
||||||
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
|
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
|
||||||
|
|
||||||
|
|
@ -58,45 +59,47 @@ class RequestMappingReflectiveProcessor implements ReflectiveProcessor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final BindingReflectionHintsRegistrar getBindingRegistrar() {
|
||||||
|
return this.bindingRegistrar;
|
||||||
|
}
|
||||||
|
|
||||||
protected void registerTypeHints(ReflectionHints hints, Class<?> type) {
|
protected void registerTypeHints(ReflectionHints hints, Class<?> type) {
|
||||||
hints.registerType(type);
|
hints.registerType(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void registerMethodHints(ReflectionHints hints, Method method) {
|
protected void registerMethodHints(ReflectionHints hints, Method method) {
|
||||||
hints.registerMethod(method, ExecutableMode.INVOKE);
|
|
||||||
registerParameterHints(hints, method);
|
|
||||||
registerReturnValueHints(hints, method);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void registerParameterHints(ReflectionHints hints, Method method) {
|
|
||||||
hints.registerMethod(method, ExecutableMode.INVOKE);
|
hints.registerMethod(method, ExecutableMode.INVOKE);
|
||||||
for (Parameter parameter : method.getParameters()) {
|
for (Parameter parameter : method.getParameters()) {
|
||||||
MethodParameter methodParameter = MethodParameter.forParameter(parameter);
|
registerParameterTypeHints(hints, MethodParameter.forParameter(parameter));
|
||||||
if (methodParameter.hasParameterAnnotation(RequestBody.class) ||
|
}
|
||||||
methodParameter.hasParameterAnnotation(ModelAttribute.class)) {
|
registerReturnTypeHints(hints, MethodParameter.forExecutable(method, -1));
|
||||||
this.bindingRegistrar.registerReflectionHints(hints, methodParameter.getGenericParameterType());
|
}
|
||||||
}
|
|
||||||
else if (HttpEntity.class.isAssignableFrom(methodParameter.getParameterType())) {
|
protected void registerParameterTypeHints(ReflectionHints hints, MethodParameter methodParameter) {
|
||||||
this.bindingRegistrar.registerReflectionHints(hints, getHttpEntityType(methodParameter));
|
if (methodParameter.hasParameterAnnotation(RequestBody.class) ||
|
||||||
}
|
methodParameter.hasParameterAnnotation(ModelAttribute.class)) {
|
||||||
|
this.bindingRegistrar.registerReflectionHints(hints, methodParameter.getGenericParameterType());
|
||||||
|
}
|
||||||
|
else if (HttpEntity.class.isAssignableFrom(methodParameter.getParameterType())) {
|
||||||
|
this.bindingRegistrar.registerReflectionHints(hints, getHttpEntityType(methodParameter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void registerReturnValueHints(ReflectionHints hints, Method method) {
|
protected void registerReturnTypeHints(ReflectionHints hints, MethodParameter returnTypeParameter) {
|
||||||
MethodParameter returnType = MethodParameter.forExecutable(method, -1);
|
if (AnnotatedElementUtils.hasAnnotation(returnTypeParameter.getContainingClass(), ResponseBody.class) ||
|
||||||
if (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) ||
|
returnTypeParameter.hasMethodAnnotation(ResponseBody.class)) {
|
||||||
returnType.hasMethodAnnotation(ResponseBody.class)) {
|
this.bindingRegistrar.registerReflectionHints(hints, returnTypeParameter.getGenericParameterType());
|
||||||
this.bindingRegistrar.registerReflectionHints(hints, returnType.getGenericParameterType());
|
|
||||||
}
|
}
|
||||||
else if (HttpEntity.class.isAssignableFrom(returnType.getParameterType())) {
|
else if (HttpEntity.class.isAssignableFrom(returnTypeParameter.getParameterType())) {
|
||||||
this.bindingRegistrar.registerReflectionHints(hints, getHttpEntityType(returnType));
|
this.bindingRegistrar.registerReflectionHints(hints, getHttpEntityType(returnTypeParameter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
protected Type getHttpEntityType(MethodParameter parameter) {
|
private Type getHttpEntityType(MethodParameter parameter) {
|
||||||
MethodParameter nestedParameter = parameter.nested();
|
MethodParameter nestedParameter = parameter.nested();
|
||||||
return (nestedParameter.getNestedParameterType() == nestedParameter.getParameterType() ? null : nestedParameter.getNestedParameterType());
|
return (nestedParameter.getNestedParameterType() == nestedParameter.getParameterType()
|
||||||
|
? null : nestedParameter.getNestedParameterType());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2021 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -22,6 +22,8 @@ import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import org.springframework.aot.hint.annotation.Reflective;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Annotation for handling exceptions in specific handler classes and/or
|
* Annotation for handling exceptions in specific handler classes and/or
|
||||||
* handler methods.
|
* handler methods.
|
||||||
|
|
@ -106,6 +108,7 @@ import java.lang.annotation.Target;
|
||||||
@Target(ElementType.METHOD)
|
@Target(ElementType.METHOD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Documented
|
@Documented
|
||||||
|
@Reflective(ExceptionHandlerReflectiveProcessor.class)
|
||||||
public @interface ExceptionHandler {
|
public @interface ExceptionHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2022 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.web.bind.annotation;
|
||||||
|
|
||||||
|
import org.springframework.aot.hint.ReflectionHints;
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.http.ProblemDetail;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link ControllerMappingReflectiveProcessor} specific implementation that
|
||||||
|
* handles {@link ExceptionHandler @ExceptionHandler}-specific types.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
* @since 6.0
|
||||||
|
*/
|
||||||
|
class ExceptionHandlerReflectiveProcessor extends ControllerMappingReflectiveProcessor{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void registerReturnTypeHints(ReflectionHints hints, MethodParameter returnTypeParameter) {
|
||||||
|
Class<?> returnType = returnTypeParameter.getParameterType();
|
||||||
|
if (ProblemDetail.class.isAssignableFrom(returnType)) {
|
||||||
|
getBindingRegistrar().registerReflectionHints(hints, returnTypeParameter.getGenericParameterType());
|
||||||
|
}
|
||||||
|
super.registerReturnTypeHints(hints, returnTypeParameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -73,7 +73,7 @@ import org.springframework.core.annotation.AliasFor;
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Documented
|
@Documented
|
||||||
@Mapping
|
@Mapping
|
||||||
@Reflective(RequestMappingReflectiveProcessor.class)
|
@Reflective(ControllerMappingReflectiveProcessor.class)
|
||||||
public @interface RequestMapping {
|
public @interface RequestMapping {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -28,13 +28,13 @@ import org.springframework.http.HttpEntity;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link RequestMappingReflectiveProcessor}.
|
* Tests for {@link ControllerMappingReflectiveProcessor}.
|
||||||
*
|
*
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
*/
|
*/
|
||||||
public class RequestMappingReflectiveProcessorTests {
|
public class ControllerMappingReflectiveProcessorTests {
|
||||||
|
|
||||||
private final RequestMappingReflectiveProcessor processor = new RequestMappingReflectiveProcessor();
|
private final ControllerMappingReflectiveProcessor processor = new ControllerMappingReflectiveProcessor();
|
||||||
|
|
||||||
private final ReflectionHints hints = new ReflectionHints();
|
private final ReflectionHints hints = new ReflectionHints();
|
||||||
|
|
||||||
Loading…
Reference in New Issue