Skip to content

Commit

Permalink
Register runtime hints for BeanOverrideProcessors
Browse files Browse the repository at this point in the history
This commit introduces a BeanOverrideReflectiveProcessor which
registers runtime hints for any BeanOverrideProcessor configured
via @⁠BeanOverride.

See spring-projectsgh-32933
  • Loading branch information
sbrannen committed Sep 17, 2024
1 parent 102ec94 commit fb1caf5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package org.springframework.test.context.bean.override;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.aot.hint.annotation.Reflective;

/**
* Mark a composed annotation as eligible for Bean Override processing.
*
Expand All @@ -31,10 +34,13 @@
* expected that it has a {@link Target} of {@link ElementType#FIELD FIELD}.
*
* @author Simon Baslé
* @author Sam Brannen
* @since 6.2
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@Documented
@Reflective(BeanOverrideReflectiveProcessor.class)
public @interface BeanOverride {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.bean.override;

import java.lang.reflect.AnnotatedElement;

import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.annotation.ReflectiveProcessor;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;

import static org.springframework.aot.hint.MemberCategory.INVOKE_DECLARED_CONSTRUCTORS;

/**
* {@link ReflectiveProcessor} that processes {@link BeanOverride @BeanOverride}
* annotations.
*
* @author Sam Brannen
* @since 6.2
*/
class BeanOverrideReflectiveProcessor implements ReflectiveProcessor {

@Override
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
MergedAnnotations.from(element)
.get(BeanOverride.class)
.synthesize(MergedAnnotation::isPresent)
.map(BeanOverride::value)
.ifPresent(clazz -> hints.registerType(clazz, INVOKE_DECLARED_CONSTRUCTORS));
}

}

0 comments on commit fb1caf5

Please sign in to comment.