nullInMap — if type is not-null, set to first enum value

This commit is contained in:
Vladimir Krivosheev
2017-02-03 14:33:16 +01:00
parent 5c270bbcb0
commit a3c8b568b5
3 changed files with 113 additions and 16 deletions
@@ -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.
@@ -25,6 +25,8 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import static com.intellij.util.xmlb.Binding.LOG;
class PropertyAccessor implements MutableAccessor {
private final String myName;
private final Class<?> myType;
@@ -72,7 +74,26 @@ class PropertyAccessor implements MutableAccessor {
throw new XmlSerializationException(e);
}
catch (InvocationTargetException e) {
throw new XmlSerializationException(e);
Throwable cause = e.getCause();
// see KotlinXmlSerializerTest.nullInMap
if (cause instanceof IllegalArgumentException && myGenericType instanceof Class && ((Class)myGenericType).isEnum() && cause.getMessage().contains("Parameter specified as non-null is null:")) {
Object[] constants = ((Class)myGenericType).getEnumConstants();
if (constants.length > 0) {
try {
LOG.warn("Cannot set enum value, will be set to first enum value", e);
myWriteMethod.invoke(host, constants[0]);
return;
}
catch (IllegalAccessException e1) {
throw new XmlSerializationException(e);
}
catch (InvocationTargetException e1) {
throw new XmlSerializationException(cause);
}
}
}
throw new XmlSerializationException(cause);
}
}
@@ -0,0 +1,90 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util.xmlb
import com.intellij.util.loadElement
import com.intellij.util.xmlb.annotations.MapAnnotation
import com.intellij.util.xmlb.annotations.Tag
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.util.*
class KotlinXmlSerializerTest {
@Test fun internalVar() {
@Tag("bean")
class Foo {
internal var PLACES_MAP = ""
}
val data = Foo()
data.PLACES_MAP = "new"
doSerializerTest("""
<bean>
<option name="PLACES_MAP" value="new" />
</bean>""", data)
}
@Test fun nullInMap() {
@Tag("bean")
class Foo {
@MapAnnotation(surroundWithTag = false, surroundKeyWithTag = false, surroundValueWithTag = false)
var PLACES_MAP: TreeMap<String, PlaceSettings> = TreeMap()
}
val data = Foo()
data.PLACES_MAP.put("", PlaceSettings())
doSerializerTest("""
<bean>
<option name="PLACES_MAP">
<entry key="">
<PlaceSettings>
<option name="IGNORE_POLICY" value="DEFAULT" />
</PlaceSettings>
</entry>
</option>
</bean>""", data)
assertThat(XmlSerializer.deserialize(loadElement("""<bean>
<option name="PLACES_MAP">
<entry key="">
<PlaceSettings>
<option name="IGNORE_POLICY" />
</PlaceSettings>
</entry>
</option>
</bean>"""), Foo::class.java)!!.PLACES_MAP.get("")!!.IGNORE_POLICY).isEqualTo(IgnorePolicy.DEFAULT)
assertThat(XmlSerializer.deserialize(loadElement("""<bean>
<option name="PLACES_MAP">
<entry key="">
<PlaceSettings>
<option name="SOME_UNKNOWN_VALUE" />
</PlaceSettings>
</entry>
</option>
</bean>"""), Foo::class.java)!!.PLACES_MAP.get("")!!.IGNORE_POLICY).isEqualTo(IgnorePolicy.DEFAULT)
}
}
private enum class IgnorePolicy constructor(val text: String) {
DEFAULT("Do not ignore"),
TRIM_WHITESPACES("Trim whitespaces"),
IGNORE_WHITESPACES("Ignore whitespaces"),
IGNORE_WHITESPACES_CHUNKS("Ignore whitespaces and empty lines"),
FORMATTING("Ignore formatting");
}
private data class PlaceSettings(var IGNORE_POLICY: IgnorePolicy = IgnorePolicy.DEFAULT)
@@ -61,20 +61,6 @@ internal class BeanWithProperty {
}
internal class XmlSerializerTest {
@Test fun internalVar() {
@Tag("bean")
class Foo {
internal var PLACES_MAP = ""
}
val data = Foo()
data.PLACES_MAP = "new"
doSerializerTest("""
<bean>
<option name="PLACES_MAP" value="new" />
</bean>""", data)
}
@Test fun annotatedInternalVar() {
@Tag("bean")
class Foo {