From 20b35f068a00cca6005ce731f2cca3034d0adb02 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 25 Mar 2025 15:32:42 +0100 Subject: [PATCH] Add visibility and return type to SimpleMethodMetadata toString Closes gh-34649 --- .../ConfigurationClassAndBeanMethodTests.java | 4 +-- .../SimpleMethodMetadataReadingVisitor.java | 33 +++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassAndBeanMethodTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassAndBeanMethodTests.java index 91876dbdf6a..b7866f31bc6 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassAndBeanMethodTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassAndBeanMethodTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -128,7 +128,7 @@ class ConfigurationClassAndBeanMethodTests { .startsWith("ConfigurationClass: beanName 'Config1', class path resource"); List beanMethods = getBeanMethods(configurationClass); - String prefix = "BeanMethod: " + Config1.class.getName(); + String prefix = "BeanMethod: java.lang.String " + Config1.class.getName(); assertThat(beanMethods.get(0).toString()).isEqualTo(prefix + ".bean0()"); assertThat(beanMethods.get(1).toString()).isEqualTo(prefix + ".bean1(java.lang.String)"); assertThat(beanMethods.get(2).toString()).isEqualTo(prefix + ".bean2(java.lang.String,java.lang.Integer)"); diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleMethodMetadataReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleMethodMetadataReadingVisitor.java index 0e1c686230b..02a521baf61 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleMethodMetadataReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleMethodMetadataReadingVisitor.java @@ -24,6 +24,7 @@ import org.jspecify.annotations.Nullable; import org.springframework.asm.AnnotationVisitor; import org.springframework.asm.MethodVisitor; +import org.springframework.asm.Opcodes; import org.springframework.asm.SpringAsmInfo; import org.springframework.asm.Type; import org.springframework.core.annotation.MergedAnnotation; @@ -87,7 +88,7 @@ final class SimpleMethodMetadataReadingVisitor extends MethodVisitor { private Object getSource() { Source source = this.source; if (source == null) { - source = new Source(this.declaringClassName, this.methodName, this.descriptor); + source = new Source(this.declaringClassName, this.methodName, this.access, this.descriptor); this.source = source; } return source; @@ -103,13 +104,16 @@ final class SimpleMethodMetadataReadingVisitor extends MethodVisitor { private final String methodName; + private final int access; + private final String descriptor; private @Nullable String toStringValue; - Source(String declaringClassName, String methodName, String descriptor) { + Source(String declaringClassName, String methodName, int access, String descriptor) { this.declaringClassName = declaringClassName; this.methodName = methodName; + this.access = access; this.descriptor = descriptor; } @@ -118,6 +122,7 @@ final class SimpleMethodMetadataReadingVisitor extends MethodVisitor { int result = 1; result = 31 * result + this.declaringClassName.hashCode(); result = 31 * result + this.methodName.hashCode(); + result = 31 * result + this.access; result = 31 * result + this.descriptor.hashCode(); return result; } @@ -132,7 +137,8 @@ final class SimpleMethodMetadataReadingVisitor extends MethodVisitor { } Source otherSource = (Source) other; return (this.declaringClassName.equals(otherSource.declaringClassName) && - this.methodName.equals(otherSource.methodName) && this.descriptor.equals(otherSource.descriptor)); + this.methodName.equals(otherSource.methodName) && + this.access == otherSource.access && this.descriptor.equals(otherSource.descriptor)); } @Override @@ -140,6 +146,27 @@ final class SimpleMethodMetadataReadingVisitor extends MethodVisitor { String value = this.toStringValue; if (value == null) { StringBuilder builder = new StringBuilder(); + if ((this.access & Opcodes.ACC_PUBLIC) != 0) { + builder.append("public "); + } + if ((this.access & Opcodes.ACC_PROTECTED) != 0) { + builder.append("protected "); + } + if ((this.access & Opcodes.ACC_PRIVATE) != 0) { + builder.append("private "); + } + if ((this.access & Opcodes.ACC_ABSTRACT) != 0) { + builder.append("abstract "); + } + if ((this.access & Opcodes.ACC_STATIC) != 0) { + builder.append("static "); + } + if ((this.access & Opcodes.ACC_FINAL) != 0) { + builder.append("final "); + } + Type returnType = Type.getReturnType(this.descriptor); + builder.append(returnType.getClassName()); + builder.append(' '); builder.append(this.declaringClassName); builder.append('.'); builder.append(this.methodName);