-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
5 changed files
with
174 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...rg/springframework/test/context/bean/override/convention/TestBeanReflectiveProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...va/org/springframework/test/context/aot/samples/bean/override/GreetingServiceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.aot.samples.bean.override; | ||
|
||
import org.springframework.test.context.aot.samples.common.GreetingService; | ||
|
||
/** | ||
* @author Sam Brannen | ||
* @since 6.2 | ||
*/ | ||
public class GreetingServiceFactory { | ||
|
||
public static GreetingService createEnigmaGreetingService() { | ||
return () -> "enigma"; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...java/org/springframework/test/context/aot/samples/bean/override/TestBeanJupiterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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.aot.samples.bean.override; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import org.springframework.test.context.aot.samples.common.GreetingService; | ||
import org.springframework.test.context.bean.override.convention.TestBean; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Sam Brannen | ||
* @since 6.2 | ||
*/ | ||
@ExtendWith(SpringExtension.class) | ||
public class TestBeanJupiterTests { | ||
|
||
@TestBean(methodName = "org.springframework.test.context.aot.samples.bean.override.GreetingServiceFactory#createEnigmaGreetingService") | ||
GreetingService greetingService; | ||
|
||
@Test | ||
void test() { | ||
assertThat(greetingService.greeting()).isEqualTo("enigma"); | ||
} | ||
|
||
} |