Java Intellij IDEA插件-未调用PersistentStateComponent loadState
我正在尝试为Intellij IDEA开发一个插件,我正在使用SDK 129.451 我遇到的问题是,我不能像一些列表项那样持久化用户数据,他可以在插件中输入这些数据,并在IDE重新启动后将其返回 我使用PersistentStateComponent来持久化数据,似乎调用了Java Intellij IDEA插件-未调用PersistentStateComponent loadState,java,intellij-idea,plugins,persistence,intellij-plugin,Java,Intellij Idea,Plugins,Persistence,Intellij Plugin,我正在尝试为Intellij IDEA开发一个插件,我正在使用SDK 129.451 我遇到的问题是,我不能像一些列表项那样持久化用户数据,他可以在插件中输入这些数据,并在IDE重新启动后将其返回 我使用PersistentStateComponent来持久化数据,似乎调用了getState()方法,但是loadState()方法没有调用 下面是一个扩展PersistentStateComponent的示例类: @State(name = "Test", storages = {@Sto
getState()
方法,但是loadState()
方法没有调用
下面是一个扩展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;
}
}
<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>
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+");
}
});
}
好的,如果我关闭应用程序或最小化它,getState方法将按我预期的那样被调用。。但是当我打开应用程序时,loadState方法没有被调用。。有人能帮我解决这个问题吗
我已经读过了,但它似乎对我帮助不大。我还想使用PersistentStateComponent
,因为我想保存比简单字符串更复杂的对象
提前谢谢你 好的,我成功了!:)
我不知道到底是什么问题,但我将测试类更改为:
@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;
}
}
final Test test = ServiceManager.getService(Test.class);
我希望它对其他人有所帮助。我已经发表了评论,但我要再次指出,在我的例子中,loadState(MyService state)没有被调用,因为本例中缺少stateValue的getter和setter:
class MyService implements PersistentStateComponent<MyService> {
public String stateValue;
public MyService getState() {
return this;
}
public void loadState(MyService state) {
XmlSerializerUtil.copyBean(state, this);
}
}
classmyservice实现PersistentStateComponent{
公共字符串状态值;
公共MyService getState(){
归还这个;
}
公共void加载状态(MyService状态){
copyBean(state,this);
}
}
在我的例子中,甚至在调用loadState
之前,我就得到了一个NullPointerException
。与上面的代码类似,我使用了元素
类作为state类。我在元素
类中有一个带有一些参数的构造函数。这就是问题所在,因为框架无法创建我的state类的实例。我试图添加一个没有任何参数的空构造函数。这是有效的。我在这里补充了类似问题的答案:简而言之,这很可能是由于将您的组件登记到