From c5de8dba178fc09c738c2a8c9501cc47f84b1c2e Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 17 Jun 2020 09:29:40 +0200 Subject: [PATCH] Polish previous commit Add author tag Rename 'getPayloadType' method to 'getGenericType' and ad javadoc Resolves #543 --- .../context/catalog/FunctionTypeUtils.java | 15 ++++++++++++--- .../context/catalog/SimpleFunctionRegistry.java | 2 +- .../context/config/JsonMessageConverter.java | 13 +++++-------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/FunctionTypeUtils.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/FunctionTypeUtils.java index 5ec671edb..9f39bb2be 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/FunctionTypeUtils.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/FunctionTypeUtils.java @@ -46,6 +46,8 @@ import org.springframework.util.ReflectionUtils; * Set of utility operations to interrogate function definitions. * * @author Oleg Zhurakousky + * @author Andrey Shlykov + * * @since 3.0 */ public final class FunctionTypeUtils { @@ -63,14 +65,21 @@ public final class FunctionTypeUtils { * @return 'true' if this type represents a {@link Collection}. Otherwise 'false'. */ public static boolean isTypeCollection(Type type) { - type = getPayloadType(type); + type = getGenericType(type); Type rawType = type instanceof ParameterizedType ? ((ParameterizedType) type).getRawType() : type; return rawType instanceof Class && Collection.class.isAssignableFrom((Class) rawType); } - public static Type getPayloadType(Type type) { - if (isPublisher(type)) { + /** + * A convenience method identical to {@link #getImmediateGenericType(Type, int)} + * for cases when provided 'type' is {@link Publisher} or {@link Message}. + * + * @param type type to interrogate + * @return generic type if possible otherwise the same type as provided + */ + public static Type getGenericType(Type type) { + if (isPublisher(type) || isMessage(type)) { type = getImmediateGenericType(type, 0); } if (isMessage(type)) { diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java index 1bfbb8d28..6457c8038 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java @@ -764,7 +764,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect if (value instanceof Message) { // see AWS adapter with Optional payload if (messageNeedsConversion(rawType, (Message) value)) { convertedValue = FunctionTypeUtils.isTypeCollection(type) - ? messageConverter.fromMessage((Message) value, (Class) rawType, FunctionTypeUtils.getPayloadType(type)) + ? messageConverter.fromMessage((Message) value, (Class) rawType, FunctionTypeUtils.getGenericType(type)) : messageConverter.fromMessage((Message) value, (Class) rawType); if (logger.isDebugEnabled()) { logger.debug("Converted from Message: " + convertedValue); diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/JsonMessageConverter.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/JsonMessageConverter.java index c46953cbf..9a34d284c 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/JsonMessageConverter.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/JsonMessageConverter.java @@ -16,7 +16,6 @@ package org.springframework.cloud.function.context.config; -import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import org.springframework.cloud.function.json.JsonMapper; @@ -32,6 +31,8 @@ import org.springframework.util.MimeType; * actual conversion via {@link JsonMapper} instance. * * @author Oleg Zhurakousky + * @author Andrey Shlykov + * * @since 3.0.4 */ public class JsonMessageConverter extends AbstractMessageConverter { @@ -74,13 +75,9 @@ public class JsonMessageConverter extends AbstractMessageConverter { if (targetClass.isInstance(message.getPayload())) { return message.getPayload(); } - Object result = null; - if (conversionHint == null) { - result = jsonMapper.fromJson(message.getPayload(), targetClass); - } - else if (conversionHint instanceof ParameterizedType) { - result = jsonMapper.fromJson(message.getPayload(), (Type) conversionHint); - } + + Type convertToType = conversionHint == null ? targetClass : (Type) conversionHint; + Object result = jsonMapper.fromJson(message.getPayload(), convertToType); return result; }