|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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.type.classreading; |
| 18 | + |
| 19 | + |
| 20 | +import java.lang.classfile.Annotation; |
| 21 | +import java.lang.classfile.AnnotationElement; |
| 22 | +import java.lang.classfile.AnnotationValue; |
| 23 | +import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute; |
| 24 | +import java.lang.reflect.Array; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.LinkedHashMap; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +import org.jspecify.annotations.Nullable; |
| 34 | + |
| 35 | +import org.springframework.core.annotation.AnnotationFilter; |
| 36 | +import org.springframework.core.annotation.MergedAnnotation; |
| 37 | +import org.springframework.core.annotation.MergedAnnotations; |
| 38 | +import org.springframework.util.ClassUtils; |
| 39 | + |
| 40 | +/** |
| 41 | + * Parse {@link RuntimeVisibleAnnotationsAttribute} into {@link MergedAnnotations} |
| 42 | + * instances. |
| 43 | + * @author Brian Clozel |
| 44 | + */ |
| 45 | +abstract class ClassFileAnnotationMetadata { |
| 46 | + |
| 47 | + static MergedAnnotations createMergedAnnotations(String className, RuntimeVisibleAnnotationsAttribute annotationAttribute, @Nullable ClassLoader classLoader) { |
| 48 | + Set<MergedAnnotation<?>> annotations = annotationAttribute.annotations() |
| 49 | + .stream() |
| 50 | + .map(ann -> createMergedAnnotation(className, ann, classLoader)) |
| 51 | + .filter(Objects::nonNull) |
| 52 | + .collect(Collectors.toSet()); |
| 53 | + return MergedAnnotations.of(annotations); |
| 54 | + } |
| 55 | + |
| 56 | + @SuppressWarnings("unchecked") |
| 57 | + private static <A extends java.lang.annotation.Annotation> @Nullable MergedAnnotation<A> createMergedAnnotation(String className, Annotation annotation, @Nullable ClassLoader classLoader) { |
| 58 | + String typeName = fromTypeDescriptor(annotation.className().stringValue()); |
| 59 | + if (AnnotationFilter.PLAIN.matches(typeName)) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + Map<String, Object> attributes = new LinkedHashMap<>(4); |
| 63 | + try { |
| 64 | + for (AnnotationElement element : annotation.elements()) { |
| 65 | + Object annotationValue = readAnnotationValue(className, element.value(), classLoader); |
| 66 | + if (annotationValue != null) { |
| 67 | + attributes.put(element.name().stringValue(), annotationValue); |
| 68 | + } |
| 69 | + } |
| 70 | + Map<String, Object> compactedAttributes = (attributes.isEmpty() ? Collections.emptyMap() : attributes); |
| 71 | + Class<A> annotationType = (Class<A>) ClassUtils.forName(typeName, classLoader); |
| 72 | + return MergedAnnotation.of(classLoader, new Source(annotation), annotationType, compactedAttributes); |
| 73 | + } |
| 74 | + catch (ClassNotFoundException | LinkageError ex) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static @Nullable Object readAnnotationValue(String className, AnnotationValue elementValue, @Nullable ClassLoader classLoader) { |
| 80 | + switch (elementValue) { |
| 81 | + case AnnotationValue.OfConstant constantValue -> { |
| 82 | + return constantValue.resolvedValue(); |
| 83 | + } |
| 84 | + case AnnotationValue.OfAnnotation annotationValue -> { |
| 85 | + return createMergedAnnotation(className, annotationValue.annotation(), classLoader); |
| 86 | + } |
| 87 | + case AnnotationValue.OfClass classValue -> { |
| 88 | + return fromTypeDescriptor(classValue.className().stringValue()); |
| 89 | + } |
| 90 | + case AnnotationValue.OfEnum enumValue -> { |
| 91 | + return parseEnum(enumValue, classLoader); |
| 92 | + } |
| 93 | + case AnnotationValue.OfArray arrayValue -> { |
| 94 | + return parseArrayValue(className, classLoader, arrayValue); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static String fromTypeDescriptor(String descriptor) { |
| 100 | + return descriptor.substring(1, descriptor.length() - 1) |
| 101 | + .replace('/', '.'); |
| 102 | + } |
| 103 | + |
| 104 | + private static Object parseArrayValue(String className, @org.jetbrains.annotations.Nullable ClassLoader classLoader, AnnotationValue.OfArray arrayValue) { |
| 105 | + if (arrayValue.values().isEmpty()) { |
| 106 | + return new Object[0]; |
| 107 | + } |
| 108 | + Stream<AnnotationValue> stream = arrayValue.values().stream(); |
| 109 | + switch (arrayValue.values().getFirst()) { |
| 110 | + case AnnotationValue.OfInt _ -> { |
| 111 | + return stream.map(AnnotationValue.OfInt.class::cast).mapToInt(AnnotationValue.OfInt::intValue).toArray(); |
| 112 | + } |
| 113 | + case AnnotationValue.OfDouble _ -> { |
| 114 | + return stream.map(AnnotationValue.OfDouble.class::cast).mapToDouble(AnnotationValue.OfDouble::doubleValue).toArray(); |
| 115 | + } |
| 116 | + case AnnotationValue.OfLong _ -> { |
| 117 | + return stream.map(AnnotationValue.OfLong.class::cast).mapToLong(AnnotationValue.OfLong::longValue).toArray(); |
| 118 | + } |
| 119 | + default -> { |
| 120 | + Object firstResolvedValue = readAnnotationValue(className, arrayValue.values().getFirst(), classLoader); |
| 121 | + return stream |
| 122 | + .map(rawValue -> readAnnotationValue(className, rawValue, classLoader)) |
| 123 | + .toArray(s -> (Object[]) Array.newInstance(firstResolvedValue.getClass(), s)); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @SuppressWarnings("unchecked") |
| 129 | + private static @Nullable <E extends Enum<E>> Enum<E> parseEnum(AnnotationValue.OfEnum enumValue, @Nullable ClassLoader classLoader) { |
| 130 | + String enumClassName = fromTypeDescriptor(enumValue.className().stringValue()); |
| 131 | + try { |
| 132 | + Class<E> enumClass = (Class<E>) ClassUtils.forName(enumClassName, classLoader); |
| 133 | + return Enum.valueOf(enumClass, enumValue.constantName().stringValue()); |
| 134 | + } |
| 135 | + catch (ClassNotFoundException | LinkageError ex) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + record Source(Annotation entryName) { |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments