Skip to content

Commit

Permalink
GH-1213 Fix regression related to removal of TypeTools
Browse files Browse the repository at this point in the history
Resolves #1213
  • Loading branch information
olegz committed Dec 9, 2024
1 parent cf9b0a3 commit 622cbda
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
Expand All @@ -27,6 +26,8 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleFunction;
Expand All @@ -42,6 +43,7 @@
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.JsonNode;
import kotlin.jvm.functions.Function0;
Expand Down Expand Up @@ -179,18 +181,28 @@ public static Class<?> getRawType(Type type) {
* @return functional method
*/
public static Method discoverFunctionalMethod(Class<?> pojoFunctionClass) {
if (Supplier.class.isAssignableFrom(pojoFunctionClass)) {
return Stream.of(ReflectionUtils.getAllDeclaredMethods(pojoFunctionClass)).filter(m -> !m.isSynthetic()
&& m.getName().equals("get")).findFirst().get();
}
else if (Consumer.class.isAssignableFrom(pojoFunctionClass) || BiConsumer.class.isAssignableFrom(pojoFunctionClass)) {
return Stream.of(ReflectionUtils.getAllDeclaredMethods(pojoFunctionClass)).filter(m -> !m.isSynthetic()
&& m.getName().equals("accept")).findFirst().get();
}
else if (Function.class.isAssignableFrom(pojoFunctionClass) || BiFunction.class.isAssignableFrom(pojoFunctionClass)) {
return Stream.of(ReflectionUtils.getAllDeclaredMethods(pojoFunctionClass)).filter(m -> !m.isSynthetic()
&& m.getName().equals("apply")).findFirst().get();
}

List<Method> methods = new ArrayList<>();
ReflectionUtils.doWithMethods(pojoFunctionClass, method -> {
if (method.getDeclaringClass() == pojoFunctionClass
&& ((method.getParameterCount() == 1))
|| (method.getParameterCount() == 2 && method.getReturnType() != null)
|| (method.getParameterCount() == 0 && method.getReturnType() != null)) {
if (method.getDeclaringClass() == pojoFunctionClass) {
methods.add(method);
}

}, method ->
!method.getDeclaringClass().isAssignableFrom(Object.class)
&& !Modifier.isStatic(method.getModifiers()) && !method.isSynthetic() && !method.isBridge() && !method.isVarArgs());
&& !method.isSynthetic() && !method.isBridge() && !method.isVarArgs());

if (methods.size() > 1) {
for (Method candidadteMethod : methods) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.cloud.function.context.catalog;


import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
Expand Down Expand Up @@ -56,6 +57,12 @@
@SuppressWarnings("unused")
public class FunctionTypeUtilsTests {

@Test
public void testDiscoverFunctionalMethod() throws Exception {
Method method = FunctionTypeUtils.discoverFunctionalMethod(SampleEventConsumer.class);
assertThat(method.getName()).isEqualTo("accept");
}

@Test
public void testFunctionTypeFrom() throws Exception {
Type type = FunctionTypeUtils.discoverFunctionTypeFromClass(SimpleConsumer.class);
Expand Down Expand Up @@ -292,4 +299,29 @@ public Flux<Integer> apply(Flux<String> inFlux) {
return inFlux.map(v -> Integer.parseInt(v));
}
}

public static abstract class AbstractConsumer<C> implements Consumer<Message<C>> {

@Override
public final void accept(Message<C> message) {
if (message == null) {
return;
}

doAccept(message.getPayload());
}

protected abstract void doAccept(C payload);
}

public static class SampleEventConsumer extends AbstractConsumer<SampleData> {
@Override
protected void doAccept(SampleData data) {
}
}

public static class SampleData {

}

}

0 comments on commit 622cbda

Please sign in to comment.