Skip to content

Fix spring-integration sample #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.cloud.function.context.FunctionCatalog;
Expand Down Expand Up @@ -188,6 +189,10 @@ private Type discoverFunctionType(Object function, String... names) {
boolean beanDefinitionExists = false;
for (int i = 0; i < names.length && !beanDefinitionExists; i++) {
beanDefinitionExists = this.applicationContext.getBeanFactory().containsBeanDefinition(names[i]);
if (this.applicationContext.containsBean("&" + names[i])) {
Class<?> objectType = this.applicationContext.getBean("&" + names[i], FactoryBean.class).getObjectType();
return FunctionTypeUtils.discoverFunctionTypeFromClass(objectType);
}
}
if (!beanDefinitionExists) {
logger.info("BeanDefinition for function name(s) '" + Arrays.asList(names) +
Expand Down Expand Up @@ -430,7 +435,9 @@ public class FunctionInvocationWrapper implements Function<Object, Object>, Cons

FunctionInvocationWrapper(Object target, Type functionType, String functionDefinition, String... acceptedOutputMimeTypes) {
this.target = target;
this.composed = !target.getClass().getName().contains("EnhancerBySpringCGLIB") && target.getClass().getDeclaredFields().length > 1;
this.composed = !target.getClass().getName().contains("$$EnhancerBySpringCGLIB")
&& !AopUtils.isAopProxy(target) && !AopUtils.isJdkDynamicProxy(target)
&& target.getClass().getDeclaredFields().length > 1;
this.functionType = functionType;
this.acceptedOutputMimeTypes = acceptedOutputMimeTypes;
this.functionDefinition = functionDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,34 @@ else if (Function.class.isAssignableFrom(pojoFunctionClass) || BiFunction.class.
return methods.get(0);
}

public static Type getFunctionTypeFromFunctionMethod(Method functionMethod) {
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;
}
else {
for (Type generic : generics) {
if (generic instanceof ParameterizedType) {
Class<?> rawClsss = (Class<?>) ((ParameterizedType) generic).getRawType();
if (rawClsss.isAssignableFrom(Function.class)
|| rawClsss.isAssignableFrom(Consumer.class)
|| rawClsss.isAssignableFrom(Supplier.class)) {
return generic;
}
else {
return discoverFunctionTypeFromClass(rawClsss);
}
}
else {
return discoverFunctionTypeFromClass((Class<?>) generic);
}
}
}
return null;
}

public static Type discoverFunctionTypeFromFunctionMethod(Method functionMethod) {
Assert.isTrue(
functionMethod.getName().equals("apply") ||
functionMethod.getName().equals("accept") ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


import java.lang.reflect.Type;
import java.sql.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
Expand All @@ -30,6 +31,9 @@
import reactor.util.function.Tuple2;
import reactor.util.function.Tuple3;

import org.springframework.cloud.function.context.FunctionType;
import org.springframework.messaging.Message;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -90,6 +94,26 @@ public void testOutputCount() throws Exception {
assertThat(outputCount).isEqualTo(2);
}

@Test
public void testFunctionTypeByClassDiscovery() {
FunctionType type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(Function.class));
assertThat(type.getInputType()).isAssignableFrom(Object.class);

type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(MessageFunction.class));
assertThat(type.getInputType()).isAssignableFrom(String.class);
assertThat(type.getOutputType()).isAssignableFrom(String.class);

type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(MyMessageFunction.class));
assertThat(type.getInputType()).isAssignableFrom(String.class);
assertThat(type.getOutputType()).isAssignableFrom(String.class);

type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(MessageConsumer.class));
assertThat(type.getInputType()).isAssignableFrom(String.class);

type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(MyMessageConsumer.class));
assertThat(type.getInputType()).isAssignableFrom(String.class);
}

// @Test
// public void testInputTypeByIndex() throws Exception {
// Type functionType = getReturnType("function");
Expand Down Expand Up @@ -163,4 +187,22 @@ private static Supplier<Tuple2<String, String>> multiOutputSupplier() {
private Type getReturnType(String methodName) throws Exception {
return FunctionTypeUtilsTests.class.getDeclaredMethod(methodName).getGenericReturnType();
}

//============

private interface MessageFunction<T> extends Function<Message<String>, Message<String>> {

}

private interface MyMessageFunction extends MessageFunction<Date> {

}

private interface MessageConsumer<T> extends Consumer<Message<String>> {

}

private interface MyMessageConsumer extends MessageConsumer<Date> {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private FunctionRegistration<?> discovereAndLoadFunctionFromClassName(String fun
ReflectionUtils.doWithMethods(functionClass, new MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
typeRef.set(FunctionTypeUtils.getFunctionTypeFromFunctionMethod(method));
typeRef.set(FunctionTypeUtils.discoverFunctionTypeFromFunctionMethod(method));
}
}, new MethodFilter() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
import org.springframework.integration.handler.LoggingHandler;
import org.springframework.messaging.Message;

//@SpringBootApplication
@SpringBootApplication
public class FunctionSampleSpringIntegrationApplication {

public static void main(String[] args) {
SpringApplication.run(FunctionSampleSpringIntegrationApplication.class, args);
}

//@Bean
@Bean
public IntegrationFlow uppercaseFlow() {
return IntegrationFlows.from(MessageFunction.class, "uppercase")
.<String, String>transform(String::toUpperCase)
Expand All @@ -43,5 +43,4 @@ public IntegrationFlow uppercaseFlow() {
public interface MessageFunction extends Function<Message<String>, Message<String>> {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;

//@RunWith(SpringRunner.class)
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FunctionSampleSpringIntegrationApplicationTests {

@Autowired
private TestRestTemplate restTemplate;

//@Test
@Test
public void upperCase() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
Expand Down