Render parameter type names in ClassFileMethodMetadata

ClassFileMethodMetadata's toString() formatted method parameter types
as packageName() + "." + displayName(). Since ClassDesc.packageName()
is empty for primitive, array and default-package types, these rendered
with a leading dot (for example ".int" and ".String[]") and reference
arrays lost their package. The return type already uses
ClassFileAnnotationMetadata.resolveTypeName(); apply it to the
parameters as well.

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
This commit is contained in:
junhyeong9812
2026-09-03 17:23:18 +02:00
committed by Brian Clozel
parent 34ed7a5e22
commit ad83d5ebd9
2 changed files with 56 additions and 1 deletions
@@ -195,7 +195,7 @@ final class ClassFileMethodMetadata implements MethodMetadata {
builder.append(this.methodName);
builder.append('(');
builder.append(Stream.of(this.descriptor.parameterArray())
.map(desc -> desc.packageName() + "." + desc.displayName())
.map(ClassFileAnnotationMetadata::resolveTypeName)
.collect(Collectors.joining(",")));
builder.append(')');
return builder.toString();
@@ -0,0 +1,55 @@
/*
* Copyright 2002-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.type.classreading;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.type.MethodMetadata;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for parameter type rendering by the {@code java.lang.classfile} based
* {@link ClassFileMethodMetadata}'s {@code toString()}.
*
* @author junhyeong9812
*/
class ClassFileMethodMetadataToStringTests {
@Test
void toStringRendersParameterTypeNames() throws Exception {
ClassFileMetadataReaderFactory factory = new ClassFileMetadataReaderFactory(new DefaultResourceLoader());
MetadataReader reader = factory.getMetadataReader(WithMethod.class.getName());
MethodMetadata method = reader.getAnnotationMetadata().getDeclaredMethods().stream()
.filter(candidate -> candidate.getMethodName().equals("sample"))
.findFirst()
.orElseThrow();
// primitive and array parameter types must keep their canonical names,
// not be prefixed with "." (".int", ".String[]")
assertThat(method.toString()).endsWith(".sample(int,java.lang.String[])");
}
@SuppressWarnings("unused")
static class WithMethod {
public void sample(int number, String[] values) {
}
}
}