Skip to content

Commit f998855

Browse files
artembilanolegz
authored andcommitted
FunctionTypeUtils test for NPE
Related to https://stackoverflow.com/questions/72163534/spring-batch-integration-throwns-org-springframework-messaging-messagehandlingex The `FunctionTypeUtils.isMessage()` fails with NPE when target method has non-message argument with generic parameter. Even if we instantiate the class with specific generic argument, that info is not available for reflection and `MethodParameter` end up with a generic parameter name which is essentially a `TypeVariable` The stacktrace is like this: ``` java.lang.NullPointerException: Cannot invoke "java.lang.Class.getGenericInterfaces()" because "targetType" is null at net.jodah.typetools.TypeResolver.getTypeVariableMap(TypeResolver.java:494) at net.jodah.typetools.TypeResolver.resolveRawClass(TypeResolver.java:387) at net.jodah.typetools.TypeResolver.resolveRawClass(TypeResolver.java:373) at org.springframework.cloud.function.context.catalog.FunctionTypeUtils.isMessage(FunctionTypeUtils.java:416) ```
1 parent d55abe5 commit f998855

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/FunctionTypeUtilsTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
package org.springframework.cloud.function.context.catalog;
1818

1919

20+
import java.lang.reflect.Method;
2021
import java.lang.reflect.ParameterizedType;
2122
import java.lang.reflect.Type;
23+
import java.util.Date;
2224
import java.util.List;
2325
import java.util.Map;
26+
import java.util.concurrent.atomic.AtomicReference;
2427
import java.util.function.Consumer;
2528
import java.util.function.Function;
2629
import java.util.function.Supplier;
@@ -31,8 +34,10 @@
3134
import reactor.util.function.Tuple2;
3235
import reactor.util.function.Tuple3;
3336

37+
import org.springframework.core.MethodParameter;
3438
import org.springframework.core.ParameterizedTypeReference;
3539
import org.springframework.messaging.Message;
40+
import org.springframework.util.ReflectionUtils;
3641

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

@@ -42,7 +47,7 @@
4247
*
4348
*/
4449
@SuppressWarnings("unused")
45-
public class FunctionTypeUtilsTests {
50+
public class FunctionTypeUtilsTests<T> {
4651

4752
@Test
4853
public void testFunctionTypeFrom() throws Exception {
@@ -147,6 +152,21 @@ public void testIsTypeCollection() {
147152
assertThat(FunctionTypeUtils.isTypeCollection(new ParameterizedTypeReference<Flux<Message<List<String>>>>() { }.getType())).isFalse();
148153
}
149154

155+
@Test
156+
public void testNoNpeFromIsMessage() {
157+
FunctionTypeUtilsTests<Date> testService = new FunctionTypeUtilsTests<>();
158+
159+
Method methodUnderTest =
160+
ReflectionUtils.findMethod(testService.getClass(), "notAMessageMethod", AtomicReference.class);
161+
MethodParameter methodParameter = MethodParameter.forExecutable(methodUnderTest, 0);
162+
163+
assertThat(FunctionTypeUtils.isMessage(methodParameter.getGenericParameterType())).isFalse();
164+
}
165+
166+
void notAMessageMethod(AtomicReference<T> payload) {
167+
168+
}
169+
150170
private static Function<String, Integer> function() {
151171
return null;
152172
}

0 commit comments

Comments
 (0)