|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.context.aot; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.stream.StreamSupport; |
| 23 | + |
| 24 | +import org.springframework.aot.generate.GenerationContext; |
| 25 | +import org.springframework.aot.hint.RuntimeHints; |
| 26 | +import org.springframework.aot.hint.annotation.Reflective; |
| 27 | +import org.springframework.aot.hint.annotation.ReflectiveProcessor; |
| 28 | +import org.springframework.aot.hint.annotation.ReflectiveRuntimeHintsRegistrar; |
| 29 | +import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; |
| 30 | +import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; |
| 31 | +import org.springframework.beans.factory.aot.BeanFactoryInitializationCode; |
| 32 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 33 | +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; |
| 34 | +import org.springframework.lang.Nullable; |
| 35 | +import org.springframework.util.ClassUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * Helper class to create an AOT contribution that detects the presence of |
| 39 | + * {@link Reflective @Reflective} on annotated elements and invoke the underlying |
| 40 | + * {@link ReflectiveProcessor} implementations. |
| 41 | + * |
| 42 | + * @author Stephane Nicoll |
| 43 | + * @since 6.2 |
| 44 | + */ |
| 45 | +public abstract class ReflectiveProcessorAotContributionProvider { |
| 46 | + |
| 47 | + private static final ReflectiveRuntimeHintsRegistrar registrar = new ReflectiveRuntimeHintsRegistrar(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Create an AOT contribution from the given classes by checking the ones |
| 51 | + * that use {@link Reflective}. The returned contribution registers the |
| 52 | + * necessary reflection hints as a result. If no class amongst the given |
| 53 | + * classes use {@link Reflective}, or if the given iterable is empty, |
| 54 | + * returns {@code null}. |
| 55 | + * @param classes the classes to inspect |
| 56 | + * @return an AOT contribution for the classes that use {@link Reflective} |
| 57 | + * or {@code null} if they aren't any |
| 58 | + */ |
| 59 | + @Nullable |
| 60 | + public static BeanFactoryInitializationAotContribution from(Iterable<Class<?>> classes) { |
| 61 | + return from(StreamSupport.stream(classes.spliterator(), false).toArray(Class<?>[]::new)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Create an AOT contribution from the given classes by checking the ones |
| 66 | + * that use {@link Reflective}. The returned contribution registers the |
| 67 | + * necessary reflection hints as a result. If no class amongst the given |
| 68 | + * classes use {@link Reflective}, or if the given iterable is empty, |
| 69 | + * returns {@code null}. |
| 70 | + * @param classes the classes to inspect |
| 71 | + * @return an AOT contribution for the classes that use {@link Reflective} |
| 72 | + * or {@code null} if they aren't any |
| 73 | + */ |
| 74 | + @Nullable |
| 75 | + public static BeanFactoryInitializationAotContribution from(Class<?>[] classes) { |
| 76 | + Class<?>[] types = Arrays.stream(classes).filter(registrar::isCandidate).toArray(Class<?>[]::new); |
| 77 | + return (types.length > 0 ? new AotContribution(types) : null); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Scan the given {@code packageNames} and their sub-packages for classes |
| 82 | + * that uses {@link Reflective} and create an AOT contribution with the |
| 83 | + * result. If no candidates were found, return {@code null}. |
| 84 | + * <p>This performs a "deep scan" by loading every class in the specified |
| 85 | + * packages and search for {@link Reflective} on types, constructors, methods, |
| 86 | + * and fields. Enclosed classes are candidates as well. Classes that fail to |
| 87 | + * load are ignored. |
| 88 | + * @param classLoader the classloader to use |
| 89 | + * @param packageNames the package names to scan |
| 90 | + * @return an AOT contribution for the identified classes or {@code null} if |
| 91 | + * they aren't any |
| 92 | + */ |
| 93 | + @Nullable |
| 94 | + public static BeanFactoryInitializationAotContribution scan(@Nullable ClassLoader classLoader, String... packageNames) { |
| 95 | + ReflectiveClassPathScanner scanner = new ReflectiveClassPathScanner(classLoader); |
| 96 | + Class<?>[] types = scanner.scan(packageNames); |
| 97 | + return (types.length > 0 ? new AotContribution(types) : null); |
| 98 | + } |
| 99 | + |
| 100 | + private static class AotContribution implements BeanFactoryInitializationAotContribution { |
| 101 | + |
| 102 | + private final Class<?>[] classes; |
| 103 | + |
| 104 | + public AotContribution(Class<?>[] classes) { |
| 105 | + this.classes = classes; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void applyTo(GenerationContext generationContext, BeanFactoryInitializationCode beanFactoryInitializationCode) { |
| 110 | + RuntimeHints runtimeHints = generationContext.getRuntimeHints(); |
| 111 | + registrar.registerRuntimeHints(runtimeHints, this.classes); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + private static class ReflectiveClassPathScanner extends ClassPathScanningCandidateComponentProvider { |
| 117 | + |
| 118 | + @Nullable |
| 119 | + private final ClassLoader classLoader; |
| 120 | + |
| 121 | + ReflectiveClassPathScanner(@Nullable ClassLoader classLoader) { |
| 122 | + super(false); |
| 123 | + this.classLoader = classLoader; |
| 124 | + addIncludeFilter((metadataReader, metadataReaderFactory) -> true); |
| 125 | + } |
| 126 | + |
| 127 | + Class<?>[] scan(String... packageNames) { |
| 128 | + if (logger.isDebugEnabled()) { |
| 129 | + logger.debug("Scanning all types for reflective usage from " + Arrays.toString(packageNames)); |
| 130 | + } |
| 131 | + Set<BeanDefinition> candidates = new HashSet<>(); |
| 132 | + for (String packageName : packageNames) { |
| 133 | + candidates.addAll(findCandidateComponents(packageName)); |
| 134 | + } |
| 135 | + return candidates.stream().map(c -> (Class<?>) c.getAttribute("type")).toArray(Class<?>[]::new); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { |
| 140 | + String className = beanDefinition.getBeanClassName(); |
| 141 | + if (className != null) { |
| 142 | + try { |
| 143 | + Class<?> type = ClassUtils.forName(className, this.classLoader); |
| 144 | + beanDefinition.setAttribute("type", type); |
| 145 | + return registrar.isCandidate(type); |
| 146 | + } |
| 147 | + catch (Exception ex) { |
| 148 | + if (logger.isTraceEnabled()) { |
| 149 | + logger.trace("Ignoring '%s' for reflective usage: %s".formatted(className, ex.getMessage())); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + return false; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments