Skip to content

Commit bad9677

Browse files
artembilangaryrussell
authored andcommitted
Add AnnotationMetadataAdapter
Since SF has deprecated `StandardAnnotationMetadata` ctors in favor of factory method, we can't extend it any more to adapt annotation attributed by the map generated from the XML attributes * Introduce an `AnnotationMetadataAdapter` to request a `getAnnotationAttributes()` implementation which is only a method used in the target `registerBeanDefinitions()` implementations * Use a new `AnnotationMetadataAdapter` whenever the `StandardAnnotationMetadata` has been used before
1 parent 2a16fc5 commit bad9677

File tree

5 files changed

+121
-24
lines changed

5 files changed

+121
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.config.annotation;
18+
19+
import java.util.Map;
20+
import java.util.Set;
21+
22+
import org.springframework.core.annotation.MergedAnnotations;
23+
import org.springframework.core.type.AnnotationMetadata;
24+
import org.springframework.core.type.MethodMetadata;
25+
26+
/**
27+
* An {@link AnnotationMetadata} implementation to expose a metadata
28+
* by the provided {@link Map} of attributes.
29+
*
30+
* @author Artem Bilan
31+
*
32+
* @since 5.2
33+
*/
34+
public abstract class AnnotationMetadataAdapter implements AnnotationMetadata {
35+
36+
private static final RuntimeException UNSUPPORTED_OPERATION =
37+
new UnsupportedOperationException("The class doesn't support this operation");
38+
39+
@Override
40+
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
41+
throw UNSUPPORTED_OPERATION;
42+
}
43+
44+
@Override
45+
public MergedAnnotations getAnnotations() {
46+
throw UNSUPPORTED_OPERATION;
47+
}
48+
49+
@Override
50+
public String getClassName() {
51+
throw UNSUPPORTED_OPERATION;
52+
}
53+
54+
@Override
55+
public boolean isInterface() {
56+
throw UNSUPPORTED_OPERATION;
57+
}
58+
59+
@Override
60+
public boolean isAnnotation() {
61+
throw UNSUPPORTED_OPERATION;
62+
}
63+
64+
@Override
65+
public boolean isAbstract() {
66+
throw UNSUPPORTED_OPERATION;
67+
}
68+
69+
@Override
70+
public boolean isFinal() {
71+
throw UNSUPPORTED_OPERATION;
72+
}
73+
74+
@Override
75+
public boolean isIndependent() {
76+
throw UNSUPPORTED_OPERATION;
77+
}
78+
79+
@Override
80+
public String getEnclosingClassName() {
81+
throw UNSUPPORTED_OPERATION;
82+
}
83+
84+
@Override
85+
public String getSuperClassName() {
86+
throw UNSUPPORTED_OPERATION;
87+
}
88+
89+
@Override
90+
public String[] getInterfaceNames() {
91+
throw UNSUPPORTED_OPERATION;
92+
}
93+
94+
@Override
95+
public String[] getMemberClassNames() {
96+
throw UNSUPPORTED_OPERATION;
97+
}
98+
99+
}

spring-integration-core/src/main/java/org/springframework/integration/config/xml/AnnotationConfigParser.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import org.springframework.beans.factory.config.BeanDefinition;
2525
import org.springframework.beans.factory.xml.BeanDefinitionParser;
2626
import org.springframework.beans.factory.xml.ParserContext;
27-
import org.springframework.core.type.StandardAnnotationMetadata;
2827
import org.springframework.integration.config.EnablePublisher;
2928
import org.springframework.integration.config.IntegrationRegistrar;
29+
import org.springframework.integration.config.annotation.AnnotationMetadataAdapter;
3030
import org.springframework.util.xml.DomUtils;
3131

