|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.messaging.handler.annotation; |
| 18 | + |
| 19 | +import java.lang.reflect.AnnotatedElement; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.lang.reflect.Parameter; |
| 22 | +import java.lang.reflect.Type; |
| 23 | +import java.security.Principal; |
| 24 | + |
| 25 | +import org.springframework.aot.hint.ExecutableMode; |
| 26 | +import org.springframework.aot.hint.ReflectionHints; |
| 27 | +import org.springframework.aot.hint.annotation.ReflectiveProcessor; |
| 28 | +import org.springframework.context.aot.BindingReflectionHintsRegistrar; |
| 29 | +import org.springframework.core.MethodParameter; |
| 30 | +import org.springframework.lang.Nullable; |
| 31 | +import org.springframework.messaging.Message; |
| 32 | +import org.springframework.messaging.MessageHeaders; |
| 33 | +import org.springframework.messaging.support.MessageHeaderAccessor; |
| 34 | + |
| 35 | +/** |
| 36 | + * {@link ReflectiveProcessor} implementation for {@link MessageMapping} |
| 37 | + * annotated types. On top of registering reflection hints for invoking |
| 38 | + * the annotated method, this implementation handles: |
| 39 | + * <ul> |
| 40 | + * <li>Return types.</li> |
| 41 | + * <li>Parameters identified as potential payload.</li> |
| 42 | + * <li>{@link Message} parameters.</li> |
| 43 | + * </ul> |
| 44 | + * |
| 45 | + * @author Sebastien Deleuze |
| 46 | + * @since 6.0 |
| 47 | + */ |
| 48 | +class MessageMappingReflectiveProcessor implements ReflectiveProcessor { |
| 49 | + |
| 50 | + private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar(); |
| 51 | + |
| 52 | + @Override |
| 53 | + public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) { |
| 54 | + if (element instanceof Class<?> type) { |
| 55 | + registerTypeHints(hints, type); |
| 56 | + } |
| 57 | + else if (element instanceof Method method) { |
| 58 | + registerMethodHints(hints, method); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + protected void registerTypeHints(ReflectionHints hints, Class<?> type) { |
| 63 | + hints.registerType(type, hint -> {}); |
| 64 | + } |
| 65 | + |
| 66 | + protected void registerMethodHints(ReflectionHints hints, Method method) { |
| 67 | + hints.registerMethod(method, hint -> hint.setModes(ExecutableMode.INVOKE)); |
| 68 | + registerParameterHints(hints, method); |
| 69 | + registerReturnValueHints(hints, method); |
| 70 | + } |
| 71 | + |
| 72 | + protected void registerParameterHints(ReflectionHints hints, Method method) { |
| 73 | + hints.registerMethod(method, hint -> hint.setModes(ExecutableMode.INVOKE)); |
| 74 | + for (Parameter parameter : method.getParameters()) { |
| 75 | + MethodParameter methodParameter = MethodParameter.forParameter(parameter); |
| 76 | + if (Message.class.isAssignableFrom(methodParameter.getParameterType())) { |
| 77 | + this.bindingRegistrar.registerReflectionHints(hints, getMessageType(methodParameter)); |
| 78 | + } |
| 79 | + else if (couldBePayload(methodParameter)) { |
| 80 | + this.bindingRegistrar.registerReflectionHints(hints, methodParameter.getGenericParameterType()); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + protected boolean couldBePayload(MethodParameter methodParameter) { |
| 86 | + return !methodParameter.hasParameterAnnotation(DestinationVariable.class) && |
| 87 | + !methodParameter.hasParameterAnnotation(Header.class) && |
| 88 | + !methodParameter.hasParameterAnnotation(Headers.class) && |
| 89 | + !MessageHeaders.class.isAssignableFrom(methodParameter.getParameterType()) && |
| 90 | + !MessageHeaderAccessor.class.isAssignableFrom(methodParameter.getParameterType()) && |
| 91 | + !Principal.class.isAssignableFrom(methodParameter.nestedIfOptional().getNestedParameterType()); |
| 92 | + } |
| 93 | + |
| 94 | + protected void registerReturnValueHints(ReflectionHints hints, Method method) { |
| 95 | + MethodParameter returnType = MethodParameter.forExecutable(method, -1); |
| 96 | + this.bindingRegistrar.registerReflectionHints(hints, returnType.getGenericParameterType()); |
| 97 | + } |
| 98 | + |
| 99 | + @Nullable |
| 100 | + protected Type getMessageType(MethodParameter parameter) { |
| 101 | + MethodParameter nestedParameter = parameter.nested(); |
| 102 | + return (nestedParameter.getNestedParameterType() == nestedParameter.getParameterType() ? null : nestedParameter.getNestedParameterType()); |
| 103 | + } |
| 104 | +} |
0 commit comments