From f6c36d049da5bdefd7cf7f31b5c4624a9eb995bf Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 4 Nov 2014 11:33:56 -0500 Subject: [PATCH] INT-3548 Fix @Payloads Annotation JIRA: https://jira.spring.io/browse/INT-3548 Refactoring in 4.1 broke the `@Payloads` annotation in the `MessagingMethodInvokerHelper`. The test case missed this because it explicitly declares `@Payloads List` which is actually handled by the case below: ````java else if (Collection.class.isAssignableFrom(parameterType) || parameterType.isArray()) { if (canProcessMessageList) { sb.append("messages.![payload]"); ... ```` `List` triggered ````java else if ((parameterTypeDescriptor.isAssignableTo(messageListTypeDescriptor) || parameterTypeDescriptor .isAssignableTo(messageArrayTypeDescriptor))) { sb.append("messages"); ... ```` - Add back the logic to detect `@Payloads`. - Remove `@Payloads` from the existing test. - Add a new test that uses `List` --- .../GatewayMethodInboundMessageMapper.java | 6 ++-- .../util/MessagingAnnotationUtils.java | 9 +++-- .../util/MessagingMethodInvokerHelper.java | 2 +- ...hodInvokingMessageGroupProcessorTests.java | 34 +++++++++++++++++-- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayMethodInboundMessageMapper.java b/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayMethodInboundMessageMapper.java index 244503d79f..9634ebc9eb 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayMethodInboundMessageMapper.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayMethodInboundMessageMapper.java @@ -139,6 +139,7 @@ class GatewayMethodInboundMessageMapper implements InboundMessageMapper toMessage(Object[] arguments) { + @Override + public Message toMessage(Object[] arguments) { Assert.notNull(arguments, "cannot map null arguments to Message"); if (arguments.length != this.parameterList.size()) { String prefix = (arguments.length < this.parameterList.size()) ? "Not enough" : "Too many"; @@ -275,7 +277,7 @@ class GatewayMethodInboundMessageMapper implements InboundMessageMapper extends AbstractExpressionEvaluator TypeDescriptor parameterTypeDescriptor = new TypeDescriptor(methodParameter); Class parameterType = parameterTypeDescriptor.getObjectType(); Annotation mappingAnnotation = - MessagingAnnotationUtils.findMessagePartAnnotation(parameterAnnotations[i]); + MessagingAnnotationUtils.findMessagePartAnnotation(parameterAnnotations[i], true); if (mappingAnnotation != null) { Class annotationType = mappingAnnotation.annotationType(); if (annotationType.equals(org.springframework.integration.annotation.Payload.class) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/aggregator/MethodInvokingMessageGroupProcessorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/aggregator/MethodInvokingMessageGroupProcessorTests.java index ab7a731d10..1961b3aecc 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/aggregator/MethodInvokingMessageGroupProcessorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/aggregator/MethodInvokingMessageGroupProcessorTests.java @@ -56,7 +56,7 @@ import org.springframework.messaging.support.GenericMessage; @RunWith(MockitoJUnitRunner.class) public class MethodInvokingMessageGroupProcessorTests { - private List> messagesUpForProcessing = new ArrayList>(3); + private final List> messagesUpForProcessing = new ArrayList>(3); @Mock private MessageGroup messageGroupMock; @@ -137,11 +137,37 @@ public class MethodInvokingMessageGroupProcessorTests { } @Test - public void shouldFindAnnotatedPayloads() throws Exception { + public void shouldFindListPayloads() throws Exception { @SuppressWarnings("unused") class SimpleAggregator { - public String and(@Payloads List flags, @Header("foo") List header) { + public String and(List flags, @Header("foo") List header) { + List result = new ArrayList(); + for (int flag : flags) { + result.add(flag); + } + for (int flag : header) { + result.add(flag); + } + return result.toString(); + } + } + + MessageGroupProcessor processor = new MethodInvokingMessageGroupProcessor(new SimpleAggregator()); + messagesUpForProcessing.add(MessageBuilder.withPayload(3).setHeader("foo", Arrays.asList(101, 102)).build()); + when(messageGroupMock.getMessages()).thenReturn(messagesUpForProcessing); + Object result = processor.processMessageGroup(messageGroupMock); + assertThat((String) ((Message) result).getPayload(), is("[1, 2, 4, 3, 101, 102]")); + } + + @Test + public void shouldFindAnnotatedPayloadsWithNoType() throws Exception { + + @SuppressWarnings("unused") + class SimpleAggregator { + public String and(@Payloads List rawFlags, @Header("foo") List header) { + @SuppressWarnings("unchecked") + List flags = (List) rawFlags; List result = new ArrayList(); for (int flag : flags) { result.add(flag); @@ -242,6 +268,7 @@ public class MethodInvokingMessageGroupProcessorTests { MethodInvokingMessageGroupProcessor processor = new MethodInvokingMessageGroupProcessor(new SimpleAggregator()); GenericConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new Converter, Iterator>() { + @Override public Iterator convert(ArrayList source) { return source.iterator(); } @@ -523,6 +550,7 @@ public class MethodInvokingMessageGroupProcessorTests { this.greeting = greeting; } + @Override @Aggregator public String sayHello(List names) { return greeting + " " + names.get(0);