GH-474 Add TypeResolver library and simplify type discovery
For complex cases where deep hierarchies are used there was still an issue with the fix in #473. By adding TypeResolver library we essentially simplify our discovery process Resolves #474
This commit is contained in:
@@ -254,6 +254,9 @@ public class BeanFactoryAwareFunctionRegistry
|
||||
}
|
||||
|
||||
private Type discoverFunctionType(Object function, String... names) {
|
||||
if (function instanceof RoutingFunction) {
|
||||
return FunctionType.of(FunctionContextUtils.findType(applicationContext.getBeanFactory(), names)).getType();
|
||||
}
|
||||
boolean beanDefinitionExists = false;
|
||||
for (int i = 0; i < names.length && !beanDefinitionExists; i++) {
|
||||
beanDefinitionExists = this.applicationContext.getBeanFactory().containsBeanDefinition(names[i]);
|
||||
@@ -267,9 +270,15 @@ public class BeanFactoryAwareFunctionRegistry
|
||||
logger.info("BeanDefinition for function name(s) '" + Arrays.asList(names) +
|
||||
"' can not be located. FunctionType will be based on " + function.getClass());
|
||||
}
|
||||
return beanDefinitionExists
|
||||
? FunctionType.of(FunctionContextUtils.findType(applicationContext.getBeanFactory(), names)).getType()
|
||||
: new FunctionType(function.getClass()).getType();
|
||||
|
||||
Type type = FunctionTypeUtils.discoverFunctionTypeFromClass(function.getClass());
|
||||
if (beanDefinitionExists) {
|
||||
Type t = FunctionTypeUtils.getImmediateGenericType(type, 0);
|
||||
if (t == null || t == Object.class) {
|
||||
type = FunctionType.of(FunctionContextUtils.findType(this.applicationContext.getBeanFactory(), names)).getType();
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
private String discoverDefaultDefinitionIfNecessary(String definition) {
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.jodah.typetools.TypeResolver;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.util.function.Tuple2;
|
||||
|
||||
@@ -118,29 +119,18 @@ public final class FunctionTypeUtils {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Type discoverFunctionTypeFromClass(Class<?> functionalClass) {
|
||||
Assert.isTrue(isFunctional(functionalClass), "Type must be one of Supplier, Function or Consumer");
|
||||
Type[] generics = functionalClass.getGenericInterfaces();
|
||||
if (ObjectUtils.isEmpty(generics)) {
|
||||
return functionalClass;
|
||||
|
||||
if (Function.class.isAssignableFrom(functionalClass)) {
|
||||
return TypeResolver.reify(Function.class, (Class<Function<?, ?>>) functionalClass);
|
||||
}
|
||||
else {
|
||||
for (Type generic : generics) {
|
||||
if (generic instanceof ParameterizedType) {
|
||||
Class<?> rawClsss = (Class<?>) ((ParameterizedType) generic).getRawType();
|
||||
if (Function.class.isAssignableFrom(rawClsss)
|
||||
|| Consumer.class.isAssignableFrom(rawClsss)
|
||||
|| Supplier.class.isAssignableFrom(rawClsss)) {
|
||||
return generic;
|
||||
}
|
||||
else {
|
||||
return discoverFunctionTypeFromClass(rawClsss);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return discoverFunctionTypeFromClass((Class<?>) generic);
|
||||
}
|
||||
}
|
||||
else if (Consumer.class.isAssignableFrom(functionalClass)) {
|
||||
return TypeResolver.reify(Consumer.class, (Class<Consumer<?>>) functionalClass);
|
||||
}
|
||||
else if (Supplier.class.isAssignableFrom(functionalClass)) {
|
||||
return TypeResolver.reify(Supplier.class, (Class<Supplier<?>>) functionalClass);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user