Polish previous commit
Add author tag Rename 'getPayloadType' method to 'getGenericType' and ad javadoc Resolves #543
This commit is contained in:
@@ -46,6 +46,8 @@ import org.springframework.util.ReflectionUtils;
|
|||||||
* Set of utility operations to interrogate function definitions.
|
* Set of utility operations to interrogate function definitions.
|
||||||
*
|
*
|
||||||
* @author Oleg Zhurakousky
|
* @author Oleg Zhurakousky
|
||||||
|
* @author Andrey Shlykov
|
||||||
|
*
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public final class FunctionTypeUtils {
|
public final class FunctionTypeUtils {
|
||||||
@@ -63,14 +65,21 @@ public final class FunctionTypeUtils {
|
|||||||
* @return 'true' if this type represents a {@link Collection}. Otherwise 'false'.
|
* @return 'true' if this type represents a {@link Collection}. Otherwise 'false'.
|
||||||
*/
|
*/
|
||||||
public static boolean isTypeCollection(Type type) {
|
public static boolean isTypeCollection(Type type) {
|
||||||
type = getPayloadType(type);
|
type = getGenericType(type);
|
||||||
Type rawType = type instanceof ParameterizedType ? ((ParameterizedType) type).getRawType() : type;
|
Type rawType = type instanceof ParameterizedType ? ((ParameterizedType) type).getRawType() : type;
|
||||||
|
|
||||||
return rawType instanceof Class<?> && Collection.class.isAssignableFrom((Class<?>) rawType);
|
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);
|
type = getImmediateGenericType(type, 0);
|
||||||
}
|
}
|
||||||
if (isMessage(type)) {
|
if (isMessage(type)) {
|
||||||
|
|||||||
@@ -764,7 +764,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
|||||||
if (value instanceof Message<?>) { // see AWS adapter with Optional payload
|
if (value instanceof Message<?>) { // see AWS adapter with Optional payload
|
||||||
if (messageNeedsConversion(rawType, (Message<?>) value)) {
|
if (messageNeedsConversion(rawType, (Message<?>) value)) {
|
||||||
convertedValue = FunctionTypeUtils.isTypeCollection(type)
|
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);
|
: messageConverter.fromMessage((Message<?>) value, (Class<?>) rawType);
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("Converted from Message: " + convertedValue);
|
logger.debug("Converted from Message: " + convertedValue);
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.function.context.config;
|
package org.springframework.cloud.function.context.config;
|
||||||
|
|
||||||
import java.lang.reflect.ParameterizedType;
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
import org.springframework.cloud.function.json.JsonMapper;
|
import org.springframework.cloud.function.json.JsonMapper;
|
||||||
@@ -32,6 +31,8 @@ import org.springframework.util.MimeType;
|
|||||||
* actual conversion via {@link JsonMapper} instance.
|
* actual conversion via {@link JsonMapper} instance.
|
||||||
*
|
*
|
||||||
* @author Oleg Zhurakousky
|
* @author Oleg Zhurakousky
|
||||||
|
* @author Andrey Shlykov
|
||||||
|
*
|
||||||
* @since 3.0.4
|
* @since 3.0.4
|
||||||
*/
|
*/
|
||||||
public class JsonMessageConverter extends AbstractMessageConverter {
|
public class JsonMessageConverter extends AbstractMessageConverter {
|
||||||
@@ -74,13 +75,9 @@ public class JsonMessageConverter extends AbstractMessageConverter {
|
|||||||
if (targetClass.isInstance(message.getPayload())) {
|
if (targetClass.isInstance(message.getPayload())) {
|
||||||
return message.getPayload();
|
return message.getPayload();
|
||||||
}
|
}
|
||||||
Object result = null;
|
|
||||||
if (conversionHint == null) {
|
Type convertToType = conversionHint == null ? targetClass : (Type) conversionHint;
|
||||||
result = jsonMapper.fromJson(message.getPayload(), targetClass);
|
Object result = jsonMapper.fromJson(message.getPayload(), convertToType);
|
||||||
}
|
|
||||||
else if (conversionHint instanceof ParameterizedType) {
|
|
||||||
result = jsonMapper.fromJson(message.getPayload(), (Type) conversionHint);
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user