Reformat reflection.jvm, fix inspections
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
@file:JvmName("KCallables")
|
||||
|
||||
package kotlin.reflect.full
|
||||
|
||||
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
|
||||
@@ -77,4 +78,4 @@ suspend fun <R> KCallable<R>.callSuspendBy(args: Map<KParameter, Any?>): R {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
if (returnType.classifier == Unit::class && !returnType.isMarkedNullable) return (Unit as R)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,7 @@ import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.internal.KCallableImpl
|
||||
import kotlin.reflect.jvm.internal.KClassImpl
|
||||
import kotlin.reflect.jvm.internal.KFunctionImpl
|
||||
import kotlin.reflect.jvm.internal.KTypeImpl
|
||||
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
|
||||
import kotlin.reflect.jvm.internal.*
|
||||
|
||||
/**
|
||||
* Returns the primary constructor of this class, or `null` if this class has no primary constructor.
|
||||
@@ -65,9 +61,12 @@ val KClass<*>.companionObjectInstance: Any?
|
||||
* Returns a type corresponding to the given class with type parameters of that class substituted as the corresponding arguments.
|
||||
* For example, for class `MyMap<K, V>` [defaultType] would return the type `MyMap<K, V>`.
|
||||
*/
|
||||
@Deprecated("This function creates a type which rarely makes sense for generic classes. " +
|
||||
@Suppress("DeprecatedCallableAddReplaceWith")
|
||||
@Deprecated(
|
||||
"This function creates a type which rarely makes sense for generic classes. " +
|
||||
"For example, such type can only be used in signatures of members of that class. " +
|
||||
"Use starProjectedType or createType() for clearer semantics.")
|
||||
"Use starProjectedType or createType() for clearer semantics."
|
||||
)
|
||||
@SinceKotlin("1.1")
|
||||
val KClass<*>.defaultType: KType
|
||||
get() = KTypeImpl((this as KClassImpl<*>).descriptor.defaultType) { jClass }
|
||||
@@ -191,27 +190,27 @@ val KClass<*>.superclasses: List<KClass<*>>
|
||||
@SinceKotlin("1.1")
|
||||
val KClass<*>.allSupertypes: Collection<KType>
|
||||
get() = DFS.dfs(
|
||||
supertypes,
|
||||
DFS.Neighbors { current ->
|
||||
val klass = current.classifier as? KClass<*> ?: throw KotlinReflectionInternalError("Supertype not a class: $current")
|
||||
val supertypes = klass.supertypes
|
||||
val typeArguments = current.arguments
|
||||
if (typeArguments.isEmpty()) supertypes
|
||||
else TypeSubstitutor.create((current as KTypeImpl).type).let { substitutor ->
|
||||
supertypes.map { supertype ->
|
||||
val substituted = substitutor.substitute((supertype as KTypeImpl).type, Variance.INVARIANT)
|
||||
?: throw KotlinReflectionInternalError("Type substitution failed: $supertype ($current)")
|
||||
KTypeImpl(substituted)
|
||||
}
|
||||
}
|
||||
},
|
||||
DFS.VisitedWithSet(),
|
||||
object : DFS.NodeHandlerWithListResult<KType, KType>() {
|
||||
override fun beforeChildren(current: KType): Boolean {
|
||||
result.add(current)
|
||||
return true
|
||||
supertypes,
|
||||
{ current ->
|
||||
val klass = current.classifier as? KClass<*> ?: throw KotlinReflectionInternalError("Supertype not a class: $current")
|
||||
val supertypes = klass.supertypes
|
||||
val typeArguments = current.arguments
|
||||
if (typeArguments.isEmpty()) supertypes
|
||||
else TypeSubstitutor.create((current as KTypeImpl).type).let { substitutor ->
|
||||
supertypes.map { supertype ->
|
||||
val substituted = substitutor.substitute((supertype as KTypeImpl).type, Variance.INVARIANT)
|
||||
?: throw KotlinReflectionInternalError("Type substitution failed: $supertype ($current)")
|
||||
KTypeImpl(substituted)
|
||||
}
|
||||
}
|
||||
},
|
||||
DFS.VisitedWithSet(),
|
||||
object : DFS.NodeHandlerWithListResult<KType, KType>() {
|
||||
override fun beforeChildren(current: KType): Boolean {
|
||||
result.add(current)
|
||||
return true
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -230,15 +229,15 @@ val KClass<*>.allSuperclasses: Collection<KClass<*>>
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
fun KClass<*>.isSubclassOf(base: KClass<*>): Boolean =
|
||||
this == base ||
|
||||
DFS.ifAny(listOf(this), KClass<*>::superclasses) { it == base }
|
||||
this == base ||
|
||||
DFS.ifAny(listOf(this), KClass<*>::superclasses) { it == base }
|
||||
|
||||
/**
|
||||
* Returns `true` if `this` class is the same or is a (possibly indirect) superclass of [derived], `false` otherwise.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean =
|
||||
derived.isSubclassOf(this)
|
||||
derived.isSubclassOf(this)
|
||||
|
||||
|
||||
/**
|
||||
@@ -275,7 +274,7 @@ fun <T : Any> KClass<T>.safeCast(value: Any?): T? {
|
||||
fun <T : Any> KClass<T>.createInstance(): T {
|
||||
// TODO: throw a meaningful exception
|
||||
val noArgsConstructor = constructors.singleOrNull { it.parameters.all(KParameter::isOptional) }
|
||||
?: throw IllegalArgumentException("Class should have a single no-arg constructor: $this")
|
||||
?: throw IllegalArgumentException("Class should have a single no-arg constructor: $this")
|
||||
|
||||
return noArgsConstructor.callBy(emptyMap())
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
@file:JvmName("KClassifiers")
|
||||
|
||||
package kotlin.reflect.full
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
@@ -40,12 +41,12 @@ import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
fun KClassifier.createType(
|
||||
arguments: List<KTypeProjection> = emptyList(),
|
||||
nullable: Boolean = false,
|
||||
annotations: List<Annotation> = emptyList()
|
||||
arguments: List<KTypeProjection> = emptyList(),
|
||||
nullable: Boolean = false,
|
||||
annotations: List<Annotation> = emptyList()
|
||||
): KType {
|
||||
val descriptor = (this as? KClassifierImpl)?.descriptor
|
||||
?: throw KotlinReflectionInternalError("Cannot create type for an unsupported classifier: $this (${this.javaClass})")
|
||||
?: throw KotlinReflectionInternalError("Cannot create type for an unsupported classifier: $this (${this.javaClass})")
|
||||
|
||||
val typeConstructor = descriptor.typeConstructor
|
||||
val parameters = typeConstructor.parameters
|
||||
@@ -56,14 +57,14 @@ fun KClassifier.createType(
|
||||
// TODO: throw exception if argument does not satisfy bounds
|
||||
|
||||
val typeAnnotations =
|
||||
if (annotations.isEmpty()) Annotations.EMPTY
|
||||
else Annotations.EMPTY // TODO: support type annotations
|
||||
if (annotations.isEmpty()) Annotations.EMPTY
|
||||
else Annotations.EMPTY // TODO: support type annotations
|
||||
|
||||
return KTypeImpl(createKotlinType(typeAnnotations, typeConstructor, arguments, nullable))
|
||||
}
|
||||
|
||||
private fun createKotlinType(
|
||||
typeAnnotations: Annotations, typeConstructor: TypeConstructor, arguments: List<KTypeProjection>, nullable: Boolean
|
||||
typeAnnotations: Annotations, typeConstructor: TypeConstructor, arguments: List<KTypeProjection>, nullable: Boolean
|
||||
): SimpleType {
|
||||
val parameters = typeConstructor.parameters
|
||||
return KotlinTypeFactory.simpleType(typeAnnotations, typeConstructor, arguments.mapIndexed { index, typeProjection ->
|
||||
@@ -87,7 +88,7 @@ private fun createKotlinType(
|
||||
val KClassifier.starProjectedType: KType
|
||||
get() {
|
||||
val descriptor = (this as? KClassifierImpl)?.descriptor
|
||||
?: return createType()
|
||||
?: return createType()
|
||||
|
||||
val typeParameters = descriptor.typeConstructor.parameters
|
||||
if (typeParameters.isEmpty()) return createType() // TODO: optimize, get defaultType from ClassDescriptor
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
@file:JvmName("KProperties")
|
||||
|
||||
package kotlin.reflect.full
|
||||
|
||||
import kotlin.reflect.KProperty1
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
@file:JvmName("KTypes")
|
||||
|
||||
package kotlin.reflect.full
|
||||
|
||||
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
||||
|
||||
@@ -41,8 +41,8 @@ class IllegalCallableAccessException(cause: IllegalAccessException) : Exception(
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
class IllegalPropertyDelegateAccessException(cause: IllegalAccessException) : Exception(
|
||||
"Cannot obtain the delegate of a non-accessible property. Use \"isAccessible = true\" to make the property accessible",
|
||||
cause
|
||||
"Cannot obtain the delegate of a non-accessible property. Use \"isAccessible = true\" to make the property accessible",
|
||||
cause
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,10 +18,10 @@
|
||||
|
||||
package kotlin.reflect.jvm
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.runtime.components.ReflectKotlinClass
|
||||
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
||||
import java.lang.reflect.*
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.javaType as stdlibJavaType
|
||||
import kotlin.reflect.full.companionObject
|
||||
import kotlin.reflect.full.functions
|
||||
import kotlin.reflect.full.memberProperties
|
||||
@@ -29,7 +29,7 @@ import kotlin.reflect.jvm.internal.KPackageImpl
|
||||
import kotlin.reflect.jvm.internal.KTypeImpl
|
||||
import kotlin.reflect.jvm.internal.asKCallableImpl
|
||||
import kotlin.reflect.jvm.internal.asKPropertyImpl
|
||||
import org.jetbrains.kotlin.descriptors.runtime.components.ReflectKotlinClass
|
||||
import kotlin.reflect.javaType as stdlibJavaType
|
||||
|
||||
// Kotlin reflection -> Java reflection
|
||||
|
||||
@@ -128,7 +128,7 @@ val Method.kotlinFunction: KFunction<*>?
|
||||
companion.functions.firstOrNull {
|
||||
val m = it.javaMethod
|
||||
m != null && m.name == this.name &&
|
||||
m.parameterTypes!!.contentEquals(this.parameterTypes) && m.returnType == this.returnType
|
||||
m.parameterTypes.contentEquals(this.parameterTypes) && m.returnType == this.returnType
|
||||
}?.let { return it }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor
|
||||
import java.lang.reflect.Array as ReflectArray
|
||||
import java.lang.reflect.ParameterizedType
|
||||
import java.lang.reflect.Type
|
||||
import java.lang.reflect.WildcardType
|
||||
@@ -19,6 +18,7 @@ import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.internal.calls.Caller
|
||||
import kotlin.reflect.jvm.javaType
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
import java.lang.reflect.Array as ReflectArray
|
||||
|
||||
internal abstract class KCallableImpl<out R> : KCallable<R>, KTypeParameterOwnerImpl {
|
||||
abstract val descriptor: CallableMemberDescriptor
|
||||
|
||||
@@ -20,6 +20,9 @@ import org.jetbrains.kotlin.builtins.CompanionObjectMapping
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.isMappedIntrinsicCompanionObject
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.runtime.components.ReflectKotlinClass
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.functionClassArity
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.wrapperByPrimitive
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
||||
@@ -37,9 +40,6 @@ import kotlin.jvm.internal.TypeIntrinsics
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.DECLARED
|
||||
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.INHERITED
|
||||
import org.jetbrains.kotlin.descriptors.runtime.components.ReflectKotlinClass
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.functionClassArity
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.wrapperByPrimitive
|
||||
|
||||
internal class KClassImpl<T : Any>(
|
||||
override val jClass: Class<T>
|
||||
@@ -130,7 +130,7 @@ internal class KClassImpl<T : Any>(
|
||||
if (superClass !is ClassDescriptor) throw KotlinReflectionInternalError("Supertype not a class: $superClass")
|
||||
|
||||
val superJavaClass = superClass.toJavaClass()
|
||||
?: throw KotlinReflectionInternalError("Unsupported superclass of $this: $superClass")
|
||||
?: throw KotlinReflectionInternalError("Unsupported superclass of $this: $superClass")
|
||||
|
||||
if (jClass.superclass == superJavaClass) {
|
||||
jClass.genericSuperclass
|
||||
@@ -142,9 +142,9 @@ internal class KClassImpl<T : Any>(
|
||||
}
|
||||
}
|
||||
if (!KotlinBuiltIns.isSpecialClassWithNoSupertypes(descriptor) && result.all {
|
||||
val classKind = DescriptorUtils.getClassDescriptorForType(it.type).kind
|
||||
classKind == ClassKind.INTERFACE || classKind == ClassKind.ANNOTATION_CLASS
|
||||
}) {
|
||||
val classKind = DescriptorUtils.getClassDescriptorForType(it.type).kind
|
||||
classKind == ClassKind.INTERFACE || classKind == ClassKind.ANNOTATION_CLASS
|
||||
}) {
|
||||
result += KTypeImpl(descriptor.builtIns.anyType) { Any::class.java }
|
||||
}
|
||||
result.compact()
|
||||
@@ -305,8 +305,7 @@ internal class KClassImpl<T : Any>(
|
||||
}
|
||||
|
||||
private fun reportUnresolvedClass(): Nothing {
|
||||
val kind = ReflectKotlinClass.create(jClass)?.classHeader?.kind
|
||||
when (kind) {
|
||||
when (val kind = ReflectKotlinClass.create(jClass)?.classHeader?.kind) {
|
||||
KotlinClassHeader.Kind.FILE_FACADE, KotlinClassHeader.Kind.MULTIFILE_CLASS, KotlinClassHeader.Kind.MULTIFILE_CLASS_PART -> {
|
||||
throw UnsupportedOperationException(
|
||||
"Packages and file facades are not yet supported in Kotlin reflection. " +
|
||||
|
||||
@@ -74,7 +74,7 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
if (match != null) {
|
||||
val (number) = match.destructured
|
||||
return getLocalProperty(number.toInt())
|
||||
?: throw KotlinReflectionInternalError("Local property #$number not found in $jClass")
|
||||
?: throw KotlinReflectionInternalError("Local property #$number not found in $jClass")
|
||||
}
|
||||
|
||||
val properties = getProperties(Name.identifier(name))
|
||||
@@ -99,9 +99,9 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
|
||||
val mostVisibleProperties = properties
|
||||
.groupBy { it.visibility }
|
||||
.toSortedMap(Comparator { first, second ->
|
||||
.toSortedMap { first, second ->
|
||||
DescriptorVisibilities.compare(first, second) ?: 0
|
||||
}).values.last()
|
||||
}.values.last()
|
||||
if (mostVisibleProperties.size == 1) {
|
||||
return mostVisibleProperties.first()
|
||||
}
|
||||
@@ -177,9 +177,7 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
// Falling back to enumerating all methods in the class in this (rather rare) case.
|
||||
// Example: class A(val x: Int) { fun getX(): String = ... }
|
||||
declaredMethods.firstOrNull { method ->
|
||||
method.name == name &&
|
||||
method.returnType == returnType &&
|
||||
method.parameterTypes!!.contentEquals(parameterTypes)
|
||||
method.name == name && method.returnType == returnType && method.parameterTypes.contentEquals(parameterTypes)
|
||||
}
|
||||
}
|
||||
} catch (e: NoSuchMethodException) {
|
||||
@@ -248,6 +246,7 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
while (desc[begin] != ')') {
|
||||
var end = begin
|
||||
while (desc[end] == '[') end++
|
||||
@Suppress("SpellCheckingInspection")
|
||||
when (desc[end]) {
|
||||
in "VZCBSIFJD" -> end++
|
||||
'L' -> end = desc.indexOf(';', begin) + 1
|
||||
|
||||
@@ -59,8 +59,7 @@ internal class KFunctionImpl private constructor(
|
||||
override val name: String get() = descriptor.name.asString()
|
||||
|
||||
override val caller: Caller<*> by ReflectProperties.lazy caller@{
|
||||
val jvmSignature = RuntimeTypeMapper.mapSignature(descriptor)
|
||||
val member: Member? = when (jvmSignature) {
|
||||
val member: Member? = when (val jvmSignature = RuntimeTypeMapper.mapSignature(descriptor)) {
|
||||
is KotlinConstructor -> {
|
||||
if (isAnnotationConstructor)
|
||||
return@caller AnnotationConstructorCaller(container.jClass, parameters.map { it.name!! }, POSITIONAL_CALL, KOTLIN)
|
||||
|
||||
@@ -19,6 +19,8 @@ package kotlin.reflect.jvm.internal
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.runtime.components.ReflectKotlinClass
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.classId
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.metadata.ProtoBuf
|
||||
import org.jetbrains.kotlin.metadata.deserialization.TypeTable
|
||||
@@ -32,8 +34,6 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.serialization.deserialization.MemberDeserializer
|
||||
import kotlin.reflect.KCallable
|
||||
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.DECLARED
|
||||
import org.jetbrains.kotlin.descriptors.runtime.components.ReflectKotlinClass
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.classId
|
||||
|
||||
internal class KPackageImpl(
|
||||
override val jClass: Class<*>,
|
||||
|
||||
@@ -49,8 +49,7 @@ internal abstract class KPropertyImpl<out V> private constructor(
|
||||
override val isBound: Boolean get() = rawBoundReceiver != CallableReference.NO_RECEIVER
|
||||
|
||||
private val _javaField: ReflectProperties.LazyVal<Field?> = ReflectProperties.lazy {
|
||||
val jvmSignature = RuntimeTypeMapper.mapPropertySignature(descriptor)
|
||||
when (jvmSignature) {
|
||||
when (val jvmSignature = RuntimeTypeMapper.mapPropertySignature(descriptor)) {
|
||||
is KotlinProperty -> {
|
||||
val descriptor = jvmSignature.descriptor
|
||||
JvmProtoBufUtil.getJvmFieldSignature(jvmSignature.proto, jvmSignature.nameResolver, jvmSignature.typeTable)?.let {
|
||||
|
||||
@@ -24,7 +24,8 @@ import java.lang.ref.SoftReference;
|
||||
|
||||
public class ReflectProperties {
|
||||
public static abstract class Val<T> {
|
||||
private static final Object NULL_VALUE = new Object() {};
|
||||
private static final Object NULL_VALUE = new Object() {
|
||||
};
|
||||
|
||||
@SuppressWarnings({"UnusedParameters", "unused"})
|
||||
public final T getValue(Object instance, Object metadata) {
|
||||
|
||||
@@ -128,9 +128,11 @@ public class ReflectionFactoryImpl extends ReflectionFactory {
|
||||
List<KTypeParameter> typeParameters;
|
||||
if (container instanceof KClass) {
|
||||
typeParameters = ((KClass<?>) container).getTypeParameters();
|
||||
} else if (container instanceof KCallable) {
|
||||
}
|
||||
else if (container instanceof KCallable) {
|
||||
typeParameters = ((KCallable<?>) container).getTypeParameters();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Type parameter container must be a class or a callable: " + container);
|
||||
}
|
||||
for (KTypeParameter typeParameter : typeParameters) {
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
|
||||
package kotlin.reflect.jvm.internal
|
||||
|
||||
import org.jetbrains.kotlin.builtins.StandardNames
|
||||
import org.jetbrains.kotlin.builtins.PrimitiveType
|
||||
import org.jetbrains.kotlin.builtins.StandardNames
|
||||
import org.jetbrains.kotlin.builtins.jvm.CloneableClassScope
|
||||
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
@@ -108,8 +108,8 @@ internal sealed class JvmPropertySignature {
|
||||
nameResolver.getString(signature.getter.name) + nameResolver.getString(signature.getter.desc)
|
||||
} else {
|
||||
val (name, desc) =
|
||||
JvmProtoBufUtil.getJvmFieldSignature(proto, nameResolver, typeTable)
|
||||
?: throw KotlinReflectionInternalError("No field signature for property: $descriptor")
|
||||
JvmProtoBufUtil.getJvmFieldSignature(proto, nameResolver, typeTable)
|
||||
?: throw KotlinReflectionInternalError("No field signature for property: $descriptor")
|
||||
JvmAbi.getterName(name) + getManglingSuffix() + "()" + desc
|
||||
}
|
||||
|
||||
@@ -218,8 +218,7 @@ internal object RuntimeTypeMapper {
|
||||
}
|
||||
}
|
||||
is JavaPropertyDescriptor -> {
|
||||
val element = (property.source as? JavaSourceElement)?.javaElement
|
||||
return when (element) {
|
||||
return when (val element = (property.source as? JavaSourceElement)?.javaElement) {
|
||||
is ReflectJavaField -> JvmPropertySignature.JavaField(element.member)
|
||||
is ReflectJavaMethod -> JvmPropertySignature.JavaMethodProperty(
|
||||
element.member,
|
||||
|
||||
+32
-35
@@ -5,12 +5,11 @@
|
||||
|
||||
package kotlin.reflect.jvm.internal.calls
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.wrapperByPrimitive
|
||||
import java.lang.reflect.Proxy
|
||||
import java.lang.reflect.Type
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.wrapperByPrimitive
|
||||
import java.lang.reflect.Method as ReflectMethod
|
||||
|
||||
internal class AnnotationConstructorCaller(
|
||||
@@ -90,11 +89,10 @@ private fun throwIllegalArgumentType(index: Int, name: String, expectedJvmType:
|
||||
else -> expectedJvmType.kotlin
|
||||
}
|
||||
// For arrays, also render the type argument in the message, e.g. "... not of the required type kotlin.Array<kotlin.reflect.KClass>"
|
||||
val typeString = when {
|
||||
kotlinClass.qualifiedName == Array<Any>::class.qualifiedName ->
|
||||
val typeString =
|
||||
if (kotlinClass.qualifiedName == Array<Any>::class.qualifiedName)
|
||||
"${kotlinClass.qualifiedName}<${kotlinClass.java.componentType.kotlin.qualifiedName}>"
|
||||
else -> kotlinClass.qualifiedName
|
||||
}
|
||||
else kotlinClass.qualifiedName
|
||||
throw IllegalArgumentException("Argument #$index $name is not of the required type $typeString")
|
||||
}
|
||||
|
||||
@@ -109,15 +107,15 @@ internal fun <T : Any> createAnnotationInstance(
|
||||
val ours = values[method.name]
|
||||
val theirs = method(other)
|
||||
when (ours) {
|
||||
is BooleanArray -> Arrays.equals(ours, theirs as BooleanArray)
|
||||
is CharArray -> Arrays.equals(ours, theirs as CharArray)
|
||||
is ByteArray -> Arrays.equals(ours, theirs as ByteArray)
|
||||
is ShortArray -> Arrays.equals(ours, theirs as ShortArray)
|
||||
is IntArray -> Arrays.equals(ours, theirs as IntArray)
|
||||
is FloatArray -> Arrays.equals(ours, theirs as FloatArray)
|
||||
is LongArray -> Arrays.equals(ours, theirs as LongArray)
|
||||
is DoubleArray -> Arrays.equals(ours, theirs as DoubleArray)
|
||||
is Array<*> -> Arrays.equals(ours, theirs as Array<*>)
|
||||
is BooleanArray -> ours contentEquals theirs as BooleanArray
|
||||
is CharArray -> ours contentEquals theirs as CharArray
|
||||
is ByteArray -> ours contentEquals theirs as ByteArray
|
||||
is ShortArray -> ours contentEquals theirs as ShortArray
|
||||
is IntArray -> ours contentEquals theirs as IntArray
|
||||
is FloatArray -> ours contentEquals theirs as FloatArray
|
||||
is LongArray -> ours contentEquals theirs as LongArray
|
||||
is DoubleArray -> ours contentEquals theirs as DoubleArray
|
||||
is Array<*> -> ours contentEquals theirs as Array<*>
|
||||
else -> ours == theirs
|
||||
}
|
||||
}
|
||||
@@ -126,15 +124,15 @@ internal fun <T : Any> createAnnotationInstance(
|
||||
values.entries.sumOf { entry ->
|
||||
val (key, value) = entry
|
||||
val valueHash = when (value) {
|
||||
is BooleanArray -> Arrays.hashCode(value)
|
||||
is CharArray -> Arrays.hashCode(value)
|
||||
is ByteArray -> Arrays.hashCode(value)
|
||||
is ShortArray -> Arrays.hashCode(value)
|
||||
is IntArray -> Arrays.hashCode(value)
|
||||
is FloatArray -> Arrays.hashCode(value)
|
||||
is LongArray -> Arrays.hashCode(value)
|
||||
is DoubleArray -> Arrays.hashCode(value)
|
||||
is Array<*> -> Arrays.hashCode(value)
|
||||
is BooleanArray -> value.contentHashCode()
|
||||
is CharArray -> value.contentHashCode()
|
||||
is ByteArray -> value.contentHashCode()
|
||||
is ShortArray -> value.contentHashCode()
|
||||
is IntArray -> value.contentHashCode()
|
||||
is FloatArray -> value.contentHashCode()
|
||||
is LongArray -> value.contentHashCode()
|
||||
is DoubleArray -> value.contentHashCode()
|
||||
is Array<*> -> value.contentHashCode()
|
||||
else -> value.hashCode()
|
||||
}
|
||||
127 * key.hashCode() xor valueHash
|
||||
@@ -148,15 +146,15 @@ internal fun <T : Any> createAnnotationInstance(
|
||||
values.entries.joinTo(this, separator = ", ", prefix = "(", postfix = ")") { entry ->
|
||||
val (key, value) = entry
|
||||
val valueString = when (value) {
|
||||
is BooleanArray -> Arrays.toString(value)
|
||||
is CharArray -> Arrays.toString(value)
|
||||
is ByteArray -> Arrays.toString(value)
|
||||
is ShortArray -> Arrays.toString(value)
|
||||
is IntArray -> Arrays.toString(value)
|
||||
is FloatArray -> Arrays.toString(value)
|
||||
is LongArray -> Arrays.toString(value)
|
||||
is DoubleArray -> Arrays.toString(value)
|
||||
is Array<*> -> Arrays.toString(value)
|
||||
is BooleanArray -> value.contentToString()
|
||||
is CharArray -> value.contentToString()
|
||||
is ByteArray -> value.contentToString()
|
||||
is ShortArray -> value.contentToString()
|
||||
is IntArray -> value.contentToString()
|
||||
is FloatArray -> value.contentToString()
|
||||
is LongArray -> value.contentToString()
|
||||
is DoubleArray -> value.contentToString()
|
||||
is Array<*> -> value.contentToString()
|
||||
else -> value.toString()
|
||||
}
|
||||
"$key=$valueString"
|
||||
@@ -165,8 +163,7 @@ internal fun <T : Any> createAnnotationInstance(
|
||||
}
|
||||
|
||||
val result = Proxy.newProxyInstance(annotationClass.classLoader, arrayOf(annotationClass)) { _, method, args ->
|
||||
val name = method.name
|
||||
when (name) {
|
||||
when (val name = method.name) {
|
||||
"annotationType" -> annotationClass
|
||||
"toString" -> toString
|
||||
"hashCode" -> hashCode
|
||||
|
||||
@@ -218,7 +218,7 @@ internal sealed class CallerImpl<out M : Member>(
|
||||
|
||||
class BoundInstance(field: ReflectField, notNull: Boolean, private val boundReceiver: Any?) : BoundCaller,
|
||||
FieldSetter(field, notNull, requiresInstance = false) {
|
||||
override fun call(args: Array<*>): Any? {
|
||||
override fun call(args: Array<*>): Any {
|
||||
checkArguments(args)
|
||||
return member.set(boundReceiver, args.first())
|
||||
}
|
||||
@@ -226,7 +226,7 @@ internal sealed class CallerImpl<out M : Member>(
|
||||
|
||||
class BoundJvmStaticInObject(field: ReflectField, notNull: Boolean) : BoundCaller,
|
||||
FieldSetter(field, notNull, requiresInstance = false) {
|
||||
override fun call(args: Array<*>): Any? {
|
||||
override fun call(args: Array<*>): Any {
|
||||
checkArguments(args)
|
||||
return member.set(null, args.last())
|
||||
}
|
||||
|
||||
@@ -16,11 +16,11 @@
|
||||
|
||||
package kotlin.reflect.jvm.internal
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.runtime.components.RuntimeModuleData
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.safeClassLoader
|
||||
import java.lang.ref.WeakReference
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.ConcurrentMap
|
||||
import org.jetbrains.kotlin.descriptors.runtime.components.RuntimeModuleData
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.safeClassLoader
|
||||
|
||||
// TODO: collect nulls periodically
|
||||
private val moduleByClassLoader: ConcurrentMap<WeakClassLoaderBox, WeakReference<RuntimeModuleData>> = ConcurrentHashMap()
|
||||
|
||||
@@ -57,8 +57,7 @@ import kotlin.reflect.jvm.internal.calls.createAnnotationInstance
|
||||
internal val JVM_STATIC = FqName("kotlin.jvm.JvmStatic")
|
||||
|
||||
internal fun ClassDescriptor.toJavaClass(): Class<*>? {
|
||||
val source = source
|
||||
return when (source) {
|
||||
return when (val source = source) {
|
||||
is KotlinJvmBinarySourceElement -> {
|
||||
(source.binaryClass as ReflectKotlinClass).klass
|
||||
}
|
||||
@@ -118,8 +117,7 @@ internal fun DescriptorVisibility.toKVisibility(): KVisibility? =
|
||||
|
||||
internal fun Annotated.computeAnnotations(): List<Annotation> =
|
||||
annotations.mapNotNull {
|
||||
val source = it.source
|
||||
when (source) {
|
||||
when (val source = it.source) {
|
||||
is ReflectAnnotationSource -> source.annotation
|
||||
is RuntimeSourceElementFactory.RuntimeSourceElement -> (source.javaElement as? ReflectJavaAnnotation)?.annotation
|
||||
else -> it.toAnnotationInstance()
|
||||
@@ -223,7 +221,7 @@ internal fun <M : MessageLite, D : CallableDescriptor> deserializeToDescriptor(
|
||||
typeTable: TypeTable,
|
||||
metadataVersion: BinaryVersion,
|
||||
createDescriptor: MemberDeserializer.(M) -> D
|
||||
): D? {
|
||||
): D {
|
||||
val moduleData = moduleAnchor.getOrCreateModule()
|
||||
|
||||
val typeParameters = when (proto) {
|
||||
|
||||
@@ -44,7 +44,7 @@ fun <R> Function<R>.reflect(): KFunction<R>? {
|
||||
|
||||
val descriptor = deserializeToDescriptor(
|
||||
javaClass, proto, nameResolver, TypeTable(proto.typeTable), metadataVersion, MemberDeserializer::loadFunction
|
||||
) ?: return null
|
||||
)
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return KFunctionImpl(EmptyContainerForLocal, descriptor) as KFunction<R>
|
||||
|
||||
Reference in New Issue
Block a user