Skip to content

Commit b069780

Browse files
chengchenartembilan
authored andcommitted
GH-2908: Publisher: synthesize anns for @AliasFor
Fixes #2908 * make sure the attribute is taken into account in @Header annotation * remove useless spaces according to checkstyle rules * make sure the expression attribute is taken into account as well in @payload annotation
1 parent 36c33bb commit b069780

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

spring-integration-core/src/main/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSource.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @author Artem Bilan
4545
* @author Gareth Chapman
4646
* @author Cameron Mayfield
47+
* @author Chengchen Ji
4748
*
4849
* @since 2.0
4950
*/
@@ -114,7 +115,7 @@ public Expression getExpressionForPayload(Method method) {
114115
"@Payload can be used at most once on a @Publisher method, " +
115116
"either at method-level or on a single parameter");
116117

117-
Assert.state("".equals(AnnotationUtils.getValue(currentAnnotation)),
118+
Assert.state("".equals(AnnotationUtils.getValue(AnnotationUtils.synthesizeAnnotation(currentAnnotation, null))),
118119
"@Payload on a parameter for a @Publisher method may not contain an expression");
119120

120121
payloadExpression =
@@ -204,8 +205,10 @@ private <T> T getAnnotationValue(Class<?> clazz, String attributeName, Class<T>
204205
@SuppressWarnings("unchecked")
205206
private <T> T getAnnotationValue(Annotation annotation, String attributeName, Class<T> expectedType) {
206207
T value = null;
207-
Object valueAsObject = (attributeName == null) ? AnnotationUtils.getValue(annotation)
208-
: AnnotationUtils.getValue(annotation, attributeName);
208+
Object valueAsObject = (attributeName == null) ?
209+
AnnotationUtils.getValue(AnnotationUtils.synthesizeAnnotation(annotation, null)) :
210+
AnnotationUtils.getValue(annotation, attributeName);
211+
209212
if (valueAsObject != null) {
210213
if (expectedType.isAssignableFrom(valueAsObject.getClass())) {
211214
value = (T) valueAsObject;

spring-integration-core/src/test/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSourceTests.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Mark Fisher
3838
* @author Artem Bilan
3939
* @author Cameron Mayfield
40+
* @author Chengchen Ji
4041
*
4142
* @since 2.0
4243
*/
@@ -116,6 +117,18 @@ public void voidReturnAndParameterPayloadAnnotation() {
116117
assertThat(payloadExpression).isEqualTo("#args[0]");
117118
}
118119

120+
@Test(expected = IllegalStateException.class)
121+
public void voidReturnAndParameterPayloadAnnotationWithExpression() {
122+
Method method = getMethod("methodWithVoidReturnAndParameterPayloadAnnotationWithExpression", String.class);
123+
source.getExpressionForPayload(method).getExpressionString();
124+
}
125+
126+
@Test(expected = IllegalStateException.class)
127+
public void voidReturnAndParameterPayloadAnnotationWithValue() {
128+
Method method = getMethod("methodWithVoidReturnAndParameterPayloadAnnotationWithValue", String.class);
129+
source.getExpressionForPayload(method).getExpressionString();
130+
}
131+
119132
@Test(expected = IllegalArgumentException.class)
120133
public void voidReturnAndNoPayloadAnnotation() {
121134
Method method = getMethod("methodWithVoidReturnAndNoPayloadAnnotation", String.class);
@@ -182,13 +195,21 @@ public void methodWithVoidReturnAndReturnValueAsPayload() {
182195

183196
@Publisher
184197
@Payload("testExpression2")
185-
public void methodWithHeaderAnnotations(String arg1, @Header("foo") String h1, @Header("bar") String h2) {
198+
public void methodWithHeaderAnnotations(String arg1, @Header("foo") String h1, @Header(name = "bar") String h2) {
186199
}
187200

188201
@Publisher
189202
public void methodWithVoidReturnAndParameterPayloadAnnotation(@Payload String payload) {
190203
}
191204

205+
@Publisher
206+
public void methodWithVoidReturnAndParameterPayloadAnnotationWithExpression(@Payload(expression = "foo") String payload) {
207+
}
208+
209+
@Publisher
210+
public void methodWithVoidReturnAndParameterPayloadAnnotationWithValue(@Payload("foo") String payload) {
211+
}
212+
192213
@Publisher
193214
public void methodWithVoidReturnAndNoPayloadAnnotation(String payload) {
194215
}

0 commit comments

Comments
 (0)