PsiElement reference needed inside JavaWalkingVisitor

Hi

In my inspection whenever I visit a class method (PsiMethod) I end up creating a JavaRecursiveElementWalkingVisitor to which I pass a reference of the parent method element.

So far I haven't encountered bugs with my inspection but should there be any concerns with storing PsiElement references inside a JavaRecursiveElementWalkingVisitor that is invoked right after creation?

I know PsiElement references may be invalidated after a file is parsed again, but since my walking visitor is called on the children of a PsiElement as I create it I thought it's highly the reference would be invalidated before the visitor finished visiting.

I also came across the SmartPsiElementPointer class, but I was unsure about the impact on performance of my analysis when many such pointers are created. 

Best,
Radu

0
1 comment
Official comment

If you create and use your visitor inside the same read/write action (which you most likely do, see also http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/general_threading_rules.html), there's nothing to worry about: PSI won't change, unless you change it yourself inside the visitor. In the latter case, I'd recommend to use visitor to collect elements to change, and then modify them after visiting.

SmartPsiElementPointer-s are for cases when you expect the PSI to change (e.g. between read actions). Performance-wise, they consume some memory and some CPU time on each reparse. Not worth noting when there are several of them, but might be noticeable when there are thousands of smart pointers.

Please sign in to leave a comment.