Find usages of member variables

I am a total plugin dev newb so have mercy on me...I am wanting to find where member variables are used in a file. I am guessing each usage has its own PsiElement?

I have been able to list all the member variables in a class (and I am pretty happy I got this far). However, how do I go about finding everywhere they are used in a file? (I eventually want to prepend some text to them) A nudge in the right direction would be appreciated.

My current code:

final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
        if (editor != null) {
            final PsiJavaFile javaFile = (PsiJavaFile) PsiUtil.getPsiFileInEditor(editor, project);
            if (javaFile != null) {
                final PsiElement element = javaFile.findElementAt(editor.getCaretModel().getOffset());

                PsiClass currentClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
                if (currentClass != null) {
                    final PsiField[] allFields = currentClass.getAllFields();
                    for (PsiField field : allFields) {
                        logger.info("Field: " + field.getText());
                    }
                }
            }
        }

4 comments
Comment actions Permalink

Hello Michael,

ReferencesSearch.search(psiElement).findAll()

For each of the returned PsiReference objects, you can call getElement()
to get the corresponding element.

I am a total plugin dev newb so have mercy on me...I am wanting to
find where member variables are used in a file. I am guessing each
usage has its own PsiElement?

I have been able to list all the member variables in a class (and I am
pretty happy I got this far). However, how do I go about finding
everywhere they are used in a file? (I eventually want to prepend some
text to them) A nudge in the right direction would be appreciated.

My current code:

 FileEditorManager.getInstance(project).getSelectedTextEditor();
> if (editor != null) {
> final PsiJavaFile javaFile = (PsiJavaFile)
> PsiUtil.getPsiFileInEditor(editor, project);
> if (javaFile != null) {
> final PsiElement element =
> javaFile.findElementAt(editor.getCaretModel().getOffset());
> PsiClass currentClass =
> PsiTreeUtil.getParentOfType(element, PsiClass.class);
> if (currentClass != null) {
> final PsiField[] allFields =
> currentClass.getAllFields();
> for (PsiField field : allFields) {
> logger.info("Field: " + field.getText());
> }
> }
> }
> }]]>

---
Original message URL:
http://www.jetbrains.net/devnet/message/5233126#5233126

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thanks! That is exactly what I was looking for.

When I was trying to figure it out on my own I came up with this code, which really helped me visualize the document tree and see how it works, maybe it will help someone else:

public void printTree(PsiClass psiClass) {
        final PsiElement[] elements = psiClass.getChildren();
        logger.info("elements.length = " + elements.length);
        printElements(elements);
    }

    private void printElements(PsiElement[] elements) {
        logger.info("------------------------");
        for (PsiElement element : elements) {
            final PsiElement[] children = element.getChildren();
            final boolean hasChildren = children.length != 0;
            logger.info("element = " + element + " HasChildren: " + hasChildren);
            if (hasChildren) {
                printElements(children);
            }
        }
    }

0
Comment actions Permalink

Hello Michael,

Thanks! That is exactly what I was looking for.

When I was trying to figure it out on my own I came up with this code,
which really helped me visualize the document tree and see how it
works, maybe it will help someone else:


There's a plugin that does that for you with a much nicer UI :)
http://plugins.intellij.net/plugin/?id=227

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thanks for the heads-up about the PsiViewer plugin. Very nice!

0

Please sign in to leave a comment.