resolve generic types

Hi,

I have what I think is quite a simple problem. Given java classes

abstract class Xxx<T> {
public T getFoo() {
return null;
}
}
public class Yyy extends Xxx<Double> { }

I want to get the return type of Yyy.getFoo(). Any idea how?

I have tried psiClassType.resolveGenerics(), but the result has an EmptySubstitutor, so I don't think this is the right method.

Generally there seems to be a shortcut for this kind of thing in util class somewhere.

I have read https://intellij-support.jetbrains.com/hc/en-us/community/posts/206277279-Uncooking-Generic-Types?input_string=resolve%20generic%20types at length but it seems more involved than I need, and I can't understand it anyway.

cheers, jamie

 

0

Hi,

what do you have? If you have an expression y.getFoo(), then getType() on that expression would give you Double. If you want to precalculate that type yourself, then you need to repeat all code in PsiMethodCallExpressionImpl.getType which could be tedious.

Hope this helps,

Anna

 

0

Hi Anna,

I have a PsiMethod... which I get from psiClass.findMethodsByName.

getReturnType on the PsiMethod gives me T for the example above. 

I think I can see what's happening here... when I call findMethodsByName I am using checkBases = true, so it's getting the method from the superclass, and I guess the return type T is correct... findMethodsAndTheirSubstitutorsByName looks more promising. Am I on the right track or is there a better way?

cheers, jamie

0

then

TypeConversionUtil.getClassSubstitutor(method.getContainingClass(), psciClass, PsiSubstitutor.EMPTY).substitute(method.getReturnType())

Please note that it's the type of the return type of method in the derived class, it is not a type of the expression y.getFoo().

 

1

请先登录再写评论。