From 26cdb2f928982dad7f0c9ab8e3bd509665d9d537 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 21 May 2021 16:27:28 +0200 Subject: [PATCH] Reformat reflection.jvm, fix inspections --- .../src/kotlin/reflect/full/KCallables.kt | 3 +- .../src/kotlin/reflect/full/KClasses.kt | 59 ++++++++-------- .../src/kotlin/reflect/full/KClassifiers.kt | 17 ++--- .../src/kotlin/reflect/full/KProperties.kt | 1 + .../src/kotlin/reflect/full/KTypes.kt | 1 + .../src/kotlin/reflect/full/exceptions.kt | 4 +- .../kotlin/reflect/jvm/ReflectJvmMapping.kt | 6 +- .../reflect/jvm/internal/KCallableImpl.kt | 2 +- .../kotlin/reflect/jvm/internal/KClassImpl.kt | 17 +++-- .../jvm/internal/KDeclarationContainerImpl.kt | 11 ++- .../reflect/jvm/internal/KFunctionImpl.kt | 3 +- .../reflect/jvm/internal/KPackageImpl.kt | 4 +- .../reflect/jvm/internal/KPropertyImpl.kt | 3 +- .../jvm/internal/ReflectProperties.java | 3 +- .../jvm/internal/ReflectionFactoryImpl.java | 6 +- .../reflect/jvm/internal/RuntimeTypeMapper.kt | 9 ++- .../calls/AnnotationConstructorCaller.kt | 67 +++++++++---------- .../reflect/jvm/internal/calls/CallerImpl.kt | 4 +- .../jvm/internal/moduleByClassLoader.kt | 4 +- .../src/kotlin/reflect/jvm/internal/util.kt | 8 +-- .../src/kotlin/reflect/jvm/reflectLambda.kt | 2 +- 21 files changed, 115 insertions(+), 119 deletions(-) diff --git a/core/reflection.jvm/src/kotlin/reflect/full/KCallables.kt b/core/reflection.jvm/src/kotlin/reflect/full/KCallables.kt index cb491242db5..dcf599d3e9a 100644 --- a/core/reflection.jvm/src/kotlin/reflect/full/KCallables.kt +++ b/core/reflection.jvm/src/kotlin/reflect/full/KCallables.kt @@ -4,6 +4,7 @@ */ @file:JvmName("KCallables") + package kotlin.reflect.full import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn @@ -77,4 +78,4 @@ suspend fun KCallable.callSuspendBy(args: Map): R { @Suppress("UNCHECKED_CAST") if (returnType.classifier == Unit::class && !returnType.isMarkedNullable) return (Unit as R) return result -} \ No newline at end of file +} diff --git a/core/reflection.jvm/src/kotlin/reflect/full/KClasses.kt b/core/reflection.jvm/src/kotlin/reflect/full/KClasses.kt index d20675d466d..5a2f781c3d7 100644 --- a/core/reflection.jvm/src/kotlin/reflect/full/KClasses.kt +++ b/core/reflection.jvm/src/kotlin/reflect/full/KClasses.kt @@ -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` [defaultType] would return the type `MyMap`. */ -@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> @SinceKotlin("1.1") val KClass<*>.allSupertypes: Collection 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() { - 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() { + override fun beforeChildren(current: KType): Boolean { + result.add(current) + return true + } + } ) /** @@ -230,15 +229,15 @@ val KClass<*>.allSuperclasses: Collection> */ @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 KClass.safeCast(value: Any?): T? { fun KClass.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()) } diff --git a/core/reflection.jvm/src/kotlin/reflect/full/KClassifiers.kt b/core/reflection.jvm/src/kotlin/reflect/full/KClassifiers.kt index 82759c0b15f..62ee114d238 100644 --- a/core/reflection.jvm/src/kotlin/reflect/full/KClassifiers.kt +++ b/core/reflection.jvm/src/kotlin/reflect/full/KClassifiers.kt @@ -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 = emptyList(), - nullable: Boolean = false, - annotations: List = emptyList() + arguments: List = emptyList(), + nullable: Boolean = false, + annotations: List = 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, nullable: Boolean + typeAnnotations: Annotations, typeConstructor: TypeConstructor, arguments: List, 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 diff --git a/core/reflection.jvm/src/kotlin/reflect/full/KProperties.kt b/core/reflection.jvm/src/kotlin/reflect/full/KProperties.kt index 7c85b2d4e40..7fd4340ef9f 100644 --- a/core/reflection.jvm/src/kotlin/reflect/full/KProperties.kt +++ b/core/reflection.jvm/src/kotlin/reflect/full/KProperties.kt @@ -15,6 +15,7 @@ */ @file:JvmName("KProperties") + package kotlin.reflect.full import kotlin.reflect.KProperty1 diff --git a/core/reflection.jvm/src/kotlin/reflect/full/KTypes.kt b/core/reflection.jvm/src/kotlin/reflect/full/KTypes.kt index a4432be1759..1b800b84c11 100644 --- a/core/reflection.jvm/src/kotlin/reflect/full/KTypes.kt +++ b/core/reflection.jvm/src/kotlin/reflect/full/KTypes.kt @@ -15,6 +15,7 @@ */ @file:JvmName("KTypes") + package kotlin.reflect.full import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf diff --git a/core/reflection.jvm/src/kotlin/reflect/full/exceptions.kt b/core/reflection.jvm/src/kotlin/reflect/full/exceptions.kt index 38e97a1777d..701ba6abe03 100644 --- a/core/reflection.jvm/src/kotlin/reflect/full/exceptions.kt +++ b/core/reflection.jvm/src/kotlin/reflect/full/exceptions.kt @@ -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 ) /** diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/ReflectJvmMapping.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/ReflectJvmMapping.kt index 4573725f51c..3beeabd1c51 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/ReflectJvmMapping.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/ReflectJvmMapping.kt @@ -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 } } } diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableImpl.kt index a1a1e5d723e..aeb47520881 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableImpl.kt @@ -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 : KCallable, KTypeParameterOwnerImpl { abstract val descriptor: CallableMemberDescriptor diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt index 8def6fe6f34..14ab0bd9e61 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt @@ -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( override val jClass: Class @@ -130,7 +130,7 @@ internal class KClassImpl( 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( } } 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( } 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. " + diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KDeclarationContainerImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KDeclarationContainerImpl.kt index 35d14c74401..e33d9a3ec1c 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KDeclarationContainerImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KDeclarationContainerImpl.kt @@ -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 diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KFunctionImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KFunctionImpl.kt index 7de5f8e343f..4b67866b3bd 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KFunctionImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KFunctionImpl.kt @@ -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) diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPackageImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPackageImpl.kt index 124c23a45a7..78588448e0e 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPackageImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPackageImpl.kt @@ -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<*>, diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPropertyImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPropertyImpl.kt index 234f59f18ca..bff42cc44f8 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPropertyImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPropertyImpl.kt @@ -49,8 +49,7 @@ internal abstract class KPropertyImpl private constructor( override val isBound: Boolean get() = rawBoundReceiver != CallableReference.NO_RECEIVER private val _javaField: ReflectProperties.LazyVal = 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 { diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/ReflectProperties.java b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/ReflectProperties.java index 0326cbc0486..d2f2d0227fd 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/ReflectProperties.java +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/ReflectProperties.java @@ -24,7 +24,8 @@ import java.lang.ref.SoftReference; public class ReflectProperties { public static abstract class Val { - 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) { diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/ReflectionFactoryImpl.java b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/ReflectionFactoryImpl.java index d36537f53e1..c0c492749de 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/ReflectionFactoryImpl.java +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/ReflectionFactoryImpl.java @@ -128,9 +128,11 @@ public class ReflectionFactoryImpl extends ReflectionFactory { List 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) { diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/RuntimeTypeMapper.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/RuntimeTypeMapper.kt index 54193eaa29b..5d648152648 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/RuntimeTypeMapper.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/RuntimeTypeMapper.kt @@ -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, diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/AnnotationConstructorCaller.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/AnnotationConstructorCaller.kt index 1546775fab2..6059b356ed0 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/AnnotationConstructorCaller.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/AnnotationConstructorCaller.kt @@ -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" - val typeString = when { - kotlinClass.qualifiedName == Array::class.qualifiedName -> + val typeString = + if (kotlinClass.qualifiedName == Array::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 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 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 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 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 diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/CallerImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/CallerImpl.kt index 054bae670f0..66656367b19 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/CallerImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/CallerImpl.kt @@ -218,7 +218,7 @@ internal sealed class CallerImpl( 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( 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()) } diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/moduleByClassLoader.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/moduleByClassLoader.kt index 68815954188..e341de4064f 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/moduleByClassLoader.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/moduleByClassLoader.kt @@ -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> = ConcurrentHashMap() diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/util.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/util.kt index fcae9bb6ea6..ddbdcd9dc9c 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/util.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/util.kt @@ -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 = 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 deserializeToDescriptor( typeTable: TypeTable, metadataVersion: BinaryVersion, createDescriptor: MemberDeserializer.(M) -> D -): D? { +): D { val moduleData = moduleAnchor.getOrCreateModule() val typeParameters = when (proto) { diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/reflectLambda.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/reflectLambda.kt index f09d1f02f95..f0cbcab6327 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/reflectLambda.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/reflectLambda.kt @@ -44,7 +44,7 @@ fun Function.reflect(): KFunction? { val descriptor = deserializeToDescriptor( javaClass, proto, nameResolver, TypeTable(proto.typeTable), metadataVersion, MemberDeserializer::loadFunction - ) ?: return null + ) @Suppress("UNCHECKED_CAST") return KFunctionImpl(EmptyContainerForLocal, descriptor) as KFunction