Skip to content

Latest commit

 

History

History
84 lines (62 loc) · 3.46 KB

persistent_state_components.md

File metadata and controls

84 lines (62 loc) · 3.46 KB

PersistentStateComponents

Official Plugin SDK documentation: Persisting State of Components

Convert class to PersistentStateComponent

According to the Implementing the PersistentStateComponent Interface section there are two ways to specify the object that will store the state of the component, whose implementation this intention action helps with.

There are dedicated actions for converting a target Java or Kotlin class to implement PersistentStateComponent, and generate a simple implementation of it by

  1. using a separate inner class for storing the component state

    See example...

    From:

    public final class SomeService {
    }

    To:

    @State(name = "SomeService", storages = @Storage("<storage name>"))
    public final class SomeService implements PersistentStateComponent<SomeService.State> {
    
        private State myState = new State();
    
        @Override
        public State getState() {
            return myState;
        }
    
        @Override
        public void loadState(State state) {
            myState = state;
        }
    
        static final class State {
    
        }
    }
  2. using the component class itself for storing the state

    See example...

    From:

    public final class SomeService {
    }

    To:

    @State(name = "SomeService", storages = @Storage("<storage name>"))
    public final class SomeService implements PersistentStateComponent<SomeService> {
    
        @Override
        public SomeService getState() {
            return this;
        }
    
        @Override
        public void loadState(SomeService state) {
            XmlSerializerUtil.copyBean(state, this);
        }
    }
Separate state object Component class as state
convert_class_to_persistent_state_component_standalone_state convert_class_to_persistent_state_component_self_as_state

The generated code is based on the aforementioned SDK document section, and is available when the following conditions are met:

  • the class is not an enum, an interface, or an abstract class (or a value class either, in case of a Kotlin class)
  • the class doesn't already implement PersistentStateComponent