ReferencesSearch findAll() returns Empty Collection

I have the following java class.

public class Sample {     public static void main(String[] args) {         String name = "TEXT";         Trial trial = new Trial();         trial.someMethod();     } }

I am writing a plugin that on placing the cursor on a variable, and selecting an option from menu, will show all the line numbers (references) where that variable is used.

I tried ReferencesSearch. The following is the Action class of my plugin.

public class FindSomethingAction extends AnAction {     @Override     public void actionPerformed(AnActionEvent e) {         Editor data = e.getData(PlatformDataKeys.EDITOR);         CaretModel caretModel = data.getCaretModel();         int offset = caretModel.getOffset();         PsiFile psiFile = e.getData(PlatformDataKeys.PSI_FILE);         PsiElement elementAt = psiFile.findElementAt(offset);         System.out.println("elementAt.getText() = " + elementAt.getText());         final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(e.getProject());         Query<PsiReference> search =, projectScope);         Collection<PsiReference> all = search.findAll();         System.out.println("all.size() = " + all.size());     } }

But all.size() is 0 (ZERO). Where am I going wrong? Please help me out.

Comment actions Permalink

When I place the cursor on the variable trial and run elementAt.getText(), I get the word "trial", but when I run elementAt.getReference(), I get null ?

1. Why is the reference for the PsiElement null?
2. Should I register the reference somewhere? But I am trying to get the reference from java class. Isn't reference already registered?
3. I am not trying to write a Custom Language plugin. I just want the get all the Usages of any variable in the java class. Same functionality as Find Usages, but I want to get the line numbers of them, so I can process them. What should I do ?

Comment actions Permalink

I still didn't get the answer what I was looking for. But I did stumble upon something.

Instead of using

PsiFile psiFile = e.getData(PlatformDataKeys.PSI_FILE);
PsiElement elementAt = psiFile.findElementAt(offset);

I used

PsiElement elementAt = e.getData(PlatformDataKeys.PSI_ELEMENT);

Now when I run the plugin, and lets say I have a java class like this :

public class Sample {     public static void main(String[] args) {         String name = "TEXT";         Trial trial = new Trial();         name = "whatever";         name = "something";         trial.someMethod();         name.equalsIgnoreCase("WHAT");     } }

On placing the cursor on the variable name and when i run the command (FindSomethingAction), I get all.size() = 3....

But when I access the PsiReference Collection (variable 'all'), all the 3 references are pointing to "String name = "TEXT";".
So, I get the same line number again and again, thrice.

for(PsiReference reference : all){      String canonicalText = reference.getCanonicalText();
     System.out.println("canonicalText = " + canonicalText); }

I get "canonicalText = String name = "TEXT" " , 3 times in a row.

But I want the line numbers of everywhere the variable 'name' is used. Now I am starting to believe that ReferencesSearch is not the right one... All I want is Find Usages, but I need to have control of the output, so I can process them.

The Quest continues!!!

Comment actions Permalink

But the PSI Cookbook clearly states that its

I know its a simple API. There should be no trouble using it, but unfortunately, I have. Could someone please point me to some sample plugin code where is used ??

Also could anyone please point me the Discussion thread on releation between PsiElement, PsiVariable, PsiReference and all that...

Comment actions Permalink


take a look at this Thread:

maybe this helps you


Comment actions Permalink

Thanks Markus. But my problem is in getting the Usages of a variable, i.e, is not working.

Comment actions Permalink

I did figure out the problem. So, this is where I went wrong : element) , takes in a PsiElement type object.

PsiFile.findElementAt(offset) returns me a PsiElement where my cursor was placed. But the problem was, that PsiElement that is returned here contains no reference data. Its just a token. I need to get the PsiElement that has reference data. Hence I used this :

PsiVariable parentOfType = PsiTreeUtil.getParentOfType(element, PsiVariable.class);

Now, works perfectly.! It depends on what you are putting your cursor on, it might be PsiVariable, or PsiField, or PsiMethod, etc... Dmitry was right, ReferencesSearch is a simple API. My problem was I was passing the wrong input.

Currently I am just playing around with PsiElements and ReferencesSearch, will post a thread with all my findings soon.

Comment actions Permalink

similar problem here: ReferencesSearch returns 0 results.

element exists, references are created..

any hints?

update: never mind, found a workaround

Comment actions Permalink

I am having a similar issue implementing  I am getting a PsiElement using new JavaElementVisitor and overriding a method from the JavaElementVisitor. Now when i try to find all the usages of the a variable in my code using, it returns an empty collection.

A little help would be appreciated.

Thanks and Regards,

Hemansu Kumar Tanty

Prod-Dev Engineer, Appknox 

Comment actions Permalink

Hi, @Henry Peterson, I have a problem with  What I am doing is custom the Find Usages. I want to find where the Integer's valueOf method had been  referenced in my project, below is my code:

public void actionPerformed(AnActionEvent e) {
// TODO: insert action logic here
Project project = e.getData(PlatformDataKeys.PROJECT);

JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
PsiClass aClass = javaPsiFacade.findClass("java.lang.Integer", GlobalSearchScope.allScope(project));
PsiMethod[] valueOVES = aClass.findMethodsByName("valueOf", false);

for (int i = 0; i < valueOVES.length; i++) {
Query<PsiReference> search =[i], GlobalSearchScope.projectScope(project), false);
Collection<PsiReference> all = search.findAll();

The valueOVES's size is three, that's correct.

And below is my test file JavaClass:

public class JavaClass {
class TestJava{
JavaClass javaClass;
public TestJava( ){
this.javaClass = new JavaClass();

But the all's size is 0;

Could you please help me with this problem? Thanks in advance!!


Please sign in to leave a comment.