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!

16 comments
Comment actions Permalink

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 

final VariablesProcessor variablesProcessor = new VariablesProcessor(false) {
@Override
protected boolean check(PsiVariable var, ResolveState state) {
return PsiTreeUtil.isAncestor(forStatement.getInitialization(), var, true);
}
};
PsiScopesUtil.treeWalkUp(variablesProcessor, expr, null);
0
Comment actions Permalink

Thanks, I managed to get all the variables via

PsiElement[] variables = PsiTreeUtil.collectElements(psiClass, new PsiElementFilter() {
public boolean isAccepted(PsiElement e) {
if (e instanceof PsiVariable) {
return true;
}
return false;
}
});

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.

0
Comment actions Permalink

I pointed to the visitor which could be used in another way, e.g.

psiClass.accept(new com.intellij.psi.JavaRecursiveElementVisitor() {

public void visitVariable(PsiVariable var) {

//get your var here

}

public void visitAssignmentExpression(PsiAssignmentExpression expression) {
//your code here
}
})

 

  

 

0
Comment actions Permalink

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!

0
Comment actions Permalink

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

0
Comment actions Permalink

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?

0
Comment actions Permalink

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:

final PsiElement[] vars = PsiTreeUtil.collectElements(classes[0], new PsiElementFilter() {
public boolean isAccepted(PsiElement e) {
if (e instanceof PsiVariable) {
return true;
}
return false;
}
});

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?

0
Comment actions Permalink

Having a reference of a PsiVariable, how can I find all the usages of this PsiVariable?

0
Comment actions Permalink

implement these extension points:

  • referencesSearch
  • lang.findUsagesProvider
  • findUsagesHandlerFactory
0
Comment actions Permalink

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?

0
Comment actions Permalink

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.

0
Comment actions Permalink

> 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.

0
Comment actions Permalink

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!

0
Comment actions Permalink

:)

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.

0
Comment actions Permalink

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:

ReferencesSearch.search(variable).findAll()

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?

0
Comment actions Permalink

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

0

Please sign in to leave a comment.