How do I get method list from PsiFile or VirtualFile?

Hi.

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

0
Avatar
Permanently deleted user

Hello, Hikaru.
Try to explore the following classes:

PsiManager.findFile(VirtualFile file)
PsiJavaFile.getClasses()

0
Avatar
Permanently deleted user

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
visitMethod.
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>();

    @Override
    public void visitElement(PsiElement element) {

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

        super.visitElement(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.

Markus

2

请先登录再写评论。