Get inner classe(s) of anoynmous class

i am using the following code to get anonymous classes. is there a way to get nested inner classes of the anonymous ones?


final PsiElement[] classes = PsiTreeUtil.collectElements(psiElement, new PsiElementFilter() {
public boolean isAccepted(final PsiElement e) {
return (e instanceof PsiAnonymousClass) && psiElement.equals(PsiTreeUtil.getParentOfType(e, PsiClass.class));
}
});

Edited by: andrep-do on Nov 9, 2008 3:47 AM

5 comments
Comment actions Permalink

PsiClass.getInnerClasses

andrep-do wrote:

i using the following code to get anonymous classes. is there a way to get nested inner classes of the anonymous ones?


final PsiElement[] classes = PsiTreeUtil.collectElements(psiElement, new PsiElementFilter() {
public boolean isAccepted(final PsiElement e) {
return (e instanceof PsiAnonymousClass) && psiElement.equals(PsiTreeUtil.getParentOfType(e, PsiClass.class));
}
});



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

in this case
---
final PsiElement[] classes = PsiTreeUtil.collectElements(psiElement, new PsiElementFilter() {
public boolean isAccepted(final PsiElement e) {
return (e instanceof PsiAnonymousClass) && psiElement.equals(PsiTreeUtil.getParentOfType(e, PsiClass.class));
}
});

---

((PsiClass) classes[0]).getInnerClasses()

does not return any existing inner classes. what's my fault?

thanks

Edited by: andrep-do on Nov 9, 2008 11:19 PM

0
Comment actions Permalink

asuming having something like:

public void myMethod() {
EventQueue.invokeLater(new Runnable() {
public void run() {

class MyClass {

}
}
});
}


how do i get MyClass

0
Comment actions Permalink

PsiClass.getInnerClasses() doesn't work because MyClass is not an inner class of the anonymous class but of the run method inside the anonymous class. So in order to get MyClass you can use the same construct again:

final PsiElement[] classes = PsiTreeUtil.collectElements(psiElement, new PsiElementFilter() {
public boolean isAccepted(final PsiElement e) {
return (e instanceof PsiAnonymousClass) && psiElement.equals(PsiTreeUtil.getParentOfType(e, PsiClass.class));
}
});
final PsiElement[] inner = PsiTreeUtil.collectElements(classes[0], new PsiElementFilter() {
public boolean isAccepted(final PsiElement e) {
return (e instanceof PsiClass) && classes[0].equals(PsiTreeUtil.getParentOfType(e, PsiClass.class));
}
});

0

Please sign in to leave a comment.