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())) {
throw new IllegalArgumentException("Cannot serialize abstract class " + rawType.getName());
}
try {
//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()
+ ". 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) {
if (Util.isKotlin(rawType)) {
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);

View File

@@ -35,7 +35,6 @@ import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.NoSuchElementException;
import java.util.Set;
@@ -49,15 +48,18 @@ public final class Util {
public static final Set<Annotation> NO_ANNOTATIONS = Collections.emptySet();
public static final Type[] EMPTY_TYPE_ARRAY = new Type[] {};
@Nullable private static final Class<?> DEFAULT_CONSTRUCTOR_MARKER;
@Nullable private static final Class<? extends Annotation> METADATA;
static {
Class<?> clazz;
Class<? extends Annotation> metadata = null;
Class<?> defaultConstructorMarker = null;
try {
clazz = Class.forName("kotlin.jvm.internal.DefaultConstructorMarker");
} catch (ClassNotFoundException e) {
clazz = null;
metadata = (Class<? extends Annotation>) Class.forName("kotlin.Metadata");
defaultConstructorMarker = Class.forName("kotlin.jvm.internal.DefaultConstructorMarker");
} catch (ClassNotFoundException ignored) {
}
DEFAULT_CONSTRUCTOR_MARKER = clazz;
METADATA = metadata;
DEFAULT_CONSTRUCTOR_MARKER = defaultConstructorMarker;
}
private Util() {
@@ -171,7 +173,7 @@ public final class Util {
}
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,
@@ -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.
*