Browse Source
This commit exposes the logic of processing `@Reflective` on beans so that it can be reused in custom arrangements. Closes gh-28975pull/28987/head
6 changed files with 242 additions and 95 deletions
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
/* |
||||
* Copyright 2002-2022 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.context.aot; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
import org.springframework.aot.generate.GenerationContext; |
||||
import org.springframework.aot.hint.RuntimeHints; |
||||
import org.springframework.aot.hint.annotation.Reflective; |
||||
import org.springframework.aot.hint.annotation.ReflectiveProcessor; |
||||
import org.springframework.aot.hint.annotation.ReflectiveRuntimeHintsRegistrar; |
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; |
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; |
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode; |
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
||||
import org.springframework.beans.factory.support.RegisteredBean; |
||||
|
||||
/** |
||||
* AOT {@code BeanFactoryInitializationAotProcessor} that detects the presence |
||||
* of {@link Reflective @Reflective} on annotated elements of all registered |
||||
* beans and invokes the underlying {@link ReflectiveProcessor} implementations. |
||||
* |
||||
* @author Stephane Nicoll |
||||
* @author Sebastien Deleuze |
||||
*/ |
||||
class ReflectiveProcessorBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor { |
||||
|
||||
private static final ReflectiveRuntimeHintsRegistrar REGISTRAR = new ReflectiveRuntimeHintsRegistrar(); |
||||
|
||||
@Override |
||||
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) { |
||||
Class<?>[] beanTypes = Arrays.stream(beanFactory.getBeanDefinitionNames()) |
||||
.map(beanName -> RegisteredBean.of(beanFactory, beanName).getBeanClass()) |
||||
.toArray(Class<?>[]::new); |
||||
return new ReflectiveProcessorBeanFactoryInitializationAotContribution(beanTypes); |
||||
} |
||||
|
||||
private static class ReflectiveProcessorBeanFactoryInitializationAotContribution implements BeanFactoryInitializationAotContribution { |
||||
|
||||
private final Class<?>[] types; |
||||
|
||||
public ReflectiveProcessorBeanFactoryInitializationAotContribution(Class<?>[] types) { |
||||
this.types = types; |
||||
} |
||||
|
||||
@Override |
||||
public void applyTo(GenerationContext generationContext, BeanFactoryInitializationCode beanFactoryInitializationCode) { |
||||
RuntimeHints runtimeHints = generationContext.getRuntimeHints(); |
||||
REGISTRAR.registerRuntimeHints(runtimeHints, this.types); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -1,3 +1,5 @@
@@ -1,3 +1,5 @@
|
||||
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor= \ |
||||
org.springframework.context.aot.ReflectiveProcessorBeanFactoryInitializationAotProcessor |
||||
|
||||
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor= \ |
||||
org.springframework.context.aot.ReflectiveProcessorBeanRegistrationAotProcessor,\ |
||||
org.springframework.cache.annotation.CachingBeanRegistrationAotProcessor |
||||
|
||||
@ -0,0 +1,107 @@
@@ -0,0 +1,107 @@
|
||||
/* |
||||
* Copyright 2002-2022 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.context.aot; |
||||
|
||||
import java.lang.reflect.Constructor; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.aot.generate.GenerationContext; |
||||
import org.springframework.aot.hint.annotation.Reflective; |
||||
import org.springframework.aot.hint.predicate.ReflectionHintsPredicates; |
||||
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; |
||||
import org.springframework.beans.factory.aot.AotServices; |
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; |
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; |
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode; |
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
||||
import org.springframework.beans.factory.support.RootBeanDefinition; |
||||
import org.springframework.core.testfixture.aot.generate.TestGenerationContext; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link ReflectiveProcessorBeanFactoryInitializationAotProcessor}. |
||||
* |
||||
* @author Stephane Nicoll |
||||
* @author Sebastien Deleuze |
||||
*/ |
||||
class ReflectiveProcessorBeanFactoryInitializationAotProcessorTests { |
||||
|
||||
private final ReflectiveProcessorBeanFactoryInitializationAotProcessor processor = new ReflectiveProcessorBeanFactoryInitializationAotProcessor(); |
||||
|
||||
private final GenerationContext generationContext = new TestGenerationContext(); |
||||
|
||||
@Test |
||||
void processorIsRegistered() { |
||||
assertThat(AotServices.factories(getClass().getClassLoader()).load(BeanFactoryInitializationAotProcessor.class)) |
||||
.anyMatch(ReflectiveProcessorBeanFactoryInitializationAotProcessor.class::isInstance); |
||||
} |
||||
|
||||
@Test |
||||
void shouldProcessAnnotationOnType() { |
||||
process(SampleTypeAnnotatedBean.class); |
||||
assertThat(RuntimeHintsPredicates.reflection().onType(SampleTypeAnnotatedBean.class)) |
||||
.accepts(this.generationContext.getRuntimeHints()); |
||||
} |
||||
|
||||
@Test |
||||
void shouldProcessAllBeans() throws NoSuchMethodException { |
||||
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection(); |
||||
process(SampleTypeAnnotatedBean.class, SampleConstructorAnnotatedBean.class); |
||||
Constructor<?> constructor = SampleConstructorAnnotatedBean.class.getDeclaredConstructor(String.class); |
||||
assertThat(reflection.onType(SampleTypeAnnotatedBean.class).and(reflection.onConstructor(constructor))) |
||||
.accepts(this.generationContext.getRuntimeHints()); |
||||
} |
||||
|
||||
private void process(Class<?>... beanClasses) { |
||||
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
||||
for (Class<?> beanClass : beanClasses) { |
||||
beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass)); |
||||
} |
||||
BeanFactoryInitializationAotContribution contribution = this.processor.processAheadOfTime(beanFactory); |
||||
assertThat(contribution).isNotNull(); |
||||
contribution.applyTo(this.generationContext, mock(BeanFactoryInitializationCode.class)); |
||||
} |
||||
|
||||
@Reflective |
||||
@SuppressWarnings("unused") |
||||
static class SampleTypeAnnotatedBean { |
||||
|
||||
private String notManaged; |
||||
|
||||
public void notManaged() { |
||||
|
||||
} |
||||
} |
||||
|
||||
@SuppressWarnings("unused") |
||||
static class SampleConstructorAnnotatedBean { |
||||
|
||||
@Reflective |
||||
SampleConstructorAnnotatedBean(String name) { |
||||
|
||||
} |
||||
|
||||
SampleConstructorAnnotatedBean(Integer nameAsNumber) { |
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue