JDOMExternalizable and PersistentStateComponent

I was using JDOMExternalizable to serialize plugin config till 6.0. How do I port that to use the new PersistentStateComponent ? I'm confused with its usage.

Previously I had:
class MyProjectComponent implements ProjectComponent, Configurable, JDOMExternalizable
List data1; Map data2; //I need to persist data1 and data2 ... readExternal(Element element) .. writeExternal() ... I tried changing it to: class MyProjectComponent implements ProjectComponent, Configurable, PersistentStateComponent]]>
..
public PersistentData getState() {
final PersistentData pd = new PersistentData();
for (String item : data1) {
pd.getData1().add(item);
}
for (String key : data2.keySet()) {
pd.getData2().put(key, data2.get(key));
}
return pd;
}
..

class PersistentData just contains data1 and data2. However, I'm getting the following exception:
java.lang.NullPointerException
at com.intellij.openapi.components.impl.stores.ComponentStoreImpl.c(ComponentStoreImpl.java:47)
at com.intellij.openapi.components.impl.stores.ComponentStoreImpl.a(ComponentStoreImpl.java:83)
at com.intellij.openapi.components.impl.stores.ComponentStoreImpl.access$000(ComponentStoreImpl.java:104)
at com.intellij.openapi.components.impl.stores.ComponentStoreImpl$1.run(ComponentStoreImpl.java:4)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:210)
at com.intellij.openapi.components.impl.stores.ComponentStoreImpl.initComponent(ComponentStoreImpl.java:19)
at com.intellij.openapi.components.impl.ComponentManagerImpl.a(ComponentManagerImpl.java:7)
at com.intellij.openapi.components.impl.ComponentManagerImpl.access$1100(ComponentManagerImpl.java:64)
at com.intellij.openapi.components.impl.ComponentManagerImpl$ComponentConfigComponentAdapter$1.getComponentInstance(ComponentManagerImpl.java:4)
at com.intellij.openapi.components.impl.ComponentManagerImpl$ComponentConfigComponentAdapter.getComponentInstance(ComponentManagerImpl.java:13)
at com.intellij.util.pico.DefaultPicoContainer.getLocalInstance(DefaultPicoContainer.java:199)
at com.intellij.util.pico.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:186)
at com.intellij.util.pico.DefaultPicoContainer.getComponentInstance(DefaultPicoContainer.java:168)
at org.picocontainer.alternatives.AbstractDelegatingMutablePicoContainer.getComponentInstance(AbstractDelegatingMutablePicoContainer.java:75)
at com.intellij.openapi.components.impl.ComponentManagerImpl.a(ComponentManagerImpl.java:167)
at com.intellij.openapi.components.impl.ComponentManagerImpl.a(ComponentManagerImpl.java:136)
at com.intellij.openapi.components.impl.ComponentManagerImpl.initComponents(ComponentManagerImpl.java:145)
at com.intellij.openapi.components.impl.ComponentManagerImpl.init(ComponentManagerImpl.java:139)
at com.intellij.openapi.project.impl.ProjectImpl.init(ProjectImpl.java:110)
at com.intellij.openapi.components.impl.stores.ProjectStoreImpl.loadProject(ProjectStoreImpl.java:168)
at com.intellij.openapi.project.impl.ProjectManagerImpl.a(ProjectManagerImpl.java:148)
at com.intellij.openapi.project.impl.ProjectManagerImpl.access$200(ProjectManagerImpl.java:245)
at com.intellij.openapi.project.impl.ProjectManagerImpl$4.run(ProjectManagerImpl.java:2)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:10)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:25)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:36)
at com.intellij.openapi.application.impl.ApplicationImpl$7$1.run(ApplicationImpl.java:12)
at com.intellij.openapi.application.impl.ApplicationImpl$5.run(ApplicationImpl.java:9)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:3)
ERROR - ij.components.ComponentManager -

Please help.
bodhi

1 comment

Hey Bodhi,

I am not sure of the specifics of your problem but I was having some trouble with this earlier today and thought I would share what I have found perhaps it could be of some use to you. This is an example from my project... In your case the file value would be "$PROJECT_FILE$" not "MODULE_FILE$". I am far from an expert so there may be a better way of doing this. Hope it helps though :)

Cheers,

Nick

@State(
name = "WOModuleSettings",
storages = {
@Storage(
id = "woms" ,
file = "$MODULE_FILE$"
)}
)
public class WOModuleSettings implements ModuleComponent, PersistentStateComponent]]> {

public boolean isFramework;
public String resourceDir;

public static WOModuleSettings getInstance(Module m) {
return ServiceManager.getService( m, WOModuleSettings.class );
}

...

public WOModuleSettings getState() {
return this;
}

public void loadState(WOModuleSettings woModuleSettings) {
resourceDir = woModuleSettings.resourceDir;
isFramework = woModuleSettings.isFramework;
}

}

0

Please sign in to leave a comment.