Register runtime hints for @⁠TestBean fully-qualified method names

This commit introduces a TestBeanReflectiveProcessor that registers
GraalVM native image reflection hints for a fully-qualified method name
configured via @⁠TestBean.

Closes gh-33836
This commit is contained in:
Sam Brannen
2024-11-01 16:03:56 +01:00
parent a8f5848a5d
commit a3b979c5ec
5 changed files with 174 additions and 14 deletions

View File

@@ -22,6 +22,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.aot.hint.annotation.Reflective;
import org.springframework.core.annotation.AliasFor;
import org.springframework.test.context.bean.override.BeanOverride;
@@ -115,6 +116,7 @@ import org.springframework.test.context.bean.override.BeanOverride;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@BeanOverride(TestBeanOverrideProcessor.class)
@Reflective(TestBeanReflectiveProcessor.class)
public @interface TestBean {
/**

View File

@@ -0,0 +1,57 @@
/*
* 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.convention;
import java.lang.reflect.AnnotatedElement;
import java.util.List;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.annotation.ReflectiveProcessor;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.util.Assert;
import static org.springframework.aot.hint.ExecutableMode.INVOKE;
/**
* {@link ReflectiveProcessor} that processes {@link TestBean @TestBean} annotations.
*
* @author Sam Brannen
* @since 6.2
*/
class TestBeanReflectiveProcessor implements ReflectiveProcessor {
@Override
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
MergedAnnotations.from(element)
.get(TestBean.class)
.synthesize(MergedAnnotation::isPresent)
.map(TestBean::methodName)
.filter(methodName -> methodName.contains("#"))
.ifPresent(methodName -> {
int indexOfHash = methodName.lastIndexOf('#');
String className = methodName.substring(0, indexOfHash).trim();
Assert.hasText(className, () -> "No class name present in fully-qualified method name: " + methodName);
String methodNameToUse = methodName.substring(indexOfHash + 1).trim();
Assert.hasText(methodNameToUse, () -> "No method name present in fully-qualified method name: " + methodName);
hints.registerType(TypeReference.of(className), builder ->
builder.withMethod(methodNameToUse, List.of(), INVOKE));
});
}
}