Skip to content

Commit 6a2e234

Browse files
committed
Register runtime hints for BeanOverrideProcessors
This commit introduces a BeanOverrideReflectiveProcessor which registers runtime hints for any BeanOverrideProcessor configured via @⁠BeanOverride. See gh-32933
1 parent 5ddeb06 commit 6a2e234

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverride.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
2424

25+
import org.springframework.aot.hint.annotation.Reflective;
26+
2527
/**
2628
* Mark a composed annotation as eligible for Bean Override processing.
2729
*
@@ -37,11 +39,13 @@
3739
* {@link org.springframework.test.context.bean.override.mockito.MockitoSpyBean @MockitoSpyBean}.
3840
*
3941
* @author Simon Baslé
42+
* @author Sam Brannen
4043
* @since 6.2
4144
*/
4245
@Retention(RetentionPolicy.RUNTIME)
4346
@Target(ElementType.ANNOTATION_TYPE)
4447
@Documented
48+
@Reflective(BeanOverrideReflectiveProcessor.class)
4549
public @interface BeanOverride {
4650

4751
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-2024 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.test.context.bean.override;
18+
19+
import java.lang.reflect.AnnotatedElement;
20+
21+
import org.springframework.aot.hint.ReflectionHints;
22+
import org.springframework.aot.hint.annotation.ReflectiveProcessor;
23+
import org.springframework.core.annotation.MergedAnnotation;
24+
import org.springframework.core.annotation.MergedAnnotations;
25+
26+
import static org.springframework.aot.hint.MemberCategory.INVOKE_DECLARED_CONSTRUCTORS;
27+
28+
/**
29+
* {@link ReflectiveProcessor} that processes {@link BeanOverride @BeanOverride}
30+
* annotations.
31+
*
32+
* @author Sam Brannen
33+
* @since 6.2
34+
*/
35+
class BeanOverrideReflectiveProcessor implements ReflectiveProcessor {
36+
37+
@Override
38+
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
39+
MergedAnnotations.from(element)
40+
.get(BeanOverride.class)
41+
.synthesize(MergedAnnotation::isPresent)
42+
.map(BeanOverride::value)
43+
.ifPresent(clazz -> hints.registerType(clazz, INVOKE_DECLARED_CONSTRUCTORS));
44+
}
45+
46+
}

0 commit comments

Comments
 (0)