|
| 1 | +/* |
| 2 | + * Copyright 2019-2021 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; |
| 18 | + |
| 19 | +import java.lang.reflect.Type; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.TreeSet; |
| 23 | + |
| 24 | +import org.apache.commons.logging.Log; |
| 25 | +import org.apache.commons.logging.LogFactory; |
| 26 | +import org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.BeanFactoryNativeConfigurationProcessor; |
| 27 | +import org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.NativeConfigurationRegistry; |
| 28 | +import org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.NativeConfigurationUtils; |
| 29 | +import org.springframework.aot.support.BeanFactoryProcessor; |
| 30 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 31 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 32 | +import org.springframework.context.annotation.Configuration; |
| 33 | +import org.springframework.core.annotation.MergedAnnotations; |
| 34 | +import org.springframework.nativex.hint.TypeAccess; |
| 35 | +import org.springframework.stereotype.Component; |
| 36 | +import org.springframework.stereotype.Indexed; |
| 37 | +import org.springframework.validation.annotation.Validated; |
| 38 | + |
| 39 | +/** |
| 40 | + * Register as much of the hierarchy of {@link Indexed} marked beans as is required. |
| 41 | + * |
| 42 | + * @author Andy Clement |
| 43 | + */ |
| 44 | +public class IndexedBeanHierarchyNativeConfigurationProcessor implements BeanFactoryNativeConfigurationProcessor { |
| 45 | + |
| 46 | + private static Log logger = LogFactory.getLog(IndexedBeanHierarchyNativeConfigurationProcessor.class); |
| 47 | + |
| 48 | + @Override |
| 49 | + public void process(ConfigurableListableBeanFactory beanFactory, NativeConfigurationRegistry registry) { |
| 50 | + new BeanFactoryProcessor(beanFactory).processBeansWithAnnotation(Indexed.class, |
| 51 | + (beanName, beanType) -> { |
| 52 | + MergedAnnotations mas = MergedAnnotations.from(beanType); |
| 53 | + if (mas.isPresent(ConfigurationProperties.class) && mas.isPresent(Validated.class)) { |
| 54 | + logger.debug("adding basic reflection configuration for @Validated @ConfigurationProperties bean "+beanType.getName()); |
| 55 | + registry.reflection().forType(beanType).withAccess(TypeAccess.DECLARED_CONSTRUCTORS,TypeAccess.DECLARED_METHODS,TypeAccess.DECLARED_CLASSES,TypeAccess.DECLARED_FIELDS); |
| 56 | + } else if (mas.isPresent(Component.class) && !mas.isPresent(Configuration.class)) { |
| 57 | + // TODO there is no doubt further optimizations here and certain types of component will need subsets of this behaviour |
| 58 | + // (which could be pushed into the related NativeConfigurationProcessors - for example web components typically only |
| 59 | + // need reflective access to the methods that host web related annotations in the hierarchy) |
| 60 | + walkTypeAndRegisterReflection(beanType, registry, new HashSet<>()); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + public void walkTypeAndRegisterReflection(Class<?> type, NativeConfigurationRegistry registry, Set<Class<?>> visited) { |
| 66 | + if (!visited.add(type)) { |
| 67 | + return; |
| 68 | + } |
| 69 | + registry.reflection().forType(type).withAccess(TypeAccess.DECLARED_METHODS); |
| 70 | + Set<Class<?>> collector = new TreeSet<>((c1,c2) -> c1.getName().compareTo(c2.getName())); |
| 71 | + Type genericSuperclass = type.getGenericSuperclass(); |
| 72 | + NativeConfigurationUtils.collectReferenceTypesUsed(genericSuperclass, collector); |
| 73 | + Type[] genericInterfaces = type.getGenericInterfaces(); |
| 74 | + for (Type genericInterface: genericInterfaces) { |
| 75 | + NativeConfigurationUtils.collectReferenceTypesUsed(genericInterface, collector); |
| 76 | + } |
| 77 | + for (Class<?> sigtype: collector) { |
| 78 | + walkTypeAndRegisterReflection(sigtype, registry, visited); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments