Skip to content

Commit 4368e25

Browse files
committed
Reinstate RuntimeHintsAgent tests
Closes gh-29062
1 parent 044f372 commit 4368e25

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.factory.annotation;
18+
19+
import java.util.stream.Stream;
20+
21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
23+
import org.springframework.util.ClassUtils;
24+
25+
/**
26+
* {@link RuntimeHintsRegistrar} for Jakarta annotations.
27+
* <p>Hints are only registered if Jakarta inject is on the classpath.
28+
*
29+
* @author Brian Clozel
30+
*/
31+
class JakartaAnnotationsRuntimeHints implements RuntimeHintsRegistrar {
32+
33+
@Override
34+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
35+
if (ClassUtils.isPresent("jakarta.inject.Inject", classLoader)) {
36+
Stream.of("jakarta.inject.Inject", "jakarta.inject.Qualifier").forEach(annotationType ->
37+
hints.reflection().registerType(ClassUtils.resolveClassName(annotationType, classLoader)));
38+
}
39+
}
40+
41+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
org.springframework.aot.hint.RuntimeHintsRegistrar=\
2-
org.springframework.beans.factory.annotation.BeanFactoryAnnotationsRuntimeHints
2+
org.springframework.beans.factory.annotation.BeanFactoryAnnotationsRuntimeHints,\
3+
org.springframework.beans.factory.annotation.JakartaAnnotationsRuntimeHints
34

45
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=\
56
org.springframework.beans.factory.aot.BeanRegistrationsAotProcessor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.factory.annotation;
18+
19+
20+
import jakarta.inject.Inject;
21+
import jakarta.inject.Qualifier;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.aot.hint.RuntimeHints;
26+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
27+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
28+
import org.springframework.beans.factory.aot.AotServices;
29+
import org.springframework.util.ClassUtils;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@link JakartaAnnotationsRuntimeHints}.
35+
*
36+
* @author Brian Clozel
37+
*/
38+
class JakartaAnnotationsRuntimeHintsTests {
39+
40+
private final RuntimeHints hints = new RuntimeHints();
41+
42+
@BeforeEach
43+
void setup() {
44+
AotServices.factories().load(RuntimeHintsRegistrar.class)
45+
.forEach(registrar -> registrar.registerHints(this.hints,
46+
ClassUtils.getDefaultClassLoader()));
47+
}
48+
49+
@Test
50+
void jakartaInjectAnnotationHasHints() {
51+
assertThat(RuntimeHintsPredicates.reflection().onType(Inject.class)).accepts(this.hints);
52+
}
53+
54+
@Test
55+
void jakartaQualifierAnnotationHasHints() {
56+
assertThat(RuntimeHintsPredicates.reflection().onType(Qualifier.class)).accepts(this.hints);
57+
}
58+
59+
}

spring-context/spring-context.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
plugins {
2+
id 'org.springframework.build.runtimehints-agent'
3+
}
4+
15
description = "Spring Context"
26

37
apply plugin: "kotlin"

0 commit comments

Comments
 (0)