common code extracted

This commit is contained in:
nik
2013-08-02 17:57:12 +04:00
parent ee51eccbf3
commit 9ea76a4ce9
6 changed files with 17 additions and 26 deletions
@@ -16,6 +16,10 @@
package com.intellij.openapi.components;
import com.intellij.util.ReflectionUtil;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.TypeVariable;
@@ -27,4 +31,11 @@ public class ComponentSerializationUtil {
TypeVariable<Class<PersistentStateComponent>> variable = PersistentStateComponent.class.getTypeParameters()[0];
return ReflectionUtil.getRawType(ReflectionUtil.resolveVariableInHierarchy(variable, aClass));
}
public static <S> void loadComponentState(@NotNull PersistentStateComponent<S> configuration, @Nullable Element element) {
if (element != null) {
Class<S> stateClass = getStateClass(configuration.getClass());
configuration.loadState(XmlSerializer.deserialize(element, stateClass));
}
}
}
@@ -79,8 +79,7 @@ public class FacetUtil {
throws InvalidDataException {
if (config != null) {
if (configuration instanceof PersistentStateComponent) {
Class<?> stateClass = ComponentSerializationUtil.getStateClass(((PersistentStateComponent<?>)configuration).getClass());
((PersistentStateComponent)configuration).loadState(XmlSerializer.deserialize(config, stateClass));
ComponentSerializationUtil.loadComponentState((PersistentStateComponent)configuration, config);
}
else {
configuration.readExternal(config);
@@ -270,9 +270,7 @@ public class LibraryImpl extends TraceableDisposable implements LibraryEx.Modifi
myProperties = myKind.createDefaultProperties();
final Element propertiesElement = element.getChild(PROPERTIES_ELEMENT);
if (propertiesElement != null) {
final Class<?> stateClass = ComponentSerializationUtil.getStateClass(myProperties.getClass());
//noinspection unchecked
myProperties.loadState(XmlSerializer.deserialize(propertiesElement, stateClass));
ComponentSerializationUtil.loadComponentState(myProperties, propertiesElement);
}
}
@@ -104,16 +104,11 @@ public class RemoteServersManagerImpl extends RemoteServersManager implements Pe
private static <C extends ServerConfiguration> RemoteServerImpl<C> createConfiguration(ServerType<C> type, RemoteServerState server) {
C configuration = type.createDefaultConfiguration();
PersistentStateComponent<?> serializer = configuration.getSerializer();
loadConfiguration(server, serializer);
ComponentSerializationUtil.loadComponentState(serializer, server.myConfiguration);
return new RemoteServerImpl<C>(server.myName, type, configuration);
}
private static <S> void loadConfiguration(RemoteServerState server, PersistentStateComponent<S> serializer) {
S deserialize = (S)XmlSerializer.deserialize(server.myConfiguration, ComponentSerializationUtil.getStateClass(serializer.getClass()));
serializer.loadState(deserialize);
}
@Nullable
@Nullable
private static ServerType<?> findServerType(@NotNull String typeId) {
for (ServerType serverType : ServerType.EP_NAME.getExtensions()) {
if (serverType.getId().equals(typeId)) {
@@ -71,12 +71,7 @@ public class XBreakpointBase<Self extends XBreakpoint<P>, P extends XBreakpointP
myBreakpointManager = breakpointManager;
myProperties = type.createProperties();
if (myProperties != null) {
//noinspection unchecked
Element element = myState.getPropertiesElement();
if (element != null) {
//noinspection unchecked
myProperties.loadState(XmlSerializer.deserialize(element, ComponentSerializationUtil.getStateClass(myProperties.getClass())));
}
ComponentSerializationUtil.loadComponentState(myProperties, myState.getPropertiesElement());
}
}
@@ -76,18 +76,11 @@ public class XDebuggerSettingsManager implements PersistentStateComponent<XDebug
for (SpecificSettingsState settingsState : state.getSpecificStates()) {
XDebuggerSettings<?> settings = findSettings(settingsState.getId());
if (settings != null) {
loadState(settings, settingsState.getSettingsElement());
ComponentSerializationUtil.loadComponentState(settings, settingsState.getSettingsElement());
}
}
}
private static <T> void loadState(final XDebuggerSettings<T> settings, final Element settingsElement) {
Class stateClass = ComponentSerializationUtil.getStateClass(settings.getClass());
//noinspection unchecked
settings.loadState((T)XmlSerializer.deserialize(settingsElement, stateClass));
}
private XDebuggerSettings findSettings(String id) {
initSettings();
return mySettingsById.get(id);