Implement typeOf intrinsic on JVM

#KT-29915 Fixed
This commit is contained in:
Alexander Udalov
2019-02-21 19:47:45 +01:00
parent 5d297c40fd
commit d1e33534db
28 changed files with 950 additions and 4 deletions
@@ -6,8 +6,12 @@
package kotlin.jvm.internal;
import kotlin.SinceKotlin;
import kotlin.collections.ArraysKt;
import kotlin.reflect.*;
import java.util.Arrays;
import java.util.Collections;
/**
* This class serves as a facade to the actual reflection implementation. JVM back-end generates calls to static methods of this class
* on any reflection-using construct.
@@ -105,4 +109,46 @@ public class Reflection {
public static KMutableProperty2 mutableProperty2(MutablePropertyReference2 p) {
return factory.mutableProperty2(p);
}
// typeOf
@SinceKotlin(version = "1.4")
public static KType typeOf(Class klass) {
return factory.typeOf(getOrCreateKotlinClass(klass), Collections.<KTypeProjection>emptyList(), false);
}
@SinceKotlin(version = "1.4")
public static KType typeOf(Class klass, KTypeProjection arg1) {
return factory.typeOf(getOrCreateKotlinClass(klass), Collections.singletonList(arg1), false);
}
@SinceKotlin(version = "1.4")
public static KType typeOf(Class klass, KTypeProjection arg1, KTypeProjection arg2) {
return factory.typeOf(getOrCreateKotlinClass(klass), Arrays.asList(arg1, arg2), false);
}
@SinceKotlin(version = "1.4")
public static KType typeOf(Class klass, KTypeProjection... arguments) {
return factory.typeOf(getOrCreateKotlinClass(klass), ArraysKt.<KTypeProjection>toList(arguments), false);
}
@SinceKotlin(version = "1.4")
public static KType nullableTypeOf(Class klass) {
return factory.typeOf(getOrCreateKotlinClass(klass), Collections.<KTypeProjection>emptyList(), true);
}
@SinceKotlin(version = "1.4")
public static KType nullableTypeOf(Class klass, KTypeProjection arg1) {
return factory.typeOf(getOrCreateKotlinClass(klass), Collections.singletonList(arg1), true);
}
@SinceKotlin(version = "1.4")
public static KType nullableTypeOf(Class klass, KTypeProjection arg1, KTypeProjection arg2) {
return factory.typeOf(getOrCreateKotlinClass(klass), Arrays.asList(arg1, arg2), true);
}
@SinceKotlin(version = "1.4")
public static KType nullableTypeOf(Class klass, KTypeProjection... arguments) {
return factory.typeOf(getOrCreateKotlinClass(klass), ArraysKt.<KTypeProjection>toList(arguments), true);
}
}
@@ -8,6 +8,8 @@ package kotlin.jvm.internal;
import kotlin.SinceKotlin;
import kotlin.reflect.*;
import java.util.List;
public class ReflectionFactory {
private static final String KOTLIN_JVM_FUNCTIONS = "kotlin.jvm.functions.";
@@ -73,4 +75,11 @@ public class ReflectionFactory {
public KMutableProperty2 mutableProperty2(MutablePropertyReference2 p) {
return p;
}
// typeOf
@SinceKotlin(version = "1.4")
public KType typeOf(KClassifier klass, List<KTypeProjection> arguments, boolean isMarkedNullable) {
return new TypeReference(klass, arguments, isMarkedNullable);
}
}
@@ -0,0 +1,70 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.jvm.internal
import kotlin.reflect.*
@SinceKotlin("1.4")
@Suppress("NEWER_VERSION_IN_SINCE_KOTLIN", "API_NOT_AVAILABLE" /* See KT-30129 */) // TODO: remove this in 1.4
public class TypeReference(
override val classifier: KClassifier,
override val arguments: List<KTypeProjection>,
override val isMarkedNullable: Boolean
) : KType {
override val annotations: List<Annotation>
get() = emptyList()
override fun equals(other: Any?): Boolean =
other is TypeReference &&
classifier == other.classifier && arguments == other.arguments && isMarkedNullable == other.isMarkedNullable
override fun hashCode(): Int =
(classifier.hashCode() * 31 + arguments.hashCode()) * 31 + isMarkedNullable.hashCode()
override fun toString(): String =
asString() + Reflection.REFLECTION_NOT_AVAILABLE
private fun asString(): String {
val javaClass = (classifier as? KClass<*>)?.java
val klass = when {
javaClass == null -> classifier.toString()
javaClass.isArray -> javaClass.arrayClassName
else -> javaClass.name
}
val args =
if (arguments.isEmpty()) ""
else arguments.joinToString(", ", "<", ">") { it.asString() }
val nullable = if (isMarkedNullable) "?" else ""
return klass + args + nullable
}
private val Class<*>.arrayClassName
get() = when (this) {
BooleanArray::class.java -> "kotlin.BooleanArray"
CharArray::class.java -> "kotlin.CharArray"
ByteArray::class.java -> "kotlin.ByteArray"
ShortArray::class.java -> "kotlin.ShortArray"
IntArray::class.java -> "kotlin.IntArray"
FloatArray::class.java -> "kotlin.FloatArray"
LongArray::class.java -> "kotlin.LongArray"
DoubleArray::class.java -> "kotlin.DoubleArray"
else -> "kotlin.Array"
}
// TODO: this should be the implementation of KTypeProjection.toString, see KT-30071
@Suppress("NO_REFLECTION_IN_CLASS_PATH")
private fun KTypeProjection.asString(): String {
if (variance == null) return "*"
val typeString = (type as? TypeReference)?.asString() ?: type.toString()
return when (variance) {
KVariance.INVARIANT -> typeString
KVariance.IN -> "in $typeString"
KVariance.OUT -> "out $typeString"
}
}
}
@@ -0,0 +1,12 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.reflect
@Suppress("unused") // KT-12448
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun <reified T> typeOf(): KType =
throw UnsupportedOperationException("This function is implemented as an intrinsic on all supported platforms.")