JavaPsiFacade.findClass() occasionally returns null (?)

hello,
I've noticed that on some occasions (cannot pinpoint exact 'context' of a method call) JavaPsiFacade.findClass() method returns null - even though the class searched for is available on active module's classpath.
What could be the reason for such behaviour ?
At first I thought that the cause could have been IDEA's 'dumb mode' but findClass() called from within DumbModeListener interface implementation returned the class in question properly.
Also - findClass() method implementation seems to suggest that the method is 'dumb-proof' and should return viable results even when IDEA's indices are being recalculated.
Another hypothesis was that my first sweep through the current project's file hierarchy might have been launched too soon (when the project has not been initialized yet) but I first call 'rebuildStructures' (my parsing method) inside runWhenProjectIsInitialized() so it shouldn't be a problem.
Thanks in advance for any clarification,
regards
Simon

6 comments

If the scope passed to findClass() is correct, and there are no exceptions, then the only reason I can imagine is outdated indices.

0

thanks for a quick response.
so what would you suggest : should I just check if the retrieved PsiClass reference is null and act accordingly or should I additionally wrap findClass() method call in DumbService.runWhenSmart() / invoke it after DumbService.waitForSmart() to ensure that the indices are up-to-date ?

0

I cannot really suggest anything since I don't know enough details - what's the class you are looking for, where is it located, in what context findClass() is called etc.

0

actually I was hoping for some kind of general 'best practice' regarding findClass() method usage (if there is any..). For example that it's safe to use findClass() method on its own - without any additional 'magic' involving checking for dumb mode or waiting for smart one :)
So I'll just assume that this method is 'self-contained' and as marked with @Nullable - should be just treated with caution.
Thank you for your assistance (and patience for noobish questions;) )
Simon

0

Well, then right, no special precautions needed, it may be called safely as it is.
The null as a return value is perfectly legal, even if you're looking for something as basic as "java.lang.Object" with widest scope possible you may still found nothing - e.g. if JDK is not configured for a project.

0

thank you once again.
Points granted ;)

0

Please sign in to leave a comment.