Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get generic type of PsiField

I'm developing a Intellij Idea plugin,I want to get all fields info of a class.But I meet with difficulties which I can not get generic type of PsiField. Such as:

private String[] stringsArr;
private List<String> stringList;
private List<BeanLog> beanLogList;

How to get the PsiClass of 'BeanLog' from PsiField ?

like image 674
xiaohuo Avatar asked Dec 25 '22 07:12

xiaohuo


1 Answers

field.getType() returns PsiClassType List<BeanLog>. Its fieldType.getParameters()[0] will be PsiClassType BeanLog. If you want to extract a collection component type in a generic way (not only from List), then you can use PsiUtil.extractIterableTypeParameter(fieldType, false). To get PsiClass from PsiClassType, you can use type.resolve() or PsiUtil.resolveClassInClassTypeOnly.

like image 55
Peter Gromov Avatar answered Jan 11 '23 12:01

Peter Gromov