How to get current class's parent class, parent parent class

for example:

class Act extends ActionBarActivity {}

 
public class Container extends Act  {

//I'm in here
}

How to know current class(Container) is ActionBarActivity?

9 comments
Comment actions Permalink

com.intellij.psi.PsiClass#getContainingClass, then com.intellij.psi.util.InheritanceUtil#isInheritor(com.intellij.psi.PsiClass, java.lang.String)

0
Comment actions Permalink

Can you tell me how to get current class according current cursor in editor, I know "view > context info" can do that, how to do it in openapi?

0
Comment actions Permalink

What is the invocation context of your code?


0
Comment actions Permalink

In android activity inner class

0
Comment actions Permalink

No, I mean in what place of your plugin's code: highlighting, intention, action, ...?


0
Comment actions Permalink

In aciton like "view > context info"

0
Comment actions Permalink

Something like

  @Override
  public void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
    PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
    if (psiClass != null) { ....}
  }

0
Comment actions Permalink

Your code get null, here is my solution, it work

//ed: currrent editor
//pj: current project
PsiElement container = null;

StructureViewBuilder builder = LanguageStructureViewBuilder.INSTANCE.getStructureViewBuilder(PsiUtilBase.getPsiFileInEditor(ed, pj));
if (builder instanceof TreeBasedStructureViewBuilder) {
    StructureViewModel range = ((TreeBasedStructureViewBuilder) builder).createStructureViewModel(ed);
    boolean _container = true;

    if (container == null) {
        _container = false;
        Object element = range.getCurrentEditorElement();
        if (element instanceof PsiElement) {
            container = (PsiElement) element;
        }
    }
}
container
0
Comment actions Permalink

Please do not use this solution, here's an improved version of my previous one working in more places:

      final Editor editor = e.getData(CommonDataKeys.EDITOR);
      if (editor != null) {
        final PsiElement currentElement = psiFile.findElementAt(editor.getCaretModel().getOffset());
        final PsiClass enclosingClass = PsiTreeUtil.getParentOfType(currentElement, PsiClass.class);
      }

0

Please sign in to leave a comment.