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
This commit is contained in:
Brian Clozel 2022-06-13 18:32:50 +02:00
parent 15b69a3ede
commit bb952cb95e
3 changed files with 20 additions and 0 deletions

View File

@ -60,6 +60,7 @@ dependencies {
optional("io.smallrye.reactive:mutiny")
optional("io.netty:netty-buffer")
testImplementation("jakarta.annotation:jakarta.annotation-api")
testImplementation("jakarta.inject:jakarta.inject-api")
testImplementation("jakarta.xml.bind:jakarta.xml.bind-api")
testImplementation("com.google.code.findbugs:jsr305")
testImplementation("com.fasterxml.woodstox:woodstox-core")

View File

@ -23,6 +23,7 @@ import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.Order;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
/**
* {@link RuntimeHintsRegistrar} for core annotations.
@ -36,6 +37,10 @@ class CoreAnnotationsRuntimeHintsRegistrar implements RuntimeHintsRegistrar {
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
Stream.of(AliasFor.class, Order.class).forEach(annotationType ->
RuntimeHintsUtils.registerAnnotation(hints, annotationType));
if (ClassUtils.isPresent("jakarta.inject.Inject", classLoader)) {
Stream.of("jakarta.inject.Inject", "jakarta.inject.Qualifier").forEach(annotationType ->
RuntimeHintsUtils.registerAnnotation(hints, ClassUtils.resolveClassName(annotationType, classLoader)));
}
}
}

View File

@ -16,6 +16,8 @@
package org.springframework.aot.hint.support;
import jakarta.inject.Inject;
import jakarta.inject.Qualifier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -59,4 +61,16 @@ class CoreAnnotationsRuntimeHintsRegistrarTests {
.withMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.hints);
}
@Test
void jakartaInjectAnnotationHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(Inject.class)
.withMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.hints);
}
@Test
void jakartaQualifierAnnotationHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(Qualifier.class)
.withMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.hints);
}
}