From f6a3346e3fac76a8afa9d80af5e9052a19ea106d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 6 Nov 2025 10:50:53 +0100 Subject: [PATCH] Polishing --- .../MetadataReaderFactoryDelegate.java | 1 + .../ClassFileAnnotationMetadata.java | 16 +++++++++++----- .../classreading/ClassFileClassMetadata.java | 7 ++++++- .../classreading/ClassFileMetadataReader.java | 2 ++ .../ClassFileMetadataReaderFactory.java | 3 ++- .../classreading/ClassFileMethodMetadata.java | 17 ++++++++++++----- .../MetadataReaderFactoryDelegate.java | 2 ++ 7 files changed, 36 insertions(+), 12 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/MetadataReaderFactoryDelegate.java b/spring-core/src/main/java/org/springframework/core/type/classreading/MetadataReaderFactoryDelegate.java index a047446c29f..3f62f8e2a2b 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/MetadataReaderFactoryDelegate.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/MetadataReaderFactoryDelegate.java @@ -37,4 +37,5 @@ abstract class MetadataReaderFactoryDelegate { static MetadataReaderFactory create(@Nullable ClassLoader classLoader) { return new SimpleMetadataReaderFactory(classLoader); } + } diff --git a/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileAnnotationMetadata.java b/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileAnnotationMetadata.java index 20cd3a8591d..7577a65083f 100644 --- a/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileAnnotationMetadata.java +++ b/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileAnnotationMetadata.java @@ -16,7 +16,6 @@ package org.springframework.core.type.classreading; - import java.lang.classfile.Annotation; import java.lang.classfile.AnnotationElement; import java.lang.classfile.AnnotationValue; @@ -42,11 +41,15 @@ import org.springframework.util.ClassUtils; /** * Parse {@link RuntimeVisibleAnnotationsAttribute} into {@link MergedAnnotations} * instances. + * * @author Brian Clozel + * @since 7.0 */ abstract class ClassFileAnnotationMetadata { - static MergedAnnotations createMergedAnnotations(String className, RuntimeVisibleAnnotationsAttribute annotationAttribute, @Nullable ClassLoader classLoader) { + static MergedAnnotations createMergedAnnotations( + String className, RuntimeVisibleAnnotationsAttribute annotationAttribute, @Nullable ClassLoader classLoader) { + Set> annotations = annotationAttribute.annotations() .stream() .map(ann -> createMergedAnnotation(className, ann, classLoader)) @@ -56,7 +59,9 @@ abstract class ClassFileAnnotationMetadata { } @SuppressWarnings("unchecked") - private static @Nullable MergedAnnotation createMergedAnnotation(String className, Annotation annotation, @Nullable ClassLoader classLoader) { + private static @Nullable MergedAnnotation createMergedAnnotation( + String className, Annotation annotation, @Nullable ClassLoader classLoader) { + String typeName = fromTypeDescriptor(annotation.className().stringValue()); if (AnnotationFilter.PLAIN.matches(typeName)) { return null; @@ -78,7 +83,9 @@ abstract class ClassFileAnnotationMetadata { } } - private static @Nullable Object readAnnotationValue(String className, AnnotationValue elementValue, @Nullable ClassLoader classLoader) { + private static @Nullable Object readAnnotationValue( + String className, AnnotationValue elementValue, @Nullable ClassLoader classLoader) { + switch (elementValue) { case AnnotationValue.OfConstant constantValue -> { return constantValue.resolvedValue(); @@ -168,7 +175,6 @@ abstract class ClassFileAnnotationMetadata { record Source(Annotation entryName) { - } } diff --git a/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileClassMetadata.java b/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileClassMetadata.java index 407a171cb4f..859d773f143 100644 --- a/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileClassMetadata.java +++ b/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileClassMetadata.java @@ -42,7 +42,9 @@ import org.springframework.util.StringUtils; /** * {@link AnnotationMetadata} implementation that leverages * the {@link java.lang.classfile.ClassFile} API. + * * @author Brian Clozel + * @since 7.0 */ class ClassFileClassMetadata implements AnnotationMetadata { @@ -66,9 +68,11 @@ class ClassFileClassMetadata implements AnnotationMetadata { private @Nullable Set annotationTypes; + ClassFileClassMetadata(String className, AccessFlags accessFlags, @Nullable String enclosingClassName, @Nullable String superClassName, boolean independentInnerClass, Set interfaceNames, Set memberClassNames, Set declaredMethods, MergedAnnotations mergedAnnotations) { + this.className = className; this.accessFlags = accessFlags; this.enclosingClassName = enclosingClassName; @@ -80,6 +84,7 @@ class ClassFileClassMetadata implements AnnotationMetadata { this.mergedAnnotations = mergedAnnotations; } + @Override public String getClassName() { return this.className; @@ -215,6 +220,7 @@ class ClassFileClassMetadata implements AnnotationMetadata { return builder.build(); } + static class Builder { private final ClassLoader clasLoader; @@ -297,7 +303,6 @@ class ClassFileClassMetadata implements AnnotationMetadata { return new ClassFileClassMetadata(this.className, this.accessFlags, this.enclosingClassName, this.superClassName, independentInnerClass, this.interfaceNames, this.memberClassNames, this.declaredMethods, this.mergedAnnotations); } - } } diff --git a/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMetadataReader.java b/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMetadataReader.java index 10caf631c6e..c351928862b 100644 --- a/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMetadataReader.java +++ b/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMetadataReader.java @@ -31,6 +31,7 @@ import org.springframework.core.type.ClassMetadata; * {@link MetadataReader} implementation based on the {@link ClassFile} API. * * @author Brian Clozel + * @since 7.0 */ final class ClassFileMetadataReader implements MetadataReader { @@ -51,6 +52,7 @@ final class ClassFileMetadataReader implements MetadataReader { } } + @Override public Resource getResource() { return this.resource; diff --git a/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMetadataReaderFactory.java b/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMetadataReaderFactory.java index eebcdb3e705..553f1acd904 100644 --- a/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMetadataReaderFactory.java +++ b/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMetadataReaderFactory.java @@ -32,7 +32,6 @@ import org.springframework.core.io.ResourceLoader; */ public class ClassFileMetadataReaderFactory extends AbstractMetadataReaderFactory { - /** * Create a new ClassFileMetadataReaderFactory for the default class loader. */ @@ -57,8 +56,10 @@ public class ClassFileMetadataReaderFactory extends AbstractMetadataReaderFactor super(classLoader); } + @Override public MetadataReader getMetadataReader(Resource resource) throws IOException { return new ClassFileMetadataReader(resource, getResourceLoader().getClassLoader()); } + } diff --git a/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMethodMetadata.java b/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMethodMetadata.java index ee0cc42ad34..98e7597be33 100644 --- a/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMethodMetadata.java +++ b/spring-core/src/main/java24/org/springframework/core/type/classreading/ClassFileMethodMetadata.java @@ -37,7 +37,9 @@ import org.springframework.util.ClassUtils; /** * {@link MethodMetadata} extracted from class bytecode using the * {@link java.lang.classfile.ClassFile} API. + * * @author Brian Clozel + * @since 7.0 */ class ClassFileMethodMetadata implements MethodMetadata { @@ -54,7 +56,10 @@ class ClassFileMethodMetadata implements MethodMetadata { private final MergedAnnotations annotations; - ClassFileMethodMetadata(String methodName, AccessFlags accessFlags, @Nullable String declaringClassName, String returnTypeName, Object source, MergedAnnotations annotations) { + + ClassFileMethodMetadata(String methodName, AccessFlags accessFlags, @Nullable String declaringClassName, + String returnTypeName, Object source, MergedAnnotations annotations) { + this.methodName = methodName; this.accessFlags = accessFlags; this.declaringClassName = declaringClassName; @@ -63,6 +68,7 @@ class ClassFileMethodMetadata implements MethodMetadata { this.annotations = annotations; } + @Override public String getMethodName() { return this.methodName; @@ -131,6 +137,7 @@ class ClassFileMethodMetadata implements MethodMetadata { return this.source.toString(); } + static ClassFileMethodMetadata of(MethodModel methodModel, ClassLoader classLoader) { String methodName = methodModel.methodName().stringValue(); AccessFlags flags = methodModel.flags(); @@ -146,13 +153,13 @@ class ClassFileMethodMetadata implements MethodMetadata { return new ClassFileMethodMetadata(methodName, flags, declaringClassName, returnTypeName, source, annotations); } + /** * {@link MergedAnnotation} source. - * * @param declaringClassName the name of the declaring class - * @param flags the access flags - * @param methodName the name of the method - * @param descriptor the bytecode descriptor for this method + * @param flags the access flags + * @param methodName the name of the method + * @param descriptor the bytecode descriptor for this method */ record Source(@Nullable String declaringClassName, AccessFlags flags, String methodName, MethodTypeDesc descriptor) { diff --git a/spring-core/src/main/java24/org/springframework/core/type/classreading/MetadataReaderFactoryDelegate.java b/spring-core/src/main/java24/org/springframework/core/type/classreading/MetadataReaderFactoryDelegate.java index ecb7b7b3e50..875b9f79ffb 100644 --- a/spring-core/src/main/java24/org/springframework/core/type/classreading/MetadataReaderFactoryDelegate.java +++ b/spring-core/src/main/java24/org/springframework/core/type/classreading/MetadataReaderFactoryDelegate.java @@ -25,6 +25,7 @@ import org.springframework.core.io.ResourceLoader; * For JDK >= 24, the {@link ClassFileMetadataReaderFactory} is being used. * * @author Brian Clozel + * @since 7.0 * @see MetadataReaderFactory */ abstract class MetadataReaderFactoryDelegate { @@ -36,4 +37,5 @@ abstract class MetadataReaderFactoryDelegate { static MetadataReaderFactory create(@Nullable ClassLoader classLoader) { return new ClassFileMetadataReaderFactory(classLoader); } + }