Access to PSI Tree in a PsiTreeChangesListener

Hi,

What do I have an access to in a PSI tree change listeners? The motivation for my question is this: I have a listener and in the beforeChildReplacement() method I'm calling PsiTreeUtils.getParentOfType() on the child to be replaced. However, it sometimes results in an error because the element I get from getParentOfType() is not a valid PSIElement (it doesn't have modifiers list for example, I checked that the element isn't null).

4 comments

The PSI tree should be in a valid state at that moment, and there should be no limitations on accessing it. Note that PsiModifierListOwner.getModifierList() is @Nullable and documented as such, so there is nothing invalid about this method returning null.

0

Thanks for the answer. You're right that PsiModifierListOwner.getModifierList() is @Nullable, however, PsiMethodImpl.getModifierList() is @NotNull.

This is the exception that I'm getting
java.lang.IllegalStateException: @NotNull method com/intellij/extapi/psi/StubBasedPsiElementBase.getRequiredStubOrPsiChild must not return null
 at com.intellij.extapi.psi.StubBasedPsiElementBase.getRequiredStubOrPsiChild(StubBasedPsiElementBase.java:305)
 at com.intellij.psi.impl.source.PsiMethodImpl.getModifierList(PsiMethodImpl.java:250)

0

This looks strange. Do you have a test project and a scenario that we could use to reproduce the problem?

0

Thanks for the answer. Here is what I am doing:
I have my PsiTreeChangeListener, in the beforeChildRemoval() method I do this:

PsiMethod removedMethod = PsiTreeUtil.getParentOfType(event.getChild(), PsiMethod.class, false);

and then call removedMethod.getModifierList() on removedMethod and it throws IllegalStateException with the message I have already posted in previous posts.

Do you have any idea why it is failing?

0

Please sign in to leave a comment.