From 5ace9b764b918ba56c0498fb18ec124797bfe9e9 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 10 Mar 2017 17:40:05 +0000 Subject: [PATCH] Resort to reflection if the function is not a @Bean Beans that are not created from a @Bean factory method do not have the same kind of metadata. This change uses relection to extract the target type from the bean definition if all else fails. --- ...ntextFunctionCatalogAutoConfiguration.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java index 89e47553f..8fa764b9b 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.cloud.function.context; +import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Collections; @@ -48,6 +49,7 @@ import org.springframework.core.ResolvableType; import org.springframework.core.type.StandardMethodMetadata; import org.springframework.stereotype.Component; import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; import reactor.core.publisher.Flux; @@ -246,10 +248,19 @@ public class ContextFunctionCatalogAutoConfiguration { private Class findType(RootBeanDefinition definition, int index) { StandardMethodMetadata source = (StandardMethodMetadata) definition .getSource(); - ParameterizedType type = (ParameterizedType) (source.getIntrospectedMethod() - .getGenericReturnType()); - type = (ParameterizedType) type.getActualTypeArguments()[index]; - Type param = type.getActualTypeArguments()[0]; + Type param; + if (source instanceof StandardMethodMetadata) { + ParameterizedType type; + type = (ParameterizedType) (source.getIntrospectedMethod() + .getGenericReturnType()); + type = (ParameterizedType) type.getActualTypeArguments()[index]; + param = type.getActualTypeArguments()[0]; + } + else { + ResolvableType resolvable = (ResolvableType) getField(definition, + "targetType"); + param = resolvable.getGeneric(index).getGeneric(0).getType(); + } if (param instanceof ParameterizedType) { ParameterizedType concrete = (ParameterizedType) param; param = concrete.getRawType(); @@ -258,6 +269,12 @@ public class ContextFunctionCatalogAutoConfiguration { registry.getClass().getClassLoader()); } + private Object getField(Object target, String name) { + Field field = ReflectionUtils.findField(target.getClass(), name); + ReflectionUtils.makeAccessible(field); + return ReflectionUtils.getField(field, target); + } + private Class findType(String name) { return findType((RootBeanDefinition) registry.getBeanDefinition(name), 0); }