GH-1204 Polish FunctionTypeUtils to ensure works with native
This commit is contained in:
@@ -44,6 +44,8 @@ import java.util.function.ToIntFunction;
|
|||||||
import java.util.function.ToLongFunction;
|
import java.util.function.ToLongFunction;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import kotlin.jvm.functions.Function0;
|
||||||
|
import kotlin.jvm.functions.Function1;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
@@ -59,6 +61,7 @@ import org.springframework.cloud.function.context.config.FunctionContextUtils;
|
|||||||
import org.springframework.cloud.function.context.config.RoutingFunction;
|
import org.springframework.cloud.function.context.config.RoutingFunction;
|
||||||
import org.springframework.context.support.GenericApplicationContext;
|
import org.springframework.context.support.GenericApplicationContext;
|
||||||
import org.springframework.core.GenericTypeResolver;
|
import org.springframework.core.GenericTypeResolver;
|
||||||
|
import org.springframework.core.KotlinDetector;
|
||||||
import org.springframework.core.ResolvableType;
|
import org.springframework.core.ResolvableType;
|
||||||
import org.springframework.messaging.Message;
|
import org.springframework.messaging.Message;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
@@ -203,25 +206,40 @@ public final class FunctionTypeUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Type discoverFunctionTypeFromClass(Class<?> functionalClass) {
|
public static Type discoverFunctionTypeFromClass(Class<?> functionalClass) {
|
||||||
Type t = discoverFunctionTypeFromFunctionMethod(discoverFunctionalMethod(functionalClass));
|
if (KotlinDetector.isKotlinPresent()) {
|
||||||
if (t == null) {
|
if (Function1.class.isAssignableFrom(functionalClass)) {
|
||||||
ResolvableType resolvableFunctionType = ResolvableType.forClass(functionalClass);
|
ResolvableType kotlinType = ResolvableType.forClass(functionalClass).as(Function1.class);
|
||||||
List<ResolvableType> resolvedGenerics = new ArrayList<>();
|
return GenericTypeResolver.resolveType(kotlinType.getType(), functionalClass);
|
||||||
if (resolvableFunctionType.hasGenerics()) {
|
}
|
||||||
for (ResolvableType generic : resolvableFunctionType.getGenerics()) {
|
else if (Function0.class.isAssignableFrom(functionalClass)) {
|
||||||
if (generic.getType() instanceof TypeVariable) {
|
ResolvableType kotlinType = ResolvableType.forClass(functionalClass).as(Function0.class);
|
||||||
resolvedGenerics.add(ResolvableType.forClass(Object.class));
|
return GenericTypeResolver.resolveType(kotlinType.getType(), functionalClass);
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
resolvedGenerics.add(generic);
|
Type typeToReturn = null;
|
||||||
|
if (Function.class.isAssignableFrom(functionalClass)) {
|
||||||
|
for (Type superInterface : functionalClass.getGenericInterfaces()) {
|
||||||
|
if (superInterface != null && !superInterface.equals(Object.class)) {
|
||||||
|
if (superInterface.toString().contains("KStream") && ResolvableType.forType(superInterface).getGeneric(1).isArray()) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ResolvableType[] generics = resolvedGenerics.toArray(new ResolvableType[] {});
|
ResolvableType functionType = ResolvableType.forClass(functionalClass).as(Function.class);
|
||||||
|
typeToReturn = GenericTypeResolver.resolveType(functionType.getType(), functionalClass);
|
||||||
t = ResolvableType.forClassWithGenerics(functionalClass, generics).getType();
|
|
||||||
}
|
}
|
||||||
return t;
|
else if (Consumer.class.isAssignableFrom(functionalClass)) {
|
||||||
|
ResolvableType functionType = ResolvableType.forClass(functionalClass).as(Consumer.class);
|
||||||
|
typeToReturn = GenericTypeResolver.resolveType(functionType.getType(), functionalClass);
|
||||||
|
}
|
||||||
|
else if (Supplier.class.isAssignableFrom(functionalClass)) {
|
||||||
|
ResolvableType functionType = ResolvableType.forClass(functionalClass).as(Supplier.class);
|
||||||
|
typeToReturn = GenericTypeResolver.resolveType(functionType.getType(), functionalClass);
|
||||||
|
}
|
||||||
|
// else {
|
||||||
|
// typeToReturn = TypeResolver.reify(functionalClass);
|
||||||
|
// }
|
||||||
|
return typeToReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
|||||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import io.cloudevents.spring.messaging.CloudEventMessageConverter;
|
import io.cloudevents.spring.messaging.CloudEventMessageConverter;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
@@ -91,6 +93,7 @@ import org.springframework.util.StringUtils;
|
|||||||
@AutoConfigureAfter(name = {"org.springframework.cloud.function.deployer.FunctionDeployerConfiguration"})
|
@AutoConfigureAfter(name = {"org.springframework.cloud.function.deployer.FunctionDeployerConfiguration"})
|
||||||
public class ContextFunctionCatalogAutoConfiguration {
|
public class ContextFunctionCatalogAutoConfiguration {
|
||||||
|
|
||||||
|
private static Log logger = LogFactory.getLog(ContextFunctionCatalogAutoConfiguration.class);
|
||||||
/**
|
/**
|
||||||
* The name of the property to specify desired JSON mapper. Available values are `jackson' and 'gson'.
|
* The name of the property to specify desired JSON mapper. Available values are `jackson' and 'gson'.
|
||||||
*/
|
*/
|
||||||
@@ -246,7 +249,44 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||||
mapper.configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, true);
|
mapper.configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, true);
|
||||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("ObjectMapper configuration: " + getConfigDetails(mapper));
|
||||||
|
}
|
||||||
return new JacksonMapper(mapper);
|
return new JacksonMapper(mapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String getConfigDetails(ObjectMapper mapper) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.append("Modules:\n");
|
||||||
|
if (mapper.getRegisteredModuleIds().isEmpty()) {
|
||||||
|
sb.append("\t").append("-none-").append("\n");
|
||||||
|
}
|
||||||
|
for (Object m : mapper.getRegisteredModuleIds()) {
|
||||||
|
sb.append(" ").append(m).append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.append("\nSerialization Features:\n");
|
||||||
|
for (SerializationFeature f : SerializationFeature.values()) {
|
||||||
|
sb.append("\t").append(f).append(" -> ")
|
||||||
|
.append(mapper.getSerializationConfig().hasSerializationFeatures(f.getMask()));
|
||||||
|
if (f.enabledByDefault()) {
|
||||||
|
sb.append(" (enabled by default)");
|
||||||
|
}
|
||||||
|
sb.append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.append("\nDeserialization Features:\n");
|
||||||
|
for (DeserializationFeature f : DeserializationFeature.values()) {
|
||||||
|
sb.append("\t").append(f).append(" -> ")
|
||||||
|
.append(mapper.getDeserializationConfig().hasDeserializationFeatures(f.getMask()));
|
||||||
|
if (f.enabledByDefault()) {
|
||||||
|
sb.append(" (enabled by default)");
|
||||||
|
}
|
||||||
|
sb.append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user