Deprecate and remove all usages of FunctionInspector

This commit is contained in:
Oleg Zhurakousky
2020-10-19 15:30:11 +02:00
parent 3f1315c523
commit 818cda144c
12 changed files with 268 additions and 319 deletions

View File

@@ -36,6 +36,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.context.config.FunctionContextUtils;
import org.springframework.cloud.function.context.config.RoutingFunction;
import org.springframework.cloud.function.json.JsonMapper;
@@ -138,15 +140,18 @@ public abstract class AbstractSpringFunctionAdapterInitializer<C> implements Clo
}
}
@Deprecated
protected FunctionInspector getInspector() {
return inspector;
}
protected Class<?> getInputType() {
if (this.inspector != null) {
return this.inspector.getInputType(function());
Object func = function();
if (func != null && func instanceof FunctionInvocationWrapper) {
return FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) func).getInputType()));
}
else if (functionRegistration != null) {
if (functionRegistration != null) {
return functionRegistration.getType().getInputType();
}
return Object.class;
@@ -237,7 +242,7 @@ public abstract class AbstractSpringFunctionAdapterInitializer<C> implements Clo
}
if (getInspector() != null) {
return Collection.class
.isAssignableFrom(getInspector().getInputType(function));
.isAssignableFrom(((FunctionInvocationWrapper) function).getRawInputType());
}
return ((Collection<?>) input).size() <= 1;
}
@@ -247,8 +252,8 @@ public abstract class AbstractSpringFunctionAdapterInitializer<C> implements Clo
return true;
}
if (getInspector() != null) {
return Collection.class
.isAssignableFrom(getInspector().getOutputType(function));
Class<?> outputType = FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) function).getOutputType()));
return Collection.class.isAssignableFrom(outputType);
}
return ((Collection<?>) output).size() <= 1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,7 +31,10 @@ import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry
/**
* @author Dave Syer
* @author Oleg Zhurakousky
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
public interface FunctionInspector {
FunctionRegistration<?> getRegistration(Object function);
@@ -49,6 +52,11 @@ public interface FunctionInspector {
return ((FunctionInvocationWrapper) function).isInputTypeMessage();
}
/**
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
default Class<?> getInputType(Object function) {
if (function == null) {
return Object.class;
@@ -69,6 +77,11 @@ public interface FunctionInspector {
return inputType;
}
/**
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
default Class<?> getOutputType(Object function) {
if (function == null) {
return Object.class;
@@ -89,6 +102,11 @@ public interface FunctionInspector {
return outputType;
}
/**
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
default Class<?> getInputWrapper(Object function) {
Class c = function == null ? Object.class : TypeResolver.resolveRawClass(((FunctionInvocationWrapper) function).getInputType(), null);
if (Flux.class.isAssignableFrom(c)) {
@@ -102,6 +120,11 @@ public interface FunctionInspector {
}
}
/**
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
default Class<?> getOutputWrapper(Object function) {
Class c = function == null ? Object.class : TypeResolver.resolveRawClass(((FunctionInvocationWrapper) function).getOutputType(), null);
if (Flux.class.isAssignableFrom(c)) {
@@ -115,6 +138,11 @@ public interface FunctionInspector {
}
}
/**
*
* @deprecated since 3.1 no longer used by the framework
*/
@Deprecated
default String getName(Object function) {
if (function == null) {
return null;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019-2019 the original author or authors.
* Copyright 2019-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@ import java.util.function.Supplier;
import java.util.stream.Stream;
import net.jodah.typetools.TypeResolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
@@ -50,6 +52,8 @@ import org.springframework.util.ReflectionUtils;
*/
public final class FunctionTypeUtils {
private static Log logger = LogFactory.getLog(FunctionTypeUtils.class);
private FunctionTypeUtils() {
}
@@ -86,6 +90,10 @@ public final class FunctionTypeUtils {
return type;
}
public static Class<?> getRawType(Type type) {
return type != null ? TypeResolver.resolveRawClass(type, null) : null;
}
/**
* Will attempt to discover functional methods on the class. It's applicable for POJOs as well as
* functional classes in `java.util.function` package. For the later the names of the methods are
@@ -188,41 +196,52 @@ public final class FunctionTypeUtils {
return outputCount;
}
/**
* Returns input type of function type that represents Function or Consumer.
* @param functionType the Type of Function or Consumer
* @return the input type as {@link Type}
*/
@SuppressWarnings("unchecked")
public static Type getInputType(Type functionType) {
if (isSupplier(functionType)) {
logger.debug("Supplier does not have input type, returning null as input type.");
return null;
}
assertSupportedTypes(functionType);
Type inputType;
if (functionType instanceof Class) {
Class<?> functionClass = (Class<?>) functionType;
if (Function.class.isAssignableFrom(functionClass)) {
functionType = TypeResolver.reify(Function.class, (Class<Function<?, ?>>) functionClass);
}
else if (Consumer.class.isAssignableFrom(functionClass)) {
functionType = TypeResolver.reify(Consumer.class, (Class<Consumer<?>>) functionClass);
}
else {
return null;
}
functionType = Function.class.isAssignableFrom((Class<?>) functionType)
? TypeResolver.reify(Function.class, (Class<Function<?, ?>>) functionType)
: TypeResolver.reify(Consumer.class, (Class<Consumer<?>>) functionType);
}
Type inputType = Object.class;
if ((isFunction(functionType) || isConsumer(functionType)) && functionType instanceof ParameterizedType) {
inputType = ((ParameterizedType) functionType).getActualTypeArguments()[0];
}
inputType = functionType instanceof ParameterizedType
? ((ParameterizedType) functionType).getActualTypeArguments()[0]
: Object.class;
return inputType;
}
public static Type getOutputType(Type functionType, int index) {
@SuppressWarnings("unchecked")
public static Type getOutputType(Type functionType) {
assertSupportedTypes(functionType);
if (isFunction(functionType)) {
return functionType instanceof ParameterizedType ? ((ParameterizedType) functionType).getActualTypeArguments()[1] : Object.class;
}
else if (isSupplier(functionType)) {
return functionType instanceof ParameterizedType ? ((ParameterizedType) functionType).getActualTypeArguments()[0] : Object.class;
}
else {
if (isConsumer(functionType)) {
logger.debug("Consumer does not have output type, returning null as output type.");
return null;
}
Type inputType;
if (functionType instanceof Class) {
functionType = Function.class.isAssignableFrom((Class<?>) functionType)
? TypeResolver.reify(Function.class, (Class<Function<?, ?>>) functionType)
: TypeResolver.reify(Function.class, (Class<Supplier<?>>) functionType);
}
inputType = functionType instanceof ParameterizedType
? (isSupplier(functionType) ? ((ParameterizedType) functionType).getActualTypeArguments()[0] : ((ParameterizedType) functionType).getActualTypeArguments()[1])
: Object.class;
return inputType;
}
public static Type getImmediateGenericType(Type type, int index) {
@@ -238,8 +257,6 @@ public final class FunctionTypeUtils {
public static boolean isFlux(Type type) {
return TypeResolver.resolveRawClass(type, null) == Flux.class;
// type = extractReactiveType(type);
// return type.getTypeName().startsWith("reactor.core.publisher.Flux");
}
public static boolean isMessage(Type type) {
@@ -252,23 +269,13 @@ public final class FunctionTypeUtils {
return TypeResolver.resolveRawClass(type, null) == Message.class;
}
/**
* Determines if input argument to a Function is an array.
* @param functionType the function type
* @return true if input type is an array, otherwise false
*/
public static boolean isInputArray(Type functionType) {
Type inputType = FunctionTypeUtils.getInputType(functionType);
return inputType instanceof GenericArrayType || inputType instanceof Class && ((Class<?>) inputType).isArray();
}
/**
* Determines if input argument to a Function is an array.
* @param functionType the function type
* @return true if input type is an array, otherwise false
*/
public static boolean isOutputArray(Type functionType) {
Type outputType = FunctionTypeUtils.getOutputType(functionType, 0);
Type outputType = FunctionTypeUtils.getOutputType(functionType);
return outputType instanceof GenericArrayType || outputType instanceof Class && ((Class<?>) outputType).isArray();
}
@@ -296,7 +303,7 @@ public final class FunctionTypeUtils {
public static boolean isMono(Type type) {
type = extractReactiveType(type);
return type.getTypeName().startsWith("reactor.core.publisher.Mono");
return type == null ? false : type.getTypeName().startsWith("reactor.core.publisher.Mono");
}
private static boolean isFunctional(Type type) {

View File

@@ -250,7 +250,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
private FunctionInvocationWrapper invocationWrapperInstance(String functionDefinition, Object target, Type functionType) {
return invocationWrapperInstance(functionDefinition, target,
FunctionTypeUtils.isSupplier(functionType) ? null : FunctionTypeUtils.getInputType(functionType),
FunctionTypeUtils.getOutputType(functionType, 0));
FunctionTypeUtils.getOutputType(functionType));
}
/**
@@ -325,11 +325,11 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
}
public Class<?> getRawOutputType() {
return TypeResolver.resolveRawClass(this.outputType, null);
return this.outputType == null ? null : TypeResolver.resolveRawClass(this.outputType, null);
}
public Class<?> getRawInputType() {
return TypeResolver.resolveRawClass(this.inputType, null);
return this.inputType == null ? null : TypeResolver.resolveRawClass(this.inputType, null);
}
/**