Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
christophstrobl opened this issue Nov 29, 2021 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@christophstrobl
Copy link
Member

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();
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 29, 2021
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 29, 2021
@jhoeller jhoeller self-assigned this Nov 29, 2021
@jhoeller jhoeller added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 29, 2021
@jhoeller jhoeller added this to the 6.0 M1 milestone Nov 29, 2021
@jhoeller
Copy link
Contributor

jhoeller commented Dec 6, 2021

I'm afraid isAssignableFrom is actually right for its purposes there, considering the Object argument as assignable to the Object-resolved type variable. That algorithm isn't really meant to be used for differentiating such arguments to begin with.

One could replace this with straight ResolvableType.equals - but the problem there is the internal type provider reference that the ResolvableType is holding, making it non-equal between the method return wrapper and the extracted generic.

That said, replacing the check with a comparison of ResolvableType.getType() seems to exhibit the desired characteristics:

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

We could introduce a ResolvableType.equalsType(ResolvableType) convenience method for this if this is common enough...

@jhoeller jhoeller modified the milestones: 6.0 M1, 6.0.x Dec 13, 2021
@jhoeller jhoeller modified the milestones: 6.0.x, 6.1.x Feb 14, 2023
@jhoeller jhoeller modified the milestones: 6.1.x, 6.1.0-M2 Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants