How to find inner / anonymous java classes in project by compiled name

Answered

Given a compiled fully qualified class name, I would like to find the corresponding psi element.

For non-compiled classes,

JavaPsiFacade.getInstance(project).findClass(name, GlobalSearchScope.allScope(project))

would do what I want; however it doesn't handle compiled class names (those containing dollar signs for inner classes). Simply replacing dollar signs with periods won't find the numerically indexed anonymous inner classes. Is there a way to do that?

0
5 comments

See com.intellij.psi.util.ClassUtil#findPsiClass() and related

0

That was exactly it. Thanks!

0

One more thing; is there possibly anything similar that would help me locate the definition of a lambda or method reference from its `class.getName()` representation?

0

There is no reliable mapping for these, it might even depend on the JVM version. You can try and map these by manually navigating the PSI tree and see if it matches the "guess".

0

For anyone wondering why this doesn't work in some cases, you should be aware that `ClassUtil.findPsiClass` currently don't find anonymous classes on libraries (or the JVM runtime).

See 

0

Please sign in to leave a comment.