Cleaned up FunctionContextUtils

cleaned up FunctionContextUtils to be more generic and return Type instead of FunctionType
This commit is contained in:
Oleg Zhurakousky
2018-11-08 11:17:16 +01:00
parent 8eb7e06e02
commit d1b9a9b3fb
3 changed files with 23 additions and 43 deletions

View File

@@ -577,7 +577,7 @@ public class ContextFunctionCatalogAutoConfiguration {
} }
} }
else { else {
param = FunctionContextUtils.findType(name, registry); param = new FunctionType(FunctionContextUtils.findType(name, registry));
} }
types.computeIfAbsent(name, str -> param); types.computeIfAbsent(name, str -> param);
return param; return param;

View File

@@ -16,11 +16,18 @@
package org.springframework.cloud.function.context.config; package org.springframework.cloud.function.context.config;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.cloud.function.context.FunctionType;
import org.springframework.cloud.function.core.FunctionFactoryMetadata; import org.springframework.cloud.function.core.FunctionFactoryMetadata;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@@ -29,15 +36,6 @@ import org.springframework.core.type.classreading.MethodMetadataReadingVisitor;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
/** /**
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* *
@@ -45,56 +43,38 @@ import java.util.List;
*/ */
abstract class FunctionContextUtils { abstract class FunctionContextUtils {
public static FunctionType findType(String name, ConfigurableListableBeanFactory registry) { public static Type findType(String name, ConfigurableListableBeanFactory registry) {
AbstractBeanDefinition definition = (AbstractBeanDefinition) registry.getBeanDefinition(name); AbstractBeanDefinition definition = (AbstractBeanDefinition) registry.getBeanDefinition(name);
Object source = definition.getSource(); Object source = definition.getSource();
FunctionType param = null; Type param = null;
// Start by assuming output -> Function // Start by assuming output -> Function
if (source instanceof StandardMethodMetadata) { if (source instanceof StandardMethodMetadata) {
// Standard @Bean metadata // Standard @Bean metadata
Type beanType = ((StandardMethodMetadata) source).getIntrospectedMethod() param = ((StandardMethodMetadata) source).getIntrospectedMethod()
.getGenericReturnType(); .getGenericReturnType();
if (beanType instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) beanType;
param = new FunctionType(type);
}
else {
param = new FunctionType(beanType);
}
} }
else if (source instanceof MethodMetadataReadingVisitor) { else if (source instanceof MethodMetadataReadingVisitor) {
// A component scan with @Beans // A component scan with @Beans
MethodMetadataReadingVisitor visitor = (MethodMetadataReadingVisitor) source; MethodMetadataReadingVisitor visitor = (MethodMetadataReadingVisitor) source;
Type type = findBeanType(definition, visitor); param = findBeanType(definition, visitor);
param = new FunctionType(type);
} }
else if (source instanceof Resource) { else if (source instanceof Resource) {
Class<?> beanType = registry.getType(name); param = registry.getType(name);
param = new FunctionType(beanType);
} }
else { else {
ResolvableType resolvable = (ResolvableType) getField(definition, ResolvableType type = (ResolvableType) getField(definition, "targetType");
"targetType"); if (type != null) {
if (resolvable != null) { param = type.getType();
param = new FunctionType(resolvable.getType());
} }
else { else {
Class<?> beanClass = definition.getBeanClass(); Class<?> beanClass = definition.getBeanClass();
if (beanClass != null && !FunctionFactoryMetadata.class if (beanClass != null && !FunctionFactoryMetadata.class.isAssignableFrom(beanClass)) {
.isAssignableFrom(beanClass)) { param = beanClass;
Type type = beanClass;
param = new FunctionType(type);
} }
else { else {
Object bean = registry.getBean(name); //assume FunctionFactoryMetadata
if (bean instanceof FunctionFactoryMetadata) { FunctionFactoryMetadata<?> factory = (FunctionFactoryMetadata<?>) registry.getBean(name);
FunctionFactoryMetadata<?> factory = (FunctionFactoryMetadata<?>) bean; param = factory.getFactoryMethod().getGenericReturnType();
Type type = factory.getFactoryMethod().getGenericReturnType();
param = new FunctionType(type);
}
else {
param = new FunctionType(bean.getClass());
}
} }
} }
} }

View File

@@ -74,7 +74,7 @@ class KotlinLambdaToFunctionAutoConfiguration {
if (source instanceof MethodMetadata) { if (source instanceof MethodMetadata) {
String returnTypeName = ((MethodMetadata)source).getReturnTypeName(); String returnTypeName = ((MethodMetadata)source).getReturnTypeName();
if (returnTypeName.startsWith("kotlin.jvm.functions.Function")) { if (returnTypeName.startsWith("kotlin.jvm.functions.Function")) {
FunctionType functionType = FunctionContextUtils.findType(beanDefinitionName, beanFactory); FunctionType functionType = new FunctionType(FunctionContextUtils.findType(beanDefinitionName, beanFactory));
if (returnTypeName.equals("kotlin.jvm.functions.Function1")) { if (returnTypeName.equals("kotlin.jvm.functions.Function1")) {
if (Unit.class.isAssignableFrom(functionType.getOutputType())) { if (Unit.class.isAssignableFrom(functionType.getOutputType())) {
logger.debug("Transforming Kotlin lambda " + beanDefinitionName + " to java Consumer"); logger.debug("Transforming Kotlin lambda " + beanDefinitionName + " to java Consumer");