Browse Source

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
pull/28616/head
Brian Clozel 4 years ago
parent
commit
bb952cb95e
  1. 1
      spring-core/spring-core.gradle
  2. 5
      spring-core/src/main/java/org/springframework/aot/hint/support/CoreAnnotationsRuntimeHintsRegistrar.java
  3. 14
      spring-core/src/test/java/org/springframework/aot/hint/support/CoreAnnotationsRuntimeHintsRegistrarTests.java

1
spring-core/spring-core.gradle

@ -60,6 +60,7 @@ dependencies { @@ -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")

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

@ -23,6 +23,7 @@ import org.springframework.aot.hint.RuntimeHintsRegistrar; @@ -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 { @@ -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)));
}
}
}

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

@ -16,6 +16,8 @@ @@ -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 { @@ -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);
}
}

Loading…
Cancel
Save