How to get the containing class of a member variable

Hi everyone

I'm working on a plugin (first attempt at an IntelliJ-plugin) that should find usages of certain methods and classes in a project - for example I want to find all places, where android.location.LocationManager is used. Now let's say I have code that reads


LocationManager lm = new LocationManager()

System.out.println(lm.GPS_PROVIDER)

How do I get the class of lm here on the second line?
I have a visitMethodCallExpression like this:

public void visitMethodCallExpression(PsiMethodCallExpression expression){
                PsiExpression[] exList = expression.getArgumentList().getExpressions();
                for (PsiExpression ex : exList) {
                    for (PsiElement param : ex.getChildren()) {
                        if
(ex.getType() instanceof PsiClassReferenceType) {
                            PsiClass aClass = ((PsiClassReferenceType) ex.getType()).resolve();
                            if
(aClass.getQualifiedName().equals("android.location.LocationManager")) { //or anything else, depending on what I'm looking for
                                //do something
                            }
                    
}
                }
            }

But the only classReferences I can pick up with this are of the member variables, not of the objects owning them (in the case above, I could only find the strings, not the LocationManager), because only the GPS_PROVIDER literal has a PsiClassReferenceType in the PsiTree. For the lm literal, I can't find anything that would reference to its class, LocationManager.

I figured out that I can use an InspectionTool, build a visitor and then  override various visitXXX-methods. I can also get the classes of  certain PsiElements, get the argumentList of a method and thus get the class of lm.GPS_PROVIDER (which is a String). But how can I get the class of the object where this string is a member of? And, once I have the class, how can I go about inspecting said class file?

Maybe as a sidenote: I encounter various issues working with this Psi* environment as I can't find any documentation and can only work by reverse engineering examples. But the matter at hand is quite complex (at least for me) and I have trouble understanding many (most probably) fundamental cornerstones and concepts. And it seems that there are multiple ways to achieve most goals, which makes it even harder to figure out what's what. Is there a way to get into this more efficiently than by reading thousands of lines of code and stepping through the debugger for days? Like a course? Someone I can hire to explain it to me over Skype? How do people go about learning this? To clarify: I spent the better part of two weeks so far trying to figure this stuff out - is it too ambitious to expect more or should I be able to solve this problem on my own by now given my time investment?

Cheers
Pip

Please sign in to leave a comment.