|
| 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.aot.hint.annotation; |
| 18 | + |
| 19 | +import java.lang.reflect.AnnotatedElement; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +import org.apache.commons.logging.Log; |
| 26 | +import org.apache.commons.logging.LogFactory; |
| 27 | + |
| 28 | +import org.springframework.aot.hint.MemberCategory; |
| 29 | +import org.springframework.aot.hint.ReflectionHints; |
| 30 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 31 | +import org.springframework.lang.Nullable; |
| 32 | +import org.springframework.util.Assert; |
| 33 | +import org.springframework.util.ClassUtils; |
| 34 | + |
| 35 | +/** |
| 36 | + * A {@link ReflectiveProcessor} implementation that pairs with |
| 37 | + * {@link RegisterReflection @RegisterReflection}. Can be used as a base |
| 38 | + * implementation for composed annotations that are meta-annotated with |
| 39 | + * {@link RegisterReflection}. |
| 40 | + * |
| 41 | + * @author Stephane Nicoll |
| 42 | + * @since 6.2 |
| 43 | + */ |
| 44 | +public class RegisterReflectionReflectiveProcessor implements ReflectiveProcessor { |
| 45 | + |
| 46 | + private static final Log logger = LogFactory.getLog(RegisterReflectionReflectiveProcessor.class); |
| 47 | + |
| 48 | + @Override |
| 49 | + public final void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) { |
| 50 | + RegisterReflection annotation = AnnotatedElementUtils.getMergedAnnotation( |
| 51 | + element, RegisterReflection.class); |
| 52 | + Assert.notNull(annotation, "Element must be annotated with @" + RegisterReflection.class.getSimpleName() |
| 53 | + + ": " + element); |
| 54 | + ReflectionRegistration registration = parse(element, annotation); |
| 55 | + registerReflectionHints(hints, registration); |
| 56 | + } |
| 57 | + |
| 58 | + protected ReflectionRegistration parse(AnnotatedElement element, RegisterReflection annotation) { |
| 59 | + List<Class<?>> allClassNames = new ArrayList<>(); |
| 60 | + allClassNames.addAll(Arrays.asList(annotation.classes())); |
| 61 | + allClassNames.addAll(Arrays.stream(annotation.classNames()) |
| 62 | + .map(this::loadClass).filter(Objects::nonNull).toList()); |
| 63 | + if (allClassNames.isEmpty()) { |
| 64 | + if (element instanceof Class<?> clazz) { |
| 65 | + allClassNames.add(clazz); |
| 66 | + } |
| 67 | + else { |
| 68 | + throw new IllegalStateException("At least one class must be specified, " |
| 69 | + + "could not detect target from '" + element + "'"); |
| 70 | + } |
| 71 | + } |
| 72 | + return new ReflectionRegistration(allClassNames.toArray(new Class<?>[0]), |
| 73 | + annotation.memberCategories()); |
| 74 | + } |
| 75 | + |
| 76 | + protected void registerReflectionHints(ReflectionHints hints, ReflectionRegistration registration) { |
| 77 | + for (Class<?> target : registration.classes) { |
| 78 | + registerReflectionHints(hints, target, registration.memberCategories); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + protected void registerReflectionHints(ReflectionHints hints, Class<?> target, MemberCategory[] memberCategories) { |
| 83 | + hints.registerType(target, type -> type.withMembers(memberCategories)); |
| 84 | + } |
| 85 | + |
| 86 | + @Nullable |
| 87 | + private Class<?> loadClass(String className) { |
| 88 | + try { |
| 89 | + return ClassUtils.forName(className, getClass().getClassLoader()); |
| 90 | + } |
| 91 | + catch (Exception ex) { |
| 92 | + logger.warn("Ignoring '" + className + "': " + ex.getMessage()); |
| 93 | + return null; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + protected record ReflectionRegistration(Class<?>[] classes, MemberCategory[] memberCategories) {} |
| 98 | + |
| 99 | +} |
0 commit comments