Look up kotlin.Metadata::class only once

This commit is contained in:
Jesse Wilson
2019-10-26 23:59:27 -04:00
parent 537b22f1c3
commit 53b045eb86
2 changed files with 18 additions and 19 deletions

View File

@@ -79,18 +79,11 @@ 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());
} }
try { if (Util.isKotlin(rawType)) {
//noinspection unchecked if the Class.forName works, the cast will work. throw new IllegalArgumentException("Cannot serialize Kotlin type " + rawType.getName()
Class<? extends Annotation> metadataClass = + ". Reflective serialization of Kotlin classes without using kotlin-reflect has "
(Class<? extends Annotation>) Class.forName("kotlin.Metadata"); + "undefined and unexpected behavior. Please use KotlinJsonAdapter from the "
if (rawType.isAnnotationPresent(metadataClass)) { + "moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.");
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<Object> classFactory = ClassFactory.get(rawType); ClassFactory<Object> classFactory = ClassFactory.get(rawType);

View File

@@ -35,7 +35,6 @@ import java.lang.reflect.WildcardType;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Set; import java.util.Set;
@@ -49,15 +48,18 @@ public final class Util {
public static final Set<Annotation> NO_ANNOTATIONS = Collections.emptySet(); public static final Set<Annotation> NO_ANNOTATIONS = Collections.emptySet();
public static final Type[] EMPTY_TYPE_ARRAY = new Type[] {}; public static final Type[] EMPTY_TYPE_ARRAY = new Type[] {};
@Nullable private static final Class<?> DEFAULT_CONSTRUCTOR_MARKER; @Nullable private static final Class<?> DEFAULT_CONSTRUCTOR_MARKER;
@Nullable private static final Class<? extends Annotation> METADATA;
static { static {
Class<?> clazz; Class<? extends Annotation> metadata = null;
Class<?> defaultConstructorMarker = null;
try { try {
clazz = Class.forName("kotlin.jvm.internal.DefaultConstructorMarker"); metadata = (Class<? extends Annotation>) Class.forName("kotlin.Metadata");
} catch (ClassNotFoundException e) { defaultConstructorMarker = Class.forName("kotlin.jvm.internal.DefaultConstructorMarker");
clazz = null; } catch (ClassNotFoundException ignored) {
} }
DEFAULT_CONSTRUCTOR_MARKER = clazz; METADATA = metadata;
DEFAULT_CONSTRUCTOR_MARKER = defaultConstructorMarker;
} }
private Util() { private Util() {
@@ -171,7 +173,7 @@ public final class Util {
} }
public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) { public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
return resolve(context, contextRawType, toResolve, new HashSet<TypeVariable>()); return resolve(context, contextRawType, toResolve, new LinkedHashSet<TypeVariable>());
} }
private static Type resolve(Type context, Class<?> contextRawType, Type toResolve, private static Type resolve(Type context, Class<?> contextRawType, Type toResolve,
@@ -553,6 +555,10 @@ public final class Util {
} }
} }
public static boolean isKotlin(Class<?> targetClass) {
return METADATA != null && targetClass.isAnnotationPresent(METADATA);
}
/** /**
* Reflectively looks up the defaults constructor of a kotlin class. * Reflectively looks up the defaults constructor of a kotlin class.
* *