From dbe7bfa15fcb3ec40031131f35708ca0cf21ae93 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Tue, 20 Nov 2018 17:20:09 -0800 Subject: [PATCH] Use faster isAnnotationPresent check Followup from https://github.com/square/moshi/pull/749#discussion_r235224202 --- .../main/java/com/squareup/moshi/ClassJsonAdapter.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/moshi/src/main/java/com/squareup/moshi/ClassJsonAdapter.java b/moshi/src/main/java/com/squareup/moshi/ClassJsonAdapter.java index 5ed6d2b..3f64c74 100644 --- a/moshi/src/main/java/com/squareup/moshi/ClassJsonAdapter.java +++ b/moshi/src/main/java/com/squareup/moshi/ClassJsonAdapter.java @@ -73,13 +73,18 @@ final class ClassJsonAdapter extends JsonAdapter { 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())) { + try { + //noinspection unchecked if the Class.forName works, the cast will work. + Class metadataClass = + (Class) Class.forName("kotlin.Metadata"); + if (rawType.isAnnotationPresent(metadataClass)) { 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."); } + } catch (ClassNotFoundException ignored) { + } ClassFactory classFactory = ClassFactory.get(rawType);