How to get current class's parent class, parent parent class roro co 创建于 2015年12月17日 14:16 for example:class Act extends ActionBarActivity {} public class Container extends Act {//I'm in here}How to know current class(Container) is ActionBarActivity?
com.intellij.psi.PsiClass#getContainingClass, then com.intellij.psi.util.InheritanceUtil#isInheritor(com.intellij.psi.PsiClass, java.lang.String)
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?
What is the invocation context of your code?
In android activity inner class
No, I mean in what place of your plugin's code: highlighting, intention, action, ...?
In aciton like "view > context info"
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) { ....}
}
Your code get null, here is my solution, it work
//ed: currrent editor
//pj: current project
PsiElement container = null;
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);
}