How to get the object or variable within a particular method.


Hi, developers. I am a beginner in plugins and I am now carrying out an analysis of the variables and objects that appear in a particular method. Now that I can lock onto the PsiMethod I need, all I need to do is find the objects and variables that appear in that method and analyse them.
For example,

public String A (){
AClass aClass = new AClass();
String name = "Plugins";
return name;

Whether there is a way to get a reference to this "aClass" object and a reference to the "name" variable. So that I can call them up and analyse them (which Class it belongs to or the organisational structure).

By the way I would be interested to know if there is a way to statically simulate the amount of data (storage size or memory footprint) to be found for this object and variable.As there is no runtime state involved for now, I'm going to statically mock it up and provide the developer with an approximate amount of data.
This is very important to me, so if you know of any, please let me know.

Comment actions Permalink

You'll need to drill down into com.intellij.psi.PsiMethod#getBody to inspect actual elements inside the method. One way is to pass com.intellij.psi.JavaElementVisitor and process/collect elements.

Comment actions Permalink

Calculating object sizes at runtime is a far from trivial problem, and most probably not fully possible just from evaluating sources.

Comment actions Permalink

Thanks for your help, I will try that way.


Please sign in to leave a comment.