mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Use faster isAnnotationPresent check
Followup from https://github.com/square/moshi/pull/749#discussion_r235224202
This commit is contained in:
@@ -73,13 +73,18 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
|||||||
if (Modifier.isAbstract(rawType.getModifiers())) {
|
if (Modifier.isAbstract(rawType.getModifiers())) {
|
||||||
throw new IllegalArgumentException("Cannot serialize abstract class " + rawType.getName());
|
throw new IllegalArgumentException("Cannot serialize abstract class " + rawType.getName());
|
||||||
}
|
}
|
||||||
for (Annotation annotation : rawType.getDeclaredAnnotations()) {
|
try {
|
||||||
if ("kotlin.Metadata".equals(annotation.annotationType().getName())) {
|
//noinspection unchecked if the Class.forName works, the cast will work.
|
||||||
|
Class<? extends Annotation> metadataClass =
|
||||||
|
(Class<? extends Annotation>) Class.forName("kotlin.Metadata");
|
||||||
|
if (rawType.isAnnotationPresent(metadataClass)) {
|
||||||
throw new IllegalArgumentException("Cannot serialize Kotlin type " + rawType.getName()
|
throw new IllegalArgumentException("Cannot serialize Kotlin type " + rawType.getName()
|
||||||
+ ". Reflective serialization of Kotlin classes without using kotlin-reflect has "
|
+ ". Reflective serialization of Kotlin classes without using kotlin-reflect has "
|
||||||
+ "undefined and unexpected behavior. Please use KotlinJsonAdapter from the "
|
+ "undefined and unexpected behavior. Please use KotlinJsonAdapter from the "
|
||||||
+ "moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.");
|
+ "moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.");
|
||||||
}
|
}
|
||||||
|
} catch (ClassNotFoundException ignored) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ClassFactory<Object> classFactory = ClassFactory.get(rawType);
|
ClassFactory<Object> classFactory = ClassFactory.get(rawType);
|
||||||
|
Reference in New Issue
Block a user