How do I get method list from PsiFile or VirtualFile?


How do I get a method list from PsiFile or VirtualFile?

评论操作 固定链接

Hello, Hikaru.
Try to explore the following classes:

PsiManager.findFile(VirtualFile file)

评论操作 固定链接

Hi Hiakru,

you need a Visitor and a PsiFile to access the methods.
If you want to search methods for a Java Class you can use a JavaRecursiveElementVisitor that extends your own visitor and override the method
If you want to search methods for another languages you can use a PsiRecusiveElementVisitor and use visitElement and check if the element you get is an instance of PsiMethod.

example for a Visitor for a PsiFile:

public class Visitor extends PsiRecursiveElementVisitor {

    private List<PsiMethod> psiMethods = new ArrayList<PsiMethod>();

    public void visitElement(PsiElement element) {

        if (element instanceof PsiMethod) {
            psiMethods.add((PsiMethod) element);


    public List<PsiMethod> getPsiMethods() {
        return psiMethods;

your PsiFile has an accept Method that accepts the visitor and after that you get the Methods from your visitor.
I hope this helps you.