Get outer class's this from PsiElement

Hello guys:
     I want to developer a postfix template plugin to generate some codes.
     E.g: if I input ["hello".m]  ,it will generate [Bar.m(this,"hello") ]
     the first arg is a reference of outer class.
     so there are the following two cases:

  1.      generate code at outer class scope
  2.      generate code at inner class scope

     I need to get the outer PsiClass,but now I only hold the PsiElement of "hello"
     How can I get the PsiClass from psiElement?

Thanks



public class Foo{

     protected void m(){
            //1) Generate Bar.m(this,"hello")  here

            new AnnoClass(){
                    public void a(){
                         //2) Generate Bar.m(Foo.this,"hello")  here
                    }
           }
     }

}

2 comments
Comment actions Permalink

Hello Wei,

with these PsiElement methods:

getContext()
getParent()
getChildren()
getPrevSibling()
getNextSibling()

you can traverse across Psi tree.

see if this works for you.

0
Comment actions Permalink

Call PsiTreeUtil.getParentOfType(element, PsiClass.class) twice (the first call will give you the inner class, the second will give you the outer class).

1

Please sign in to leave a comment.