Java IDEA插件:PersistentStateComponent不持久化xml
我无法使我的插件保持其状态。 永远不会创建configProvider.xml文件,Java IDEA插件:PersistentStateComponent不持久化xml,java,xml,intellij-idea,intellij-plugin,Java,Xml,Intellij Idea,Intellij Plugin,我无法使我的插件保持其状态。 永远不会创建configProvider.xml文件,@State注释也没有任何效果(显然) 这是plugin.xml中的相关部分 <extensions defaultExtensionNs="com.intellij"> <applicationService serviceImplementation="my.plugins.idea.vcs.ConfigProvider" serviceInterface="my.plugins.i
@State
注释也没有任何效果(显然)
这是plugin.xml中的相关部分
<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="my.plugins.idea.vcs.ConfigProvider" serviceInterface="my.plugins.idea.vcs.ConfigProvider"/>
</extensions>
<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
这是提供应持久化的对象的类:
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
}
}
导入com.intellij.openapi.components.PersistentStateComponent;
导入com.intellij.openapi.components.ServiceManager;
导入com.intellij.openapi.components.State;
导入com.intellij.openapi.components.Storage;
导入java.util.LinkedHashMap;
@陈述(
name=“ConfigProvider”,
存储={
@存储(id=“main”,file=“$APP\u CONFIG$/configProvider.xml”)
}
)
公共类ConfigProvider实现PersistentStateComponent{
私有状态=新状态();
阶级国家{
公共状态(){}
public LinkedHashMap commitTypes=null;
公共整数selectedDefaultCommitTypeIndex=null;
公共字符串jiraapirl;
公共字符串授权;
公共字符串jiraFilterId;
}
公共静态ConfigProvider getInstance(){
返回ServiceManager.getService(ConfigProvider.class);
}
@凌驾
@org.jetbrains.annotations.Nullable
public ConfigProvider.State getState(){
返回状态;//定期调用
}
@凌驾
公共无效加载状态(ConfigProvider.State状态){
this.state=state;//根本不调用
}
}
我错过了什么
谢谢,
Christopher这是否有帮助,使
状态成为公共和静态的
public static class State {
//...
}
这是一个工作示例:
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;
}
}
package com.foo
导入com.intellij.openapi.components.ApplicationComponent;
导入com.intellij.openapi.components.PersistentStateComponent;
导入com.intellij.openapi.components.State;
导入com.intellij.openapi.components.Storage;
导入org.jetbrains.annotations.NotNull;
@陈述(
name=“ConfigProviderState”,
存储={
@存储(id=“其他”,file=“$APP\u CONFIG$/configProvider.xml”)
}
)
公共类ConfigProvider实现ApplicationComponent、PersistentStateComponent{
@NotNull
@凌驾
公共字符串getComponentName(){
返回getClass().getSimpleName();
}
@凌驾
public void disposeComponent(){}
@凌驾
public void initComponent(){}
公共状态=新状态();
公共静态类状态{
公共状态(){}
公共字符串foo;
}
@凌驾
@org.jetbrains.annotations.Nullable
public ConfigProvider.State getState(){
返回状态;//将所有公共变量保存到磁盘。
}
@凌驾
公共无效加载状态(ConfigProvider.State状态){
this.state=state;//从磁盘恢复状态
}
公共字符串getFoo(){
if(this.state.foo==null){
this.state.foo=”https://jira.rz.is/rest/api/2/";
}
返回this.state.foo;
}
公共void setFoo(字符串foo){
this.state.foo=foo;
}
}
plugin.xml
<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="my.plugins.idea.vcs.ConfigProvider" serviceInterface="my.plugins.idea.vcs.ConfigProvider"/>
</extensions>
<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
com.foo.ConfigProvider
com.foo.ConfigProvider
页面上有重要注释:
请注意,扩展实例无法通过实现PersistentStateComponent
因此,您的第一次尝试没有成功,因为组件是作为
的一部分登记的。
第二个版本可以工作,因为组件现在在
为了使答案更精确,可能需要删除扩展部分部分。这对您来说是如何实现的?您是否尝试了我下面的答案。如果您的更改最终使其运行,我无法精确复制,但它现在正在运行。自从我重构了一些东西之后,这可能不是我唯一需要改变的东西。但我接受了你的答案,并附上了工作示例作为新答案。谢谢。太好了,希望它将来能帮助其他人。:)是的,特别是因为除了官方的实现之外,很难找到示例实现。这是正确的答案。如果您的扩展需要具有持久状态,那么您需要定义一个单独的服务来负责管理该状态。