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

GenericTypeResolver.resolveType() should resolve ParameterizedType recursively #24963

Closed
quaff opened this issue Apr 23, 2020 · 4 comments
Closed
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another type: enhancement A general enhancement

Comments

@quaff
Copy link
Contributor

quaff commented Apr 23, 2020

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;
import org.springframework.core.GenericTypeResolver;

public class Main {
	public interface Parent<T> {
		T m1();
		List<T> m2();
		List<List<T>> m3();
	}
	public interface Child extends Parent<String> {
	}
	public static void main(String[] args) {
		for (Method m : Parent.class.getMethods()) {
			Type type = m.getGenericReturnType();
			type = GenericTypeResolver.resolveType(type, Child.class);
			System.out.println("method " + m.getName() + "() will return " + type);
		}
	}
}

will print

method m3() will return java.util.List<java.util.List<T>>
method m1() will return class java.lang.String
method m2() will return java.util.List<java.lang.String>

The expected type for m3 should be java.util.List<java.util.List<String>>

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 23, 2020
@quaff
Copy link
Contributor Author

quaff commented Apr 26, 2020

Type type = m.getGenericReturnType(); // List<List<T>>
ResolvableType.forType(type).hasUnresolvableGenerics(); //should be true but false

@quaff
Copy link
Contributor Author

quaff commented Apr 26, 2020

Here is my workaround

	public static Type resolve(Type type, Class<?> contextClass) {
		if (type instanceof ParameterizedType) {
			ParameterizedType pt = (ParameterizedType) type;
			ResolvableType[] generics = new ResolvableType[pt.getActualTypeArguments().length];
			int i = 0;
			for (Type arg : pt.getActualTypeArguments()) {
				generics[i++] = ResolvableType.forType(resolve(arg, contextClass));
			}
			return ResolvableType.forClassWithGenerics((Class<?>) pt.getRawType(), generics).getType();
		}
		return GenericTypeResolver.resolveType(type, contextClass);
	}

@jhoeller jhoeller self-assigned this Apr 29, 2020
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 11, 2021
@quaff

This comment was marked as duplicate.

@quaff
Copy link
Contributor Author

quaff commented Mar 8, 2023

Superseded by #30079

@quaff quaff closed this as completed Mar 8, 2023
@sbrannen sbrannen added type: enhancement A general enhancement status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 8, 2023
@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Mar 9, 2023
quaff added a commit to quaff/spring-framework that referenced this issue Nov 30, 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) status: superseded An issue that has been superseded by another type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants