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>
Please sign in to leave a comment.
Is there empty constructor for `RangeHighlighter` class? Maybe there is full error text with details?
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
Here is the stack trace that IntelliJ is showing me:
`com.intellij.openapi.editor.impl.RangeHighlighterImpl` is not serializable. I suggest you to write own simple POJO structure.
Oh I see. Thank you.