Intellij IDEA plugin - PersistentStateComponent loadState not called

I am trying to develop a plugin for Intellij IDEA, I am working with SDK 129.451.

The problem is that I cannot save user data, like some list items that it can enter into the plugin, and return data after restarting the IDE.

I am using PersistentStateComponent to save data, the getState() method getState() be called, but the loadState() method does not work.

Here is an example of a class that extends PersistentStateComponent:

  @State(name = "Test", storages = {@Storage(file = StoragePathMacros.APP_CONFIG+"/other.xml" )}) public class Test implements PersistentStateComponent<Element> { String ceva; public Test() { ceva = "sad"; System.out.println("constr"); } public String getCeva() { return ceva; } public void setCeva(String ceva) { this.ceva = ceva; } public void loadState(Element state) { System.out.println("cstate load"); ceva = (String) state.getContent().get(0); } public Element getState() { System.out.println("cstate retu"); Element configurationsElement = new Element("testtt"); configurationsElement.addContent(ceva); return configurationsElement; } } 

I also added this class to plugin.xml here:

 <extensions defaultExtensionNs="com.intellij"> <applicationService serviceImplementation="ro.catalin.prata.testflightuploader.controller.Test"/> <!-- Add your extensions here --> <toolWindow id="TF Uploader" secondary="true" icon="/general/add.png" anchor="right" factoryClass="ro.catalin.prata.testflightuploader.view.TFUploader"> </toolWindow> </extensions> 

And I also have a toolbox class:

 public class TFUploader implements ToolWindowFactory { private JButton buttonAction; private ToolWindow myToolWindow; final Test test = ServiceManager.getService(Test.class); public TFUploader() { // I assume it should print the saved string but it doesn't System.out.println(test.getCeva()); buttonAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // if I click a button I am setting some new value to the string I want to save test.setCeva(test.getCeva() + "-dddddd+"); } }); } 

So, if I close the application or hide it, the getState method will be called as I expected. but when I open the application, the loadState method will not be called .. can someone help me, how can I solve this?

I already read this one , but it doesn’t seem to help me much. I also want to use PersistentStateComponent , because I want to keep objects more complex than a simple string.

Thank you in advance!

+6
source share
3 answers

Ok, I did it! :)

I don’t know exactly what the problem is, but I changed the Test class to this:

 @State( name = "Test", storages = { @Storage( id = "other", file = "$APP_CONFIG$/testpersist.xml") }) public class Test implements PersistentStateComponent<Test> { String ceva; public Test() { ceva = "sad"; System.out.println("constr"); } public String getCeva() { return ceva; } public void setCeva(String ceva) { this.ceva = ceva; } public void loadState(Test state) { System.out.println("cstate load"); XmlSerializerUtil.copyBean(state, this); } public Test getState() { System.out.println("cstate retu"); return this; } } 

And in TFUploader, I changed the way the Test class is loaded:

 final Test test = ServiceManager.getService(Test.class); 

I hope this helps others.

+6
source

I already commented here , but again I will say that in my case loadState (state MyService) was not called due to the lack of a getter and setter for stateValue in this example:

 class MyService implements PersistentStateComponent<MyService> { public String stateValue; public MyService getState() { return this; } public void loadState(MyService state) { XmlSerializerUtil.copyBean(state, this); } } 
+1
source

In my case, I was getting a NullPointerException before calling loadState . Like your code above, I used the Element class as a state class. I had a constructor with some parameters in the Element class. This was a problem since the structure could not instantiate my state class. I tried to add an empty constructor without any parameters. It worked.

0
source

Source: https://habr.com/ru/post/946726/


All Articles