3232
/**
@@ -41,31 +41,27 @@ public class AnnotationConfigParser implements BeanDefinitionParser {
4141

4242
@Override
4343
public BeanDefinition parse(final Element element, ParserContext parserContext) {
44-
new IntegrationRegistrar().registerBeanDefinitions(new ExtendedAnnotationMetadata(Object.class, element),
44+
new IntegrationRegistrar().registerBeanDefinitions(new ExtendedAnnotationMetadata(element),
4545
parserContext.getRegistry());
4646
return null;
4747
}
4848

49-
private static final class ExtendedAnnotationMetadata extends StandardAnnotationMetadata {
49+
private static final class ExtendedAnnotationMetadata extends AnnotationMetadataAdapter {
5050

5151
private final Element element;
5252

53-
ExtendedAnnotationMetadata(Class<?> introspectedClass, Element element) {
54-
super(introspectedClass);
53+
ExtendedAnnotationMetadata(Element element) {
5554
this.element = element;
5655
}
5756

5857
@Override
5958
public Map<String, Object> getAnnotationAttributes(String annotationType) {
6059
if (EnablePublisher.class.getName().equals(annotationType)) {
61-
Element enablePublisherElement =
62-
DomUtils.getChildElementByTagName(this.element, "enable-publisher");
60+
Element enablePublisherElement = DomUtils.getChildElementByTagName(this.element, "enable-publisher");
6361
if (enablePublisherElement != null) {
6462
Map<String, Object> attributes = new HashMap<>();
65-
attributes.put("defaultChannel",
66-
enablePublisherElement.getAttribute("default-publisher-channel"));
67-
attributes.put("proxyTargetClass",
68-
enablePublisherElement.getAttribute("proxy-target-class"));
63+
attributes.put("defaultChannel", enablePublisherElement.getAttribute("default-publisher-channel"));
64+
attributes.put("proxyTargetClass", enablePublisherElement.getAttribute("proxy-target-class"));
6965
attributes.put("order", enablePublisherElement.getAttribute("order"));
7066
return attributes;
7167
}

spring-integration-core/src/main/java/org/springframework/integration/config/xml/MessageHistoryParser.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import org.springframework.beans.factory.config.BeanDefinition;
2525
import org.springframework.beans.factory.xml.BeanDefinitionParser;
2626
import org.springframework.beans.factory.xml.ParserContext;
27-
import org.springframework.core.type.StandardAnnotationMetadata;
2827
import org.springframework.integration.config.MessageHistoryRegistrar;
28+
import org.springframework.integration.config.annotation.AnnotationMetadataAdapter;
2929

3030
/**
3131
* The {@code <message-history/>} parser.
@@ -43,14 +43,16 @@ public class MessageHistoryParser implements BeanDefinitionParser {
4343

4444
@Override
4545
public BeanDefinition parse(final Element element, ParserContext parserContext) {
46-
this.messageHistoryRegistrar.registerBeanDefinitions(new StandardAnnotationMetadata(MessageHistoryParser.class) {
46+
this.messageHistoryRegistrar.registerBeanDefinitions(
47+
new AnnotationMetadataAdapter() {
4748

48-
@Override
49-
public Map<String, Object> getAnnotationAttributes(String annotationType) {
50-
return Collections.<String, Object>singletonMap("value", element.getAttribute("tracked-components"));
51-
}
49+
@Override
50+
public Map<String, Object> getAnnotationAttributes(String annotationType) {
51+
return Collections.singletonMap("value", element.getAttribute("tracked-components"));
52+
}
5253

53-
}, parserContext.getRegistry());
54+
}, parserContext.getRegistry());
5455
return null;
5556
}
57+
5658
}

spring-integration-core/src/test/java/org/springframework/integration/enablecomponentscan/EnableComponentScanTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.context.annotation.Configuration;
3131
import org.springframework.context.annotation.Import;
3232
import org.springframework.core.type.AnnotationMetadata;
33-
import org.springframework.core.type.StandardAnnotationMetadata;
3433
import org.springframework.integration.annotation.IntegrationComponentScan;
3534
import org.springframework.integration.config.EnableIntegration;
3635
import org.springframework.integration.config.IntegrationComponentScanRegistrar;
@@ -42,6 +41,7 @@
4241
/**
4342
* @author Artem Bilan
4443
* @author Gary Russell
44+
*
4545
* @since 4.0
4646
*/
4747
@RunWith(SpringRunner.class)
@@ -68,8 +68,8 @@ private static class CustomIntegrationComponentScanRegistrar extends Integration
6868
@Override
6969
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
7070
BeanDefinitionRegistry registry) {
71-
super.registerBeanDefinitions(new StandardAnnotationMetadata(
72-
IntegrationComponentScanConfiguration.class, true), registry);
71+
super.registerBeanDefinitions(
72+
AnnotationMetadata.introspect(IntegrationComponentScanConfiguration.class), registry);
7373
}
7474

7575
@Override

spring-integration-http/src/main/java/org/springframework/integration/http/config/IntegrationGraphControllerParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.springframework.beans.factory.config.BeanDefinition;
2525
import org.springframework.beans.factory.xml.BeanDefinitionParser;
2626
import org.springframework.beans.factory.xml.ParserContext;
27-
import org.springframework.core.type.StandardAnnotationMetadata;
27+
import org.springframework.integration.config.annotation.AnnotationMetadataAdapter;
2828

2929
/**
3030
* The {@link BeanDefinitionParser} for the {@code <int-http:graph-controller>} component.
@@ -41,11 +41,11 @@ public class IntegrationGraphControllerParser implements BeanDefinitionParser {
4141
public BeanDefinition parse(final Element element, ParserContext parserContext) {
4242
if (HttpContextUtils.WEB_MVC_PRESENT) {
4343
this.graphControllerRegistrar.registerBeanDefinitions(
44-
new StandardAnnotationMetadata(IntegrationGraphControllerParser.class) {
44+
new AnnotationMetadataAdapter() {
4545

4646
@Override
4747
public Map<String, Object> getAnnotationAttributes(String annotationType) {
48-
return Collections.<String, Object>singletonMap("value", element.getAttribute("path"));
48+
return Collections.singletonMap("value", element.getAttribute("path"));
4949
}
5050

5151
}, parserContext.getRegistry());

0 commit comments

Comments
 (0)