【问题标题】:IDEA Plugin: PersistentStateComponent not persisting xmlIDEA 插件:PersistentStateComponent 不持久化 xml
【发布时间】:2015-09-23 11:51:18
【问题描述】:

我无法让我的插件保持其状态。 configProvider.xml 文件永远不会被创建,@State 注释也没有任何效果(显然)。

这是plugin.xml中的相关部分

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

这是提供应该被持久化的对象的类:

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
  }

}

我错过了什么?

谢谢, 克里斯托弗

【问题讨论】:

  • 这对您来说效果如何?你有没有在下面尝试我的答案。

标签: java xml intellij-idea intellij-plugin


【解决方案1】:

这是一个工作示例:

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;
  }

}

插件.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】:

    页面Persisting State of Components有重要说明:

    请注意,扩展实例无法通过实现 PersistentStateComponent 来保持其状态

    因此,由于组件被列为&lt;extensions&gt; 的一部分,因此您的第一次尝试没有成功。 第二个版本有效,因为该组件现在已在 &lt;application-components&gt; 中注册 可能值得删除extension 部分以使答案更准确。

    【讨论】:

    • 这是正确答案。如果您的扩展需要具有持久状态,那么您需要定义一个单独的服务来负责管理该状态。
    【解决方案3】:

    这是否有助于使 State 类公开和静态?

    public static class State {
        //...
    }
    

    【讨论】:

    • 如果您的更改最终使其运行,我无法精确重现,但它现在正在运行。由于我重构了一些东西,这可能不是我唯一需要改变的东西。但我接受了你的回答,并附上了工作示例作为新答案。谢谢。
    • 太好了,希望它会在未来帮助其他人。 :)
    • 是的,特别是因为除了官方的例子实现之外很难找到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多