XmlSerializerUtil "Unable to serialize state" for PersistentStateComponent

Hello, 

I am writing a class that implements PersistentStateComponent that tries to make highlighters persistent through sessions. 

@State(
name = "highlights",
storages = {
@Storage("highlights.xml")
}
)

public class PersistentHighlightsRepository implements PersistentStateComponent<PersistentHighlightsRepository> {
@Nullable
@Override
@Transient
public PersistentHighlightsRepository getState() {
System.out.println("Saved state");
return this;
}

@Override
@Transient
public void loadState(@NotNull PersistentHighlightsRepository state) {
XmlSerializerUtil.copyBean(state, this);
}

/*-------Class begins------*/
public Map<String, Set<RangeHighlighter>> highlighters = new HashMap<String, Set<RangeHighlighter>>();

public static PersistentHighlightsRepository getInstance(Project project) {
return ServiceManager.getService(project, PersistentHighlightsRepository.class);
}

@Transient
public void addHighlightToStorage(String filePath, RangeHighlighter highlighter) {
highlighters.computeIfAbsent(filePath, k -> new HashSet<RangeHighlighter>());
highlighters.get(filePath).add(highlighter);
}

@Transient
public void removeHighlightFromStorage(String filePath, RangeHighlighter highlighter) {
Set<RangeHighlighter> rHighlighters = highlighters.get(filePath);
if (rHighlighters != null && rHighlighters.contains(highlighter)) {
highlighters.get(filePath).remove(highlighter);
}
}

@Transient
public Set<RangeHighlighter> getFileHighlighters(String path) {
Set<RangeHighlighter> rHighlighters = highlighters.get(path);

return highlighters != null ? highlighters : new HashSet<>();
}

Currently when it calls getState() it will output this error:

com.intellij.util.xmlb.XmlSerializationException: Can't serialize instance of class components.PersistentHighlightsRepository

I have tried wrapping the RangedHighlighters in a serializable class but that didn't seem to work. 

Here is the relevant plugin.xml bit:

<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<applicationService
serviceInterface="components.PersistentHighlightsRepository"
serviceImplementation="components.PersistentHighlightsRepository" />

</extensions>
0
4 comments

Is there empty constructor for `RangeHighlighter` class? Maybe there is full error text with details?

0
Avatar
Permanently deleted user

RangeHighlighter is an interface and it uses RangeHighlighterImpl which does have a constructor. 

I never explicitly calls the constructor for RangeHighlighter because I get the return value from doing

editor.getMarkupModel().addLineHighlighter(...)

Here is the stack trace that IntelliJ is showing me:


at com.intellij.util.Alarm$Request.runSafely(Alarm.java:405)
at com.intellij.util.Alarm$Request.access$700(Alarm.java:330)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:371)
at com.intellij.openapi.application.TransactionGuardImpl$2.run(TransactionGuardImpl.java:315)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.doRun(LaterInvocator.java:447)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.runNextEvent(LaterInvocator.java:431)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:415)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:762)
at java.awt.EventQueue.access$500(EventQueue.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:715)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:732)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:779)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:720)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:395)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassCastException: com.intellij.openapi.editor.impl.RangeHighlighterImpl cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1294)
at java.util.TreeMap.put(TreeMap.java:538)
at java.util.TreeSet.add(TreeSet.java:255)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
at java.util.TreeSet.addAll(TreeSet.java:312)
at java.util.TreeSet.<init>(TreeSet.java:160)
at com.intellij.util.xmlb.CollectionBinding.getIterable(CollectionBinding.java:40)
at com.intellij.util.xmlb.AbstractCollectionBinding.serialize(AbstractCollectionBinding.java:116)
at com.intellij.util.xmlb.MapBinding.serializeKeyOrValue(MapBinding.java:209)
at com.intellij.util.xmlb.MapBinding.serialize(MapBinding.java:96)
at com.intellij.util.xmlb.OptionTagBinding.serialize(OptionTagBinding.java:62)
at com.intellij.util.xmlb.BasePrimitiveBinding.serialize(BasePrimitiveBinding.java:41)
at com.intellij.util.xmlb.BeanBinding.serializeInto(BeanBinding.java:103)
at com.intellij.util.xmlb.BeanBinding.serialize(BeanBinding.java:68)
at com.intellij.configurationStore.XmlSerializer.serialize(xmlSerializer.kt:51)
... 47 more
0

`com.intellij.openapi.editor.impl.RangeHighlighterImpl` is not serializable. I suggest you to write own simple POJO structure.

1
Avatar
Permanently deleted user

Oh I see. Thank you. 

0

Please sign in to leave a comment.