Get the class of a PSI element

Answered

Hello!

I am trying to listen to changes in a project and log what PSI class the change belongs to. I am implementing the PsiTreeChangeListener interface and it works as expected until I try to get the class by using something like "String className = event.getElement().getClass().getName();". It seems like when I add that it ruins the method, as even if I don't use "className" it wont run anything in the method after it. What could the issue be? Thanks! 

4 comments
Comment actions Permalink

Hi,

Please be more specific.

What do you want to achieve? What information are you trying to retrieve? What is the full implementation? How is it registered? What is the calling context (steps to reproduce)? What does it mean that it "ruins" the method? It's impossible to help you with so few details.

0
Comment actions Permalink

Hi, when a change is made in a project, I want to see what kind of psi element it belongs to. For example if I make a changes to a comment I want retrieve that it was a comment that was edited. I am creating class called "CustomDocumentListener" which implements the PsiTreeChangeListener interface as seen here:

public class CustomDocumentListener implements PsiTreeChangeListener {
 
I then override the methods for example my childrenChanged method looks like this:
 
@Override
public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
String className = event.getElement().getClass().getName();
logWriter.writeToJson("childrenChanged", "childrenChanged", className);
}
 
The logWriter is just a class that helps me write to a JSON file and take 3 strings as parameters. What I mean with that it ruins the method is that if I don´t have "event.getElement().getClass().getName();" the method runs fine and childrenChanged is called and writes to the JSON file as expected. But if I use "String className = event.getElement().getClass().getName();", even if I just send a string such as "name" instead of className to "writeToJson", just by having that line in the method the childrenChanged method doesn't seem to run.
 
I have registered this in the plugin.xml file by using:
<extensions defaultExtensionNs="com.intellij">
<psi.treeChangeListener implementation="se.ch.project.timeTool.CustomDocumentListener"/>
0
Comment actions Permalink

Hi,

Thanks for clarifying.

Unfortunately, I don't see what could be the issue's cause.

Is there anything in the idea.log in the IDE you test the plugin in?

If not, please share your sources (or minimal plugin example) I can use to reproduce the problem, with the example project and clear steps to reproduce. Please make sure all works without any additional steps like building dependencies, configuring repositories, etc.

0
Comment actions Permalink

Hi again,

I solved the issue by using the "getChild()" methods instead. For example these code lines work:

String className = event.getChild().getClass().getName();
String className = event.getOldChild().getClass().getName();
String className = event.getNewChild().getClass().getName();

which is sufficient for my goal. I am still not sure why

event.getElement().getClass().getName();

does not work. There is not anything related to it in the idea.log file and I wont be able to provide a minimal plugin example right now. But either way the issue is solved. Thanks for your time! :)

0

Please sign in to leave a comment.