Skip to content

Commit bb952cb

Browse files
committed
Register native hints for jakarta.inject annotations
Prior to this commit, native images would dynamically check for the presence of `jakarta.inject.*` annotations and might fail at runtime or miss them entirely. This change ensures that if such classes are present during the AOT build, relevant runtime hints are registered so that these annotations can be read at runtime. Closes gh-28614
1 parent 15b69a3 commit bb952cb

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

spring-core/spring-core.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ dependencies {
6060
optional("io.smallrye.reactive:mutiny")
6161
optional("io.netty:netty-buffer")
6262
testImplementation("jakarta.annotation:jakarta.annotation-api")
63+
testImplementation("jakarta.inject:jakarta.inject-api")
6364
testImplementation("jakarta.xml.bind:jakarta.xml.bind-api")
6465
testImplementation("com.google.code.findbugs:jsr305")
6566
testImplementation("com.fasterxml.woodstox:woodstox-core")

spring-core/src/main/java/org/springframework/aot/hint/support/CoreAnnotationsRuntimeHintsRegistrar.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.core.annotation.AliasFor;
2424
import org.springframework.core.annotation.Order;
2525
import org.springframework.lang.Nullable;
26+
import org.springframework.util.ClassUtils;
2627

2728
/**
2829
* {@link RuntimeHintsRegistrar} for core annotations.
@@ -36,6 +37,10 @@ class CoreAnnotationsRuntimeHintsRegistrar implements RuntimeHintsRegistrar {
3637
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
3738
Stream.of(AliasFor.class, Order.class).forEach(annotationType ->
3839
RuntimeHintsUtils.registerAnnotation(hints, annotationType));
40+
if (ClassUtils.isPresent("jakarta.inject.Inject", classLoader)) {
41+
Stream.of("jakarta.inject.Inject", "jakarta.inject.Qualifier").forEach(annotationType ->
42+
RuntimeHintsUtils.registerAnnotation(hints, ClassUtils.resolveClassName(annotationType, classLoader)));
43+
}
3944
}
4045

4146
}

spring-core/src/test/java/org/springframework/aot/hint/support/CoreAnnotationsRuntimeHintsRegistrarTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.aot.hint.support;
1818

19+
import jakarta.inject.Inject;
20+
import jakarta.inject.Qualifier;
1921
import org.junit.jupiter.api.BeforeEach;
2022
import org.junit.jupiter.api.Test;
2123

@@ -59,4 +61,16 @@ void orderAnnotationHasHints() {
5961
.withMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.hints);
6062
}
6163

64+
@Test
65+
void jakartaInjectAnnotationHasHints() {
66+
assertThat(RuntimeHintsPredicates.reflection().onType(Inject.class)
67+
.withMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.hints);
68+
}
69+
70+
@Test
71+
void jakartaQualifierAnnotationHasHints() {
72+
assertThat(RuntimeHintsPredicates.reflection().onType(Qualifier.class)
73+
.withMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.hints);
74+
}
75+
6276
}

0 commit comments

Comments
 (0)