How to get all variables within a PsiClass (global and local variables)
Hi,
can anyone help me out on finding all variables of a PsiClass? I'm looking for global as well as local variables. So far I am only able to find all global fields via psiClass.getAllFields(). Is there a convenient way in finding ALL variables within a PsiClass?
Furthermore, I wanted to ask how to identify each time a variable is assigned a value, such as
int a = 9; a = 30; String s = "text1"; s = "text2"
I'm looking for all of such assignments, can anyone help me out with these two questions?
Best regards!
Please sign in to leave a comment.
Hi,
you may create visitor and collect all variables/assignments you need psiClass.accept(visitor). For variables you may also collect them through the dedicated processor (com.intellij.psi.scope.processor.VariablesProcessor) walkup
Thanks, I managed to get all the variables via
However, I can't manage to find all the PsiExpressions that assign a new value/reference to a variable as mentioned above. To be more particular, I can indeed find those expressions, but I have no reference to the PsiVariable that has been assigned a new value when I look for those expressions.
I pointed to the visitor which could be used in another way, e.g.
Great thanks for the quick help!
My problem is now that I don't know how to retrieve a list of PsiVariables that match a specific pattern, for example variables that have been assigned a new value 4 times or something similar. Plus visitAssignmentExpression does not get called each time a variable is assigned a new value.
Could you please make it a little bit clearer for me, I'm very new to the PSI concept.
Thanks!
visitAssignment is called for assignment x = 0; if you have int x = 0; then it's variable declaration and is covered by visitVariable. You may see the structure of PSI if you call Tools | View PSI Structure
Thanks! I just have looked into the PSI Structure as you mentioned. But how can I find out whether a value was assigned to a variable on declaration, in other words how do i distinguish between
int x = 5;
and
int x;
And how do I get all the PsiVariables that match a specific behavior? E.g. a variable was never assignes a value or similar?
Since I am unable to retrieve all variables with a visitor (or is that somehow possible?), I have obtained all variables and all method parameters as follows:
Now I want for example to find out which variables of the array could be made final (by looking into their assignment history or whatever. This is where I am stuck. I just can't figure out how to determien this.
Can anyone help?
Having a reference of a PsiVariable, how can I find all the usages of this PsiVariable?
implement these extension points:
Thanks for the lead!
Can you please give me a short description what each of those Interfaces/Classes does? I can't seem to find any documentation on all of this, so I'm basically looking into the source code but that is not very helpful.
How do I make use of all this?
try adding these extension points in plugin.xml
Idea will suggest fields to be completed. There may be implementation property. Type in any existing (or a new placeholder blank) class. Idea will complain that this class does not implements interface ...
Then you'd need to implement that interface Idea complains about. Some methods are kind of self-explanatory. Some are not.
Try this first. Write if you run into problems.
> I'm basically looking into the source code but that is not very helpful.
It takes a lot of time - months - to get it kind of working. Be prepared.
Is all of this really necessary to find the usage of PsiVariables? It seems like a lot of overhead (?)
I have found all the variables in my currently selected .java file and now I want to find where they are used in this .java file and get the type of usage.
By the way, thanks for your help!
:)
you are welcome.
if you work with Java, things probably are a bit easier. I don't work with Java plugins though.
I assumed you work on custom language plugin.
Oh ok. Yes I work with Java alright. Any hints are welcome.
So what I now tried is to ReferenceSearch the variables I found earlier like this:
This returns the references/usages of the requested variable. Now I can call getParent() on the returned references so that I can check for example if the usage of the variable is located within a PsiExpressionStatement. This feels a bit hacky, though. Is there a clean way to determine this?
Call ReferencesSearch.search(variable) - it would find all references on this variable in it's scope (in java and other supported languages). Please check https://github.com/JetBrains/intellij-sdk-docs for samples/how-tos