mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
serialization: kotlin data class full support (initial, enabled only in tests for now) — part 3 (test added for self class serialization)
This commit is contained in:
@@ -417,7 +417,7 @@ private fun checkSchemes(baseDir: Path, expected: String, ignoreDeleted: Boolean
|
||||
|
||||
baseDir.directoryStreamIfExists {
|
||||
for (file in it) {
|
||||
val scheme = deserialize(loadElement(file), TestScheme::class.java)
|
||||
val scheme = loadElement(file).deserialize(TestScheme::class.java)
|
||||
assertThat(fileToSchemeMap.get(FileUtil.getNameWithoutExtension(file.fileName.toString()))).isEqualTo(scheme.name)
|
||||
}
|
||||
}
|
||||
@@ -439,7 +439,7 @@ open class TestSchemesProcessor : LazySchemeProcessor<TestScheme, TestScheme>()
|
||||
name: String,
|
||||
attributeProvider: Function<String, String?>,
|
||||
isBundled: Boolean): TestScheme {
|
||||
val scheme = deserialize(dataHolder.read(), TestScheme::class.java)
|
||||
val scheme = dataHolder.read().deserialize(TestScheme::class.java)
|
||||
dataHolder.updateDigest(scheme)
|
||||
return scheme
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class StoredPropertyStateTest {
|
||||
assertThat(state).isNotEqualTo(AState())
|
||||
|
||||
assertThat(XmlSerializer.serialize(state)).isEqualTo("""<AState customName="foo" />""")
|
||||
assertThat(XmlSerializer.deserialize(loadElement("""<AState customName="foo" />"""), AState::class.java).languageLevel).isEqualTo("foo")
|
||||
assertThat(loadElement("""<AState customName="foo" />""").deserialize(AState::class.java).languageLevel).isEqualTo("foo")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -58,7 +58,7 @@ class KotlinXmlSerializerTest {
|
||||
</option>
|
||||
</bean>""", data)
|
||||
|
||||
assertThat(deserialize(loadElement("""<bean>
|
||||
assertThat(loadElement("""<bean>
|
||||
<option name="PLACES_MAP">
|
||||
<entry key="">
|
||||
<PlaceSettings>
|
||||
@@ -66,9 +66,9 @@ class KotlinXmlSerializerTest {
|
||||
</PlaceSettings>
|
||||
</entry>
|
||||
</option>
|
||||
</bean>"""), Foo::class.java).PLACES_MAP.get("")!!.IGNORE_POLICY).isEqualTo(IgnorePolicy.DEFAULT)
|
||||
</bean>""").deserialize<Foo>().PLACES_MAP.get("")!!.IGNORE_POLICY).isEqualTo(IgnorePolicy.DEFAULT)
|
||||
|
||||
val value = deserialize(loadElement("""<bean>
|
||||
val value = loadElement("""<bean>
|
||||
<option name="PLACES_MAP">
|
||||
<entry key="">
|
||||
<PlaceSettings>
|
||||
@@ -76,7 +76,7 @@ class KotlinXmlSerializerTest {
|
||||
</PlaceSettings>
|
||||
</entry>
|
||||
</option>
|
||||
</bean>"""), Foo::class.java)
|
||||
</bean>""").deserialize<Foo>()
|
||||
assertThat(value).isNotNull()
|
||||
val placeSettings = value.PLACES_MAP.get("")
|
||||
assertThat(placeSettings).isNotNull()
|
||||
|
||||
@@ -204,17 +204,15 @@ internal class XmlSerializerTest {
|
||||
})
|
||||
}
|
||||
|
||||
@Test fun `parallel deserialization 2`() {
|
||||
val e = Element("root").addContent(Element("name").setText("x"))
|
||||
assertConcurrent(*Array(5) {
|
||||
{
|
||||
for (i in 0..9) {
|
||||
val bean = e.deserialize<BeanWithFieldWithTagAnnotation>()
|
||||
assertThat(bean).isNotNull()
|
||||
assertThat(bean.STRING_V).isEqualTo("x")
|
||||
}
|
||||
}
|
||||
})
|
||||
class Complex {
|
||||
var foo: Complex? = null
|
||||
}
|
||||
|
||||
@Test fun `self class reference deserialization`() {
|
||||
doSerializerTest("""
|
||||
<Complex>
|
||||
<option name="foo" />
|
||||
</Complex>""", Complex())
|
||||
}
|
||||
|
||||
@Test fun fieldWithTagAnnotation() {
|
||||
@@ -661,7 +659,7 @@ internal fun <T: Any> doSerializerTest(@Language("XML") expectedText: String, be
|
||||
val element = assertSerializer(bean, expectedTrimmed, filter)
|
||||
|
||||
// test deserializer
|
||||
val o = deserialize(element, bean.javaClass)
|
||||
val o = element.deserialize(bean.javaClass)
|
||||
assertSerializer(o, expectedTrimmed, filter, "Deserialization failure")
|
||||
return o
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import com.intellij.openapi.components.PersistentStateComponent
|
||||
import org.jdom.Element
|
||||
|
||||
fun deserializeAndLoadState(component: PersistentStateComponent<*>, element: Element) {
|
||||
val state = deserialize(element, ComponentSerializationUtil.getStateClass<Any>(component.javaClass))
|
||||
val state = element.deserialize(ComponentSerializationUtil.getStateClass<Any>(component.javaClass))
|
||||
(state as? BaseState)?.resetModificationCount()
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(component as PersistentStateComponent<Any>).loadState(state)
|
||||
|
||||
@@ -13,11 +13,14 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
@file:JvmName("XmlSerializer")
|
||||
package com.intellij.configurationStore
|
||||
|
||||
import com.intellij.openapi.util.JDOMUtil
|
||||
import com.intellij.reference.SoftReference
|
||||
import com.intellij.util.xmlb.*
|
||||
import com.intellij.util.xmlb.XmlSerializerImpl.isPrimitive
|
||||
import com.intellij.util.xmlb.XmlSerializerImpl.typeToClass
|
||||
import gnu.trove.THashMap
|
||||
import org.jdom.Element
|
||||
import org.jdom.JDOMException
|
||||
@@ -31,12 +34,16 @@ import kotlin.reflect.primaryConstructor
|
||||
|
||||
fun <T : Any> T.serialize(filter: SerializationFilter? = SkipDefaultValuesSerializationFilters()): Element = XmlSerializer.serialize(this, filter)
|
||||
|
||||
inline fun <reified T: Any> Element.deserialize(): T = deserialize(this, T::class.java)
|
||||
fun serialize(`object`: Any): Element {
|
||||
return XmlSerializer.serialize(`object`)
|
||||
}
|
||||
|
||||
fun <T> deserialize(element: Element, aClass: Class<T>): T {
|
||||
inline fun <reified T: Any> Element.deserialize(): T = deserialize(T::class.java)
|
||||
|
||||
fun <T> Element.deserialize(aClass: Class<T>): T {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
try {
|
||||
return (getBinding(aClass) as NotNullDeserializeBinding).deserialize(null, element) as T
|
||||
return (getBinding(aClass) as NotNullDeserializeBinding).deserialize(null, this) as T
|
||||
}
|
||||
catch (e: XmlSerializationException) {
|
||||
throw e
|
||||
@@ -50,7 +57,7 @@ fun <T> deserialize(url: URL, aClass: Class<T>): T {
|
||||
try {
|
||||
var document = JDOMUtil.loadDocument(url)
|
||||
document = JDOMXIncluder.resolve(document, url.toExternalForm())
|
||||
return deserialize(document.rootElement, aClass)
|
||||
return document.rootElement.deserialize(aClass)
|
||||
}
|
||||
catch (e: IOException) {
|
||||
throw XmlSerializationException(e)
|
||||
@@ -104,6 +111,12 @@ private class KotlinAwareBeanBinding(beanClass: Class<*>, accessor: MutableAcces
|
||||
return instance
|
||||
}
|
||||
|
||||
override fun getBinding(accessor: MutableAccessor): Binding? {
|
||||
val type = accessor.genericType
|
||||
val aClass = typeToClass(type)
|
||||
return if (isPrimitive(aClass)) null else getBinding(aClass, type, accessor)
|
||||
}
|
||||
|
||||
private fun newInstance(): Any {
|
||||
val clazz = myBeanClass
|
||||
try {
|
||||
|
||||
@@ -410,8 +410,8 @@ public class BeanBinding extends NotNullDeserializeBinding {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Binding createBinding(@NotNull MutableAccessor accessor) {
|
||||
Binding binding = XmlSerializerImpl.getBinding(accessor);
|
||||
private Binding createBinding(@NotNull MutableAccessor accessor) {
|
||||
Binding binding = getBinding(accessor);
|
||||
if (binding instanceof JDOMElementBinding) {
|
||||
return binding;
|
||||
}
|
||||
@@ -450,4 +450,9 @@ public class BeanBinding extends NotNullDeserializeBinding {
|
||||
|
||||
return new OptionTagBinding(accessor, accessor.getAnnotation(OptionTag.class));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected Binding getBinding(@NotNull MutableAccessor accessor) {
|
||||
return XmlSerializerImpl.getBinding(accessor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class XmlSerializerImpl {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static Class<?> typeToClass(@NotNull Type type) {
|
||||
public static Class<?> typeToClass(@NotNull Type type) {
|
||||
if (type instanceof Class) {
|
||||
return (Class<?>)type;
|
||||
}
|
||||
@@ -97,7 +97,11 @@ public class XmlSerializerImpl {
|
||||
|
||||
@Nullable
|
||||
static synchronized Binding getClassBinding(@NotNull Class<?> aClass, @NotNull Type originalType, @Nullable MutableAccessor accessor) {
|
||||
if (aClass.isPrimitive() ||
|
||||
return isPrimitive(aClass) ? null : getMainBinding(aClass, originalType, accessor);
|
||||
}
|
||||
|
||||
public static boolean isPrimitive(@NotNull Class<?> aClass) {
|
||||
return aClass.isPrimitive() ||
|
||||
aClass == String.class ||
|
||||
aClass == Integer.class ||
|
||||
aClass == Long.class ||
|
||||
@@ -105,11 +109,7 @@ public class XmlSerializerImpl {
|
||||
aClass == Double.class ||
|
||||
aClass == Float.class ||
|
||||
aClass.isEnum() ||
|
||||
Date.class.isAssignableFrom(aClass)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getMainBinding(aClass, originalType, accessor);
|
||||
Date.class.isAssignableFrom(aClass);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -15,12 +15,12 @@
|
||||
*/
|
||||
package com.intellij.xdebugger;
|
||||
|
||||
import com.intellij.configurationStore.XmlSerializer;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.testFramework.TempFiles;
|
||||
import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters;
|
||||
import com.intellij.util.xmlb.XmlSerializer;
|
||||
import com.intellij.xdebugger.breakpoints.XBreakpoint;
|
||||
import com.intellij.xdebugger.breakpoints.XBreakpointType;
|
||||
import com.intellij.xdebugger.impl.breakpoints.XBreakpointBase;
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
*/
|
||||
package com.intellij.xdebugger;
|
||||
|
||||
import com.intellij.configurationStore.XmlSerializer;
|
||||
import com.intellij.testFramework.PlatformLiteFixture;
|
||||
import com.intellij.util.xmlb.XmlSerializer;
|
||||
import com.intellij.util.xmlb.annotations.Attribute;
|
||||
import com.intellij.xdebugger.impl.XDebuggerUtilImpl;
|
||||
import com.intellij.xdebugger.impl.settings.XDebuggerSettingManagerImpl;
|
||||
@@ -24,8 +24,6 @@ import com.intellij.xdebugger.settings.XDebuggerSettings;
|
||||
import com.intellij.xdebugger.settings.XDebuggerSettingsManager;
|
||||
import org.jdom.Element;
|
||||
|
||||
import static com.intellij.util.xmlb.XmlSerializer.deserialize;
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
@@ -53,12 +51,11 @@ public class XDebuggerSettingsTest extends PlatformLiteFixture {
|
||||
settings.myOption = "42";
|
||||
assertSame(settings, MyDebuggerSettings.getInstance());
|
||||
|
||||
settingsManager.loadState(deserialize(element, XDebuggerSettingManagerImpl.SettingsState.class));
|
||||
settingsManager.loadState(com.intellij.configurationStore.XmlSerializer.deserialize(element, XDebuggerSettingManagerImpl.SettingsState.class));
|
||||
assertSame(settings, MyDebuggerSettings.getInstance());
|
||||
assertEquals("239", settings.myOption);
|
||||
}
|
||||
|
||||
|
||||
public static class MyDebuggerSettings extends XDebuggerSettings<MyDebuggerSettings> {
|
||||
@Attribute("option")
|
||||
public String myOption;
|
||||
|
||||
+1
-1
@@ -16,9 +16,9 @@
|
||||
package org.jetbrains.idea.maven.execution;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.intellij.configurationStore.XmlSerializer;
|
||||
import com.intellij.openapi.options.ConfigurationException;
|
||||
import com.intellij.testFramework.IdeaTestCase;
|
||||
import com.intellij.util.xmlb.XmlSerializer;
|
||||
import org.jdom.Element;
|
||||
import org.jetbrains.idea.maven.project.MavenGeneralSettings;
|
||||
import org.jetbrains.idea.maven.server.MavenServerManager;
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.tasks;
|
||||
|
||||
import com.intellij.configurationStore.XmlSerializer;
|
||||
import com.intellij.notification.Notification;
|
||||
import com.intellij.notification.Notifications;
|
||||
import com.intellij.notification.NotificationsAdapter;
|
||||
@@ -25,7 +26,6 @@ import com.intellij.tasks.impl.TaskManagerImpl;
|
||||
import com.intellij.tasks.impl.TaskProjectConfiguration;
|
||||
import com.intellij.tasks.youtrack.YouTrackRepository;
|
||||
import com.intellij.tasks.youtrack.YouTrackRepositoryType;
|
||||
import com.intellij.util.xmlb.XmlSerializer;
|
||||
import org.jdom.Element;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,10 +15,10 @@
|
||||
*/
|
||||
package com.intellij.tasks;
|
||||
|
||||
import com.intellij.configurationStore.XmlSerializer;
|
||||
import com.intellij.openapi.util.JDOMUtil;
|
||||
import com.intellij.tasks.impl.TaskManagerImpl;
|
||||
import com.intellij.tasks.youtrack.YouTrackRepository;
|
||||
import com.intellij.util.xmlb.XmlSerializer;
|
||||
import org.jdom.Document;
|
||||
import org.jdom.Element;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user