diff --git a/spring-web/src/main/java/org/springframework/web/service/annotation/HttpExchange.java b/spring-web/src/main/java/org/springframework/web/service/annotation/HttpExchange.java index 774d33b590..9f63e77f04 100644 --- a/spring-web/src/main/java/org/springframework/web/service/annotation/HttpExchange.java +++ b/spring-web/src/main/java/org/springframework/web/service/annotation/HttpExchange.java @@ -22,6 +22,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.aot.hint.annotation.Reflective; import org.springframework.core.annotation.AliasFor; import org.springframework.web.bind.annotation.Mapping; @@ -105,6 +106,7 @@ import org.springframework.web.bind.annotation.Mapping; @Retention(RetentionPolicy.RUNTIME) @Documented @Mapping +@Reflective(HttpExchangeReflectiveProcessor.class) public @interface HttpExchange { /** diff --git a/spring-web/src/main/java/org/springframework/web/service/annotation/HttpExchangeReflectiveProcessor.java b/spring-web/src/main/java/org/springframework/web/service/annotation/HttpExchangeReflectiveProcessor.java new file mode 100644 index 0000000000..16e24ab6b8 --- /dev/null +++ b/spring-web/src/main/java/org/springframework/web/service/annotation/HttpExchangeReflectiveProcessor.java @@ -0,0 +1,70 @@ +/* + * 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.service.annotation; + +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; + +import org.springframework.aot.hint.BindingReflectionHintsRegistrar; +import org.springframework.aot.hint.ExecutableMode; +import org.springframework.aot.hint.ReflectionHints; +import org.springframework.aot.hint.annotation.ReflectiveProcessor; +import org.springframework.core.MethodParameter; +import org.springframework.web.bind.annotation.RequestBody; + +/** + * {@link ReflectiveProcessor} implementation for {@link HttpExchange @HttpExchange} + * and annotated methods. On top of registering reflection hints for invoking + * the annotated method, this implementation handles reflection-based + * binding for return types and parameters annotated with {@link RequestBody}. + * + * @author Sebastien Deleuze + * @since 6.0 + */ +class HttpExchangeReflectiveProcessor implements ReflectiveProcessor { + + private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar(); + + @Override + public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) { + if (element instanceof Method method) { + registerMethodHints(hints, method); + } + } + + protected void registerMethodHints(ReflectionHints hints, Method method) { + hints.registerMethod(method, ExecutableMode.INVOKE); + for (Parameter parameter : method.getParameters()) { + registerParameterTypeHints(hints, MethodParameter.forParameter(parameter)); + } + registerReturnTypeHints(hints, MethodParameter.forExecutable(method, -1)); + } + + protected void registerParameterTypeHints(ReflectionHints hints, MethodParameter methodParameter) { + if (methodParameter.hasParameterAnnotation(RequestBody.class)) { + this.bindingRegistrar.registerReflectionHints(hints, methodParameter.getGenericParameterType()); + } + } + + protected void registerReturnTypeHints(ReflectionHints hints, MethodParameter returnTypeParameter) { + if (!void.class.equals(returnTypeParameter.getParameterType())) { + this.bindingRegistrar.registerReflectionHints(hints, returnTypeParameter.getGenericParameterType()); + } + } + +} diff --git a/spring-web/src/test/java/org/springframework/web/service/annotation/HttpExchangeReflectiveProcessorTests.java b/spring-web/src/test/java/org/springframework/web/service/annotation/HttpExchangeReflectiveProcessorTests.java new file mode 100644 index 0000000000..04ff2ae21c --- /dev/null +++ b/spring-web/src/test/java/org/springframework/web/service/annotation/HttpExchangeReflectiveProcessorTests.java @@ -0,0 +1,102 @@ +/* + * 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.service.annotation; + +import java.lang.reflect.Method; + +import org.junit.jupiter.api.Test; + +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; +import org.springframework.web.bind.annotation.RequestBody; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link HttpExchangeReflectiveProcessor}. + * + * @author Sebastien Deleuze + */ +class HttpExchangeReflectiveProcessorTests { + + private final HttpExchangeReflectiveProcessor processor = new HttpExchangeReflectiveProcessor(); + + private final RuntimeHints hints = new RuntimeHints(); + + @Test + void registerReflectiveHintsForMethodWithReturnValue() throws NoSuchMethodException { + Method method = SampleService.class.getDeclaredMethod("get"); + processor.registerReflectionHints(hints.reflection(), method); + assertThat(RuntimeHintsPredicates.reflection().onType(SampleService.class)).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleService.class, "get")).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onType(Response.class)).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onMethod(Response.class, "getMessage")).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onMethod(Response.class, "setMessage")).accepts(hints); + } + + @Test + void registerReflectiveHintsForMethodWithRequestBodyParameter() throws NoSuchMethodException { + Method method = SampleService.class.getDeclaredMethod("post", Request.class); + processor.registerReflectionHints(hints.reflection(), method); + assertThat(RuntimeHintsPredicates.reflection().onType(SampleService.class)).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleService.class, "post")).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onType(Request.class)).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onMethod(Request.class, "getMessage")).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onMethod(Request.class, "setMessage")).accepts(hints); + } + + + interface SampleService { + + @GetExchange + Response get(); + + @PostExchange + void post(@RequestBody Request request); + } + + static class Request { + + private String message; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + } + + static class Response { + + private String message; + + public Response(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + } + +}