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 = ReferencesSearch.search(elementAt, 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.

9 comments
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 ?

0
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!!!

0
Comment actions Permalink

But the PSI Cookbook clearly states that its ReferencesSearch.search().

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 ReferencesSearch.search(PsiElement) is used ??

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

0
Comment actions Permalink

Hi

take a look at this Thread:

http://devnet.jetbrains.com/message/5497803#5497803

maybe this helps you

Markus

0
Comment actions Permalink

Thanks Markus. But my problem is in getting the Usages of a variable, i.e, ReferencesSearch.search(PsiElement) is not working.

0
Comment actions Permalink

I did figure out the problem. So, this is where I went wrong :

ReferencesSearch.search(PsiElement 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, ReferencesSearch.search(parentOfType) 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.

0
Comment actions Permalink

similar problem here: ReferencesSearch returns 0 results.

element exists, references are created..

any hints?

update: never mind, found a workaround

0
Comment actions Permalink

I am having a similar issue implementing ReferencesSearch.search().  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 ReferencesSearch.search().findAll(), it returns an empty collection.

A little help would be appreciated.

Thanks and Regards,

Hemansu Kumar Tanty

Prod-Dev Engineer, Appknox 

0
Comment actions Permalink

Hi, @Henry Peterson, I have a problem with ReferencesSearch.search().  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 = ReferencesSearch.search(valueOVES[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();
Integer.valueOf("1223");
Integer.valueOf("1223");
Integer.valueOf("1223");
Integer.valueOf("1223");
Integer.valueOf("1223");
Integer.valueOf("1223");
}
}
}

But the all's size is 0;

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

0

Please sign in to leave a comment.