IDEA plugin: PersistentStateComponent not saved xml

I can not force my plugin to keep its state. The configProvider.xml file is never created, and the @State annotation has no effect (apparently).

This is an important part in the plugin.xml file.

 <extensions defaultExtensionNs="com.intellij"> <applicationService serviceImplementation="my.plugins.idea.vcs.ConfigProvider" serviceInterface="my.plugins.idea.vcs.ConfigProvider"/> </extensions> 

This is the class that provides the object to be persisted:

 import com.intellij.openapi.components.PersistentStateComponent; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; import java.util.LinkedHashMap; @State( name = "ConfigProvider", storages = { @Storage(id = "main", file = "$APP_CONFIG$/configProvider.xml") } ) public class ConfigProvider implements PersistentStateComponent<ConfigProvider.State> { private State state = new State(); class State { public State() {} public LinkedHashMap<String, String> commitTypes = null; public Integer selectedDefaultCommitTypeIndex = null; public String jiraApiUrl; public String jiraAuthorization; public String jiraFilterId; } public static ConfigProvider getInstance() { return ServiceManager.getService(ConfigProvider.class); } @Override @org.jetbrains.annotations.Nullable public ConfigProvider.State getState() { return state; // gets called regulary } @Override public void loadState(ConfigProvider.State state) { this.state = state; // not called at all } } 

What am I missing?

Thanks Christopher

+1
source share
3 answers

Does this help make the State class public and static?

 public static class State { //... } 
0
source

On the Constant Status of Components page:

Note that extension instances cannot maintain their state by implementing PersistentStateComponent

Therefore, your first attempt did not work because the component was enumerated as part of the <extensions> . The second version works because the component is now registered inside <application-components> It might be worth deleting the extension section to make the answer more accurate.

+3
source

Now this is a working example:

 package com.foo import com.intellij.openapi.components.ApplicationComponent; import com.intellij.openapi.components.PersistentStateComponent; import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; import org.jetbrains.annotations.NotNull; @State( name = "ConfigProviderState", storages = { @Storage(id = "other", file = "$APP_CONFIG$/configProvider.xml") } ) public class ConfigProvider implements ApplicationComponent, PersistentStateComponent<ConfigProvider.State> { @NotNull @Override public String getComponentName() { return getClass().getSimpleName(); } @Override public void disposeComponent() {} @Override public void initComponent() {} public State state = new State(); public static class State { public State() {} public String foo; } @Override @org.jetbrains.annotations.Nullable public ConfigProvider.State getState() { return state; //Saves all public variables to disk. } @Override public void loadState(ConfigProvider.State state) { this.state = state; //restores state from disk } public String getFoo() { if (this.state.foo == null) { this.state.foo = "https://jira.rz.is/rest/api/2/"; } return this.state.foo; } public void setFoo(String foo) { this.state.foo = foo; } } 

plugin.xml

 <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> <applicationService serviceImplementation="com.foo.ConfigProvider" serviceInterface="com.foo.ConfigProvider" overrides="true" /> </extensions> <application-components> <component> <implementation-class>com.foo.ConfigProvider</implementation-class> <interface-class>com.foo.ConfigProvider</interface-class> </component> </application-components> <project-components> <!-- Add your project components here --> </project-components 
+2
source

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


All Articles