Get PsiClass from PsiFile

Does anyone know how to get PsiClass (java) from PsiFile  ?

Thanks.

2
2 comments

Just typecast PsiFile to PsiJavaFile. Then call getClasses() method.

PsiJavaFile psiJavaFile = (PsiJavaFile)psiFile;
final PsiClass[] classes = psiJavaFile.getClasses();
2

To support both Java and Kotlin, you will have to type cast to "PsiClassOwner"

val psiClassOwner = psiFile as PsiClassOwner
psiClassOwner.classes.forEach { ... }
1

Please sign in to leave a comment.