Fix ClassFileMethodMetadata return type names for primitives and arrays

Return correct names for primitive and array types
Introduce resolveTypeName helper for ClassDesc handling
Add ClassFileMethodMetadataTests (java24Test)
Extend AbstractMethodMetadataTests with void return case

See gh-36577

Signed-off-by: Junseo Bae <ferrater1013@gmail.com>
This commit is contained in:
Junseo Bae
2026-04-02 10:17:21 +02:00
committed by Brian Clozel
parent 3f01e3fe11
commit b01fdb0140
3 changed files with 106 additions and 1 deletions
@@ -143,7 +143,7 @@ final class ClassFileMethodMetadata implements MethodMetadata {
AccessFlags flags = methodModel.flags();
String declaringClassName = methodModel.parent().map(parent -> ClassUtils.convertResourcePathToClassName(parent.thisClass().name().stringValue())).orElse(null);
ClassDesc returnType = methodModel.methodTypeSymbol().returnType();
String returnTypeName = returnType.packageName() + "." + returnType.displayName();
String returnTypeName = resolveTypeName(returnType);
Source source = new Source(declaringClassName, flags, methodName, methodModel.methodTypeSymbol());
MergedAnnotations annotations = methodModel.elementStream()
.filter(element -> element instanceof RuntimeVisibleAnnotationsAttribute)
@@ -154,6 +154,18 @@ final class ClassFileMethodMetadata implements MethodMetadata {
}
private static String resolveTypeName(ClassDesc type) {
if (type.isPrimitive()) {
return type.displayName();
}
if (type.isArray()) {
return resolveTypeName(type.componentType()) + "[]";
}
String packageName = type.packageName();
return (packageName.isEmpty() ? type.displayName() : packageName + "." + type.displayName());
}
/**
* {@link MergedAnnotation} source.
* @param declaringClassName the name of the declaring class
@@ -102,6 +102,11 @@ public abstract class AbstractMethodMetadataTests {
String.class.getName());
}
@Test
void getReturnTypeReturnsVoidForVoidReturnType() {
assertThat(getTagged(WithVoidMethod.class).getReturnTypeName()).isEqualTo("void");
}
@Test
void isAbstractWhenAbstractReturnsTrue() {
assertThat(getTagged(WithAbstractMethod.class).isAbstract()).isTrue();
@@ -217,6 +222,13 @@ public abstract class AbstractMethodMetadataTests {
}
public static class WithVoidMethod {
@Tag
public void test() {}
}
public static class WithMethodWithOneArgument {
@Tag
@@ -0,0 +1,81 @@
package org.springframework.core.type.classreading;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.junit.jupiter.api.Test;
import org.springframework.core.type.MethodMetadata;
import static org.assertj.core.api.Assertions.assertThat;
class ClassFileMethodMetadataTests {
@Test
void getReturnTypeReturnsVoidForVoidReturnType() throws Exception {
MethodMetadata metadata = new ClassFileMetadataReaderFactory(getClass().getClassLoader())
.getMetadataReader(WithVoidMethod.class.getName())
.getAnnotationMetadata()
.getAnnotatedMethods(Tag.class.getName())
.iterator().next();
assertThat(metadata.getReturnTypeName()).isEqualTo("void");
}
@Test
void getReturnTypeReturnsPrimitiveForPrimitiveReturnType() throws Exception {
MethodMetadata metadata = new ClassFileMetadataReaderFactory(getClass().getClassLoader())
.getMetadataReader(WithIntMethod.class.getName())
.getAnnotationMetadata()
.getAnnotatedMethods(Tag.class.getName())
.iterator().next();
assertThat(metadata.getReturnTypeName()).isEqualTo("int");
}
@Test
void getReturnTypeReturnsReferenceTypeForReferenceReturnType() throws Exception {
MethodMetadata metadata = new ClassFileMetadataReaderFactory(getClass().getClassLoader())
.getMetadataReader(WithStringMethod.class.getName())
.getAnnotationMetadata()
.getAnnotatedMethods(Tag.class.getName())
.iterator().next();
assertThat(metadata.getReturnTypeName()).isEqualTo(String.class.getName());
}
@Test
void getReturnTypeReturnsArrayTypeForArrayReturnType() throws Exception {
MethodMetadata metadata = new ClassFileMetadataReaderFactory(getClass().getClassLoader())
.getMetadataReader(WithStringArrayMethod.class.getName())
.getAnnotationMetadata()
.getAnnotatedMethods(Tag.class.getName())
.iterator().next();
assertThat(metadata.getReturnTypeName()).isEqualTo("java.lang.String[]");
}
@Retention(RetentionPolicy.RUNTIME)
@interface Tag {}
public static class WithVoidMethod {
@Tag
public void test() {}
}
public static class WithIntMethod {
@Tag
public int test() { return 0; }
}
public static class WithStringMethod {
@Tag
public String test() { return ""; }
}
public static class WithStringArrayMethod {
@Tag
public String[] test() { return new String[0]; }
}
}