Skip to content

Commit

Permalink
Add missing reflection hints on Jakarta types
Browse files Browse the repository at this point in the history
This commit adds reflection hints for `jakarta.inject.Provider` and
ensures that hints are always contributed even if jakarta classes are
not on the classpath.

Fixes gh-31259
  • Loading branch information
bclozel committed Sep 19, 2023
1 parent e53c2c6 commit 8074f93
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;

/**
* {@link RuntimeHintsRegistrar} for Jakarta annotations.
* <p>Hints are only registered if Jakarta inject is on the classpath.
*
* @author Brian Clozel
*/
class JakartaAnnotationsRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
if (ClassUtils.isPresent("jakarta.inject.Inject", classLoader)) {
Stream.of("jakarta.inject.Inject", "jakarta.inject.Qualifier").forEach(annotationType ->
hints.reflection().registerType(ClassUtils.resolveClassName(annotationType, classLoader)));
}
Stream.of("jakarta.inject.Inject", "jakarta.inject.Provider", "jakarta.inject.Qualifier").forEach(typeName ->
hints.reflection().registerType(TypeReference.of(typeName)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


import jakarta.inject.Inject;
import jakarta.inject.Provider;
import jakarta.inject.Qualifier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -51,6 +52,11 @@ void jakartaInjectAnnotationHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(Inject.class)).accepts(this.hints);
}

@Test
void jakartaProviderAnnotationHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(Provider.class)).accepts(this.hints);
}

@Test
void jakartaQualifierAnnotationHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(Qualifier.class)).accepts(this.hints);
Expand Down

0 comments on commit 8074f93

Please sign in to comment.