mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 07:59:21 +08:00
Refuse kotlin classes in ClassJsonAdapter (#749)
* Refuse kotlin classes in ClassJsonAdapter * Fail() * Tweak message
This commit is contained in:
@@ -952,4 +952,16 @@ class KotlinJsonAdapterTest {
|
||||
assertThat(adapter.fromJson("null")).isNull()
|
||||
assertThat(adapter.toJson(null)).isEqualTo("null")
|
||||
}
|
||||
|
||||
@Test fun kotlinClassesWithoutAdapterAreRefused() {
|
||||
val moshi = Moshi.Builder().build()
|
||||
try {
|
||||
moshi.adapter<PlainKotlinClass>(PlainKotlinClass::class.java)
|
||||
fail("Should not pass here")
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessageContaining("Reflective serialization of Kotlin classes")
|
||||
}
|
||||
}
|
||||
|
||||
class PlainKotlinClass
|
||||
}
|
||||
|
@@ -75,6 +75,14 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
||||
if (Modifier.isAbstract(rawType.getModifiers())) {
|
||||
throw new IllegalArgumentException("Cannot serialize abstract class " + rawType.getName());
|
||||
}
|
||||
for (Annotation annotation : rawType.getDeclaredAnnotations()) {
|
||||
if ("kotlin.Metadata".equals(annotation.annotationType().getName())) {
|
||||
throw new IllegalArgumentException("Cannot serialize Kotlin type " + rawType.getName()
|
||||
+ ". Reflective serialization of Kotlin classes without using kotlin-reflect has "
|
||||
+ "undefined and unexpected behavior. Please use KotlinJsonAdapter from the "
|
||||
+ "moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.");
|
||||
}
|
||||
}
|
||||
|
||||
ClassFactory<Object> classFactory = ClassFactory.get(rawType);
|
||||
Map<String, FieldBinding<?>> fields = new TreeMap<>();
|
||||
|
Reference in New Issue
Block a user