Skip to content

Commit e7e60f7

Browse files
committed
Add native hints for core annotations
Add `CoreAnnotationsRuntimeHintsRegistrar` to provide native hints for `@Order` and `@AliasFor`. Closes gh-28442
1 parent 4cebd9d commit e7e60f7

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.core.annotation;
18+
19+
import java.util.function.Consumer;
20+
21+
import org.springframework.aot.hint.MemberCategory;
22+
import org.springframework.aot.hint.RuntimeHints;
23+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
24+
import org.springframework.aot.hint.TypeHint;
25+
26+
/**
27+
* {@link RuntimeHintsRegistrar} for core annotations.
28+
*
29+
* @author Phillip Webb
30+
* @since 6.0
31+
*/
32+
class CoreAnnotationsRuntimeHintsRegistrar implements RuntimeHintsRegistrar {
33+
34+
private static final Consumer<TypeHint.Builder> HINT = builder -> builder.withMembers(
35+
MemberCategory.INVOKE_DECLARED_METHODS);
36+
37+
@Override
38+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
39+
hints.reflection().registerType(AliasFor.class, HINT);
40+
hints.reflection().registerType(Order.class, HINT);
41+
}
42+
43+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.aot.hint.RuntimeHintsRegistrar=\
2+
org.springframework.core.annotation.CoreAnnotationsRuntimeHintsRegistrar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.core.annotation;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.aot.hint.MemberCategory;
23+
import org.springframework.aot.hint.RuntimeHints;
24+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
25+
import org.springframework.aot.hint.TypeReference;
26+
import org.springframework.core.io.support.SpringFactoriesLoader;
27+
import org.springframework.util.ClassUtils;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link CoreAnnotationsRuntimeHintsRegistrar}.
33+
*
34+
* @author Phillip Webb
35+
*/
36+
class CoreAnnotationsRuntimeHintsRegistrarTests {
37+
38+
private RuntimeHints hints;
39+
40+
@BeforeEach
41+
void setup() {
42+
this.hints = new RuntimeHints();
43+
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
44+
.load(RuntimeHintsRegistrar.class).forEach(registrar -> registrar
45+
.registerHints(this.hints, ClassUtils.getDefaultClassLoader()));
46+
}
47+
48+
@Test
49+
void aliasForHasHints() {
50+
assertThat(this.hints.reflection().getTypeHint(TypeReference.of(AliasFor.class)))
51+
.satisfies((hint) -> assertThat(hint.getMemberCategories())
52+
.containsExactly(MemberCategory.INVOKE_DECLARED_METHODS));
53+
}
54+
55+
@Test
56+
void orderAnnotationHasHints() {
57+
assertThat(this.hints.reflection().getTypeHint(TypeReference.of(Order.class)))
58+
.satisfies((hint) -> assertThat(hint.getMemberCategories())
59+
.containsExactly(MemberCategory.INVOKE_DECLARED_METHODS));
60+
}
61+
62+
}

0 commit comments

Comments
 (0)