Skip to content

Commit 044f372

Browse files
committed
Introduce @RegisterReflectionForBinding
This annotation and the related processor allows to register reflection hints for data binding purpose (class, fields, properties, record components for the whole type hierarchy) Closes gh-28979
1 parent aaffb8b commit 044f372

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.aot.hint.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.core.annotation.AliasFor;
26+
27+
/**
28+
* Indicates that one or more {@link Class} reflection hints should be registered for
29+
* data binding purpose (class, fields, properties, record components for the whole
30+
* type hierarchy).
31+
*
32+
* <p>Typically used to annotate the bean class or bean method where the reflection hint
33+
* is needed.
34+
*
35+
* @author Sebastien Deleuze
36+
* @since 6.0
37+
*/
38+
@Target({ ElementType.TYPE, ElementType.METHOD })
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@Documented
41+
@Reflective(RegisterReflectionForBindingProcessor.class)
42+
public @interface RegisterReflectionForBinding {
43+
44+
/**
45+
* Classes for which reflection hints should be registered to enable data binding
46+
* (class, fields, properties, record components for the whole type hierarchy).
47+
* @see #classes()
48+
*/
49+
@AliasFor("classes")
50+
Class<?>[] value() default {};
51+
52+
/**
53+
* Classes for which reflection hints should be registered to enable data binding
54+
* (class, fields, properties, record components for the whole type hierarchy).
55+
* @see #value()
56+
*/
57+
@AliasFor("value")
58+
Class<?>[] classes() default {};
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.aot.hint.annotation;
18+
19+
import java.lang.reflect.AnnotatedElement;
20+
21+
import org.springframework.aot.hint.ReflectionHints;
22+
import org.springframework.core.annotation.AnnotationUtils;
23+
24+
/**
25+
* A {@link ReflectiveProcessor} implementation that registers reflection hints
26+
* for data binding purpose (class, constructors, fields, properties, record
27+
* components for the whole type hierarchy).
28+
*
29+
* @author Sebastien Deleuze
30+
* @since 6.0
31+
*/
32+
public class RegisterReflectionForBindingProcessor implements ReflectiveProcessor {
33+
34+
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
35+
36+
@Override
37+
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
38+
RegisterReflectionForBinding registerReflection = AnnotationUtils.getAnnotation(element, RegisterReflectionForBinding.class);
39+
if (registerReflection != null) {
40+
for (Class<?> type : registerReflection.classes()) {
41+
this.bindingRegistrar.registerReflectionHints(hints, type);
42+
}
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.aot.hint.annotation;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link RegisterReflectionForBindingProcessor}.
28+
*
29+
* @author Sebastien Deleuze
30+
*/
31+
public class RegisterReflectionForBindingProcessorTests {
32+
33+
private final RegisterReflectionForBindingProcessor processor = new RegisterReflectionForBindingProcessor();
34+
35+
private final RuntimeHints hints = new RuntimeHints();
36+
37+
@Test
38+
void registerReflectionForBindingOnClass() {
39+
processor.registerReflectionHints(hints.reflection(), ClassLevelAnnotatedBean.class);
40+
assertThat(RuntimeHintsPredicates.reflection().onType(SampleClassWithGetter.class)).accepts(hints);
41+
assertThat(RuntimeHintsPredicates.reflection().onType(String.class)).accepts(hints);
42+
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleClassWithGetter.class, "getName")).accepts(hints);
43+
}
44+
45+
@Test
46+
void registerReflectionForBindingOnMethod() throws NoSuchMethodException {
47+
processor.registerReflectionHints(hints.reflection(), MethodLevelAnnotatedBean.class.getMethod("method"));
48+
assertThat(RuntimeHintsPredicates.reflection().onType(SampleClassWithGetter.class)).accepts(hints);
49+
assertThat(RuntimeHintsPredicates.reflection().onType(String.class)).accepts(hints);
50+
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleClassWithGetter.class, "getName")).accepts(hints);
51+
}
52+
53+
@RegisterReflectionForBinding(SampleClassWithGetter.class)
54+
static class ClassLevelAnnotatedBean {
55+
}
56+
57+
static class MethodLevelAnnotatedBean {
58+
59+
@RegisterReflectionForBinding(SampleClassWithGetter.class)
60+
public void method() {
61+
}
62+
}
63+
64+
static class SampleClassWithGetter {
65+
66+
public String getName() {
67+
return null;
68+
}
69+
70+
public BindingReflectionHintsRegistrarTests.SampleEmptyClass unmanaged() {
71+
return null;
72+
}
73+
}
74+
75+
}

0 commit comments

Comments
 (0)