Support non-reified type parameters in typeOf in JVM and JVM_IR

#KT-30279 Fixed
This commit is contained in:
Alexander Udalov
2020-01-31 14:13:11 +01:00
committed by Alexander Udalov
parent 6fb40878c4
commit 0ce16b9d8c
46 changed files with 1748 additions and 122 deletions
@@ -50,6 +50,17 @@ internal class KTypeParameterImpl(override val descriptor: TypeParameterDescript
override fun hashCode() =
descriptor.hashCode()
// TODO: this temporarily duplicates TypeParameterReference.toString to prevent JPS build failure until stdlib is bootstrapped
override fun toString() =
ReflectionObjectRenderer.renderTypeParameter(descriptor)
// TypeParameterReference.toString(this)
buildString {
when (variance) {
KVariance.INVARIANT -> {
}
KVariance.IN -> append("in ")
KVariance.OUT -> append("out ")
}
append(name)
}
}
@@ -28,7 +28,7 @@ import java.util.List;
/**
* @suppress
*/
@SuppressWarnings({"UnusedDeclaration", "unchecked"})
@SuppressWarnings({"UnusedDeclaration", "unchecked", "rawtypes"})
public class ReflectionFactoryImpl extends ReflectionFactory {
@Override
public KClass createKotlinClass(Class javaClass) {
@@ -123,6 +123,28 @@ public class ReflectionFactoryImpl extends ReflectionFactory {
return KClassifiers.createType(klass, arguments, isMarkedNullable, Collections.<Annotation>emptyList());
}
// TODO: this is a temporary workaround for the JPS build
// @Override
public KTypeParameter typeParameter(Object container, String name, KVariance variance, boolean isReified) {
List<KTypeParameter> typeParameters;
if (container instanceof KClass) {
typeParameters = ((KClass<?>) container).getTypeParameters();
} else if (container instanceof KCallable) {
typeParameters = ((KCallable<?>) container).getTypeParameters();
} else {
throw new IllegalArgumentException("Type parameter container must be a class or a callable: " + container);
}
for (KTypeParameter typeParameter : typeParameters) {
if (typeParameter.getName().equals(name)) return typeParameter;
}
throw new IllegalArgumentException("Type parameter " + name + " is not found in container: " + container);
}
// @Override
public void setUpperBounds(KTypeParameter typeParameter, List<KType> bounds) {
// Do nothing. KTypeParameterImpl implementation will load upper bounds from the metadata.
}
// Misc
public static void clearCaches() {
@@ -16,10 +16,12 @@
package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance
import kotlin.reflect.KParameter
internal object ReflectionObjectRenderer {
@@ -105,19 +107,6 @@ internal object ReflectionObjectRenderer {
}
}
fun renderTypeParameter(typeParameter: TypeParameterDescriptor): String {
return buildString {
when (typeParameter.variance) {
Variance.INVARIANT -> {
}
Variance.IN_VARIANCE -> append("in ")
Variance.OUT_VARIANCE -> append("out ")
}
append(typeParameter.name)
}
}
fun renderType(type: KotlinType): String {
return renderer.renderType(type)
}