|
17 | 17 | package org.springframework.test.context.bean.override;
|
18 | 18 |
|
19 | 19 | import java.lang.annotation.Annotation;
|
20 |
| -import java.lang.reflect.Constructor; |
21 | 20 | import java.lang.reflect.Field;
|
22 |
| -import java.lang.reflect.InvocationTargetException; |
23 | 21 | import java.util.Collections;
|
24 | 22 | import java.util.LinkedHashSet;
|
25 | 23 | import java.util.Set;
|
26 | 24 | import java.util.concurrent.atomic.AtomicBoolean;
|
27 | 25 |
|
28 |
| -import org.springframework.beans.factory.support.BeanDefinitionValidationException; |
| 26 | +import org.springframework.beans.BeanUtils; |
29 | 27 | import org.springframework.core.ResolvableType;
|
30 | 28 | import org.springframework.core.annotation.MergedAnnotation;
|
31 | 29 | import org.springframework.core.annotation.MergedAnnotations;
|
32 |
| -import org.springframework.lang.Nullable; |
33 | 30 | import org.springframework.util.Assert;
|
34 |
| -import org.springframework.util.ClassUtils; |
35 | 31 | import org.springframework.util.ReflectionUtils;
|
36 | 32 |
|
37 | 33 | import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.DIRECT;
|
|
41 | 37 | * on fields of a given class and creates {@link OverrideMetadata} accordingly.
|
42 | 38 | *
|
43 | 39 | * @author Simon Baslé
|
| 40 | + * @author Sam Brannen |
44 | 41 | * @since 6.2
|
45 | 42 | */
|
46 | 43 | class BeanOverrideParser {
|
@@ -102,41 +99,21 @@ private void parseField(Field field, Class<?> source) {
|
102 | 99 | .map(mergedAnnotation -> {
|
103 | 100 | MergedAnnotation<?> metaSource = mergedAnnotation.getMetaSource();
|
104 | 101 | Assert.notNull(metaSource, "@BeanOverride annotation must be meta-present");
|
105 |
| - return new AnnotationPair(metaSource.synthesize(), mergedAnnotation); |
| 102 | + return new AnnotationPair(metaSource.synthesize(), mergedAnnotation.synthesize()); |
106 | 103 | })
|
107 | 104 | .forEach(pair -> {
|
108 |
| - BeanOverride beanOverride = pair.mergedAnnotation().synthesize(); |
109 |
| - BeanOverrideProcessor processor = getProcessorInstance(beanOverride.value()); |
110 |
| - if (processor == null) { |
111 |
| - return; |
112 |
| - } |
113 |
| - ResolvableType typeToOverride = processor.getOrDeduceType(field, pair.annotation(), source); |
| 105 | + BeanOverrideProcessor processor = BeanUtils.instantiateClass(pair.beanOverride.value()); |
| 106 | + ResolvableType typeToOverride = processor.getOrDeduceType(field, pair.composedAnnotation, source); |
114 | 107 |
|
115 | 108 | Assert.state(overrideAnnotationFound.compareAndSet(false, true),
|
116 | 109 | () -> "Multiple @BeanOverride annotations found on field: " + field);
|
117 |
| - OverrideMetadata metadata = processor.createMetadata(field, pair.annotation(), typeToOverride); |
| 110 | + OverrideMetadata metadata = processor.createMetadata(field, pair.composedAnnotation, typeToOverride); |
118 | 111 | boolean isNewDefinition = this.parsedMetadata.add(metadata);
|
119 | 112 | Assert.state(isNewDefinition, () -> "Duplicate " + metadata.getBeanOverrideDescription() +
|
120 | 113 | " OverrideMetadata: " + metadata);
|
121 | 114 | });
|
122 | 115 | }
|
123 | 116 |
|
124 |
| - @Nullable |
125 |
| - private BeanOverrideProcessor getProcessorInstance(Class<? extends BeanOverrideProcessor> processorClass) { |
126 |
| - Constructor<? extends BeanOverrideProcessor> constructor = ClassUtils.getConstructorIfAvailable(processorClass); |
127 |
| - if (constructor != null) { |
128 |
| - try { |
129 |
| - ReflectionUtils.makeAccessible(constructor); |
130 |
| - return constructor.newInstance(); |
131 |
| - } |
132 |
| - catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) { |
133 |
| - throw new BeanDefinitionValidationException( |
134 |
| - "Failed to instantiate BeanOverrideProcessor of type " + processorClass.getName(), ex); |
135 |
| - } |
136 |
| - } |
137 |
| - return null; |
138 |
| - } |
139 |
| - |
140 |
| - private record AnnotationPair(Annotation annotation, MergedAnnotation<BeanOverride> mergedAnnotation) {} |
| 117 | + private record AnnotationPair(Annotation composedAnnotation, BeanOverride beanOverride) {} |
141 | 118 |
|
142 | 119 | }
|
0 commit comments