Listening to new class events

I was hoping to use the PsiManager.addPsiTreeChangeListener to listen to class creation(or any other PsiFile) events.

The problem is that the childAdded method in the listeners are getting called before i can access it's contents. What this means is that i just can't get any basic information about the created class like it's package.

((PsiJavaFile) event.getChild()).getPackageName() always returns null.

Am i doing something wrong ? Is there any other type of listener i can use to get this done ?



Correction, ((PsiJavaFile) event.getChild()).getPackageName() return empty string.


Hello Hugo,

Correction, ((PsiJavaFile) event.getChild()).getPackageName() return
empty string.

Is that the first file in the package?

Dmitry Jemerov
Development Lead
JetBrains, Inc.
"Develop with Pleasure!"


No, there's more files in the same package.


Should it make a difference ?


I've searched and searched and can't find a solution for this.

Can anyone please help me ? There's got to be someone out there that has a similar use case working.


Sorry to bump this again but's it's just that this should be simple and i just can't a find a way to do it....

All i want is to listen to new file events and be able to access the file contents when it happens.


Please sign in to leave a comment.