Skip to content

ResolvableType should allow to determine matching type argument for generic return type #27748

@christophstrobl

Description

@christophstrobl

Assume a declaration like the following where you need to identify the one method parameter where the generic type matches the return type of the method.

interface SomeRepository {
	<T> T someMethod1(Class<T> arg0, Class<?> arg1, Class<Object> arg2);
}
Method method = ReflectionUtils.findMethod(SomeRepository.class, "someMethod1", Class.class, Class.class, Class.class);

ResolvableType returnType = ResolvableType.forMethodReturnType(method, SomeRepository.class);

ResolvableType arg0 = ResolvableType.forMethodParameter(method, 0, SomeRepository.class); // generic[0]=T
ResolvableType arg1 = ResolvableType.forMethodParameter(method, 1, SomeRepository.class); // generic[0]=?
ResolvableType arg2 = ResolvableType.forMethodParameter(method, 2, SomeRepository.class); // generic[0]=java.lang.Object

assertThat(returnType.isAssignableFrom(arg0.as(Class.class).getGeneric(0))).isTrue();
assertThat(returnType.isAssignableFrom(arg1.as(Class.class).getGeneric(0))).isFalse();
assertThat(returnType.isAssignableFrom(arg2.as(Class.class).getGeneric(0))).isFalse();

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions