Extract Caller interface out of FunctionCaller, move to subpackage

Also rename FunctionCaller to CallerImpl and extract irrelevant
declarations out of it
This commit is contained in:
Alexander Udalov
2018-08-23 15:18:39 +02:00
parent 43dcbbcce1
commit 356b02cb91
8 changed files with 135 additions and 111 deletions
@@ -15,16 +15,17 @@ import java.lang.reflect.WildcardType
import java.util.*
import kotlin.coroutines.Continuation
import kotlin.reflect.*
import kotlin.reflect.jvm.internal.calls.Caller
import kotlin.reflect.jvm.javaType
internal abstract class KCallableImpl<out R> : KCallable<R> {
abstract val descriptor: CallableMemberDescriptor
// The instance which is used to perform a positional call, i.e. `call`
abstract val caller: FunctionCaller<*>
abstract val caller: Caller<*>
// The instance which is used to perform a call "by name", i.e. `callBy`
abstract val defaultCaller: FunctionCaller<*>?
abstract val defaultCaller: Caller<*>?
abstract val container: KDeclarationContainerImpl
@@ -25,11 +25,12 @@ import java.lang.reflect.Modifier
import kotlin.jvm.internal.CallableReference
import kotlin.jvm.internal.FunctionBase
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.internal.AnnotationConstructorCaller.CallMode.CALL_BY_NAME
import kotlin.reflect.jvm.internal.AnnotationConstructorCaller.CallMode.POSITIONAL_CALL
import kotlin.reflect.jvm.internal.AnnotationConstructorCaller.Origin.JAVA
import kotlin.reflect.jvm.internal.AnnotationConstructorCaller.Origin.KOTLIN
import kotlin.reflect.jvm.internal.JvmFunctionSignature.*
import kotlin.reflect.jvm.internal.calls.*
import kotlin.reflect.jvm.internal.calls.AnnotationConstructorCaller.CallMode.CALL_BY_NAME
import kotlin.reflect.jvm.internal.calls.AnnotationConstructorCaller.CallMode.POSITIONAL_CALL
import kotlin.reflect.jvm.internal.calls.AnnotationConstructorCaller.Origin.JAVA
import kotlin.reflect.jvm.internal.calls.AnnotationConstructorCaller.Origin.KOTLIN
internal class KFunctionImpl private constructor(
override val container: KDeclarationContainerImpl,
@@ -56,7 +57,7 @@ internal class KFunctionImpl private constructor(
override val name: String get() = descriptor.name.asString()
override val caller: FunctionCaller<*> by ReflectProperties.lazySoft caller@{
override val caller: Caller<*> by ReflectProperties.lazySoft caller@{
val jvmSignature = RuntimeTypeMapper.mapSignature(descriptor)
val member: Member? = when (jvmSignature) {
is KotlinConstructor -> {
@@ -89,7 +90,7 @@ internal class KFunctionImpl private constructor(
}
}
override val defaultCaller: FunctionCaller<*>? by ReflectProperties.lazySoft defaultCaller@{
override val defaultCaller: Caller<*>? by ReflectProperties.lazySoft defaultCaller@{
val jvmSignature = RuntimeTypeMapper.mapSignature(descriptor)
val member: Member? = when (jvmSignature) {
is KotlinFunction -> {
@@ -132,16 +133,16 @@ internal class KFunctionImpl private constructor(
}
private fun createStaticMethodCaller(member: Method) =
if (isBound) FunctionCaller.BoundStaticMethod(member, boundReceiver) else FunctionCaller.StaticMethod(member)
if (isBound) CallerImpl.BoundStaticMethod(member, boundReceiver) else CallerImpl.StaticMethod(member)
private fun createJvmStaticInObjectCaller(member: Method) =
if (isBound) FunctionCaller.BoundJvmStaticInObject(member) else FunctionCaller.JvmStaticInObject(member)
if (isBound) CallerImpl.BoundJvmStaticInObject(member) else CallerImpl.JvmStaticInObject(member)
private fun createInstanceMethodCaller(member: Method) =
if (isBound) FunctionCaller.BoundInstanceMethod(member, boundReceiver) else FunctionCaller.InstanceMethod(member)
if (isBound) CallerImpl.BoundInstanceMethod(member, boundReceiver) else CallerImpl.InstanceMethod(member)
private fun createConstructorCaller(member: Constructor<*>) =
if (isBound) FunctionCaller.BoundConstructor(member, boundReceiver) else FunctionCaller.Constructor(member)
if (isBound) CallerImpl.BoundConstructor(member, boundReceiver) else CallerImpl.Constructor(member)
override val arity: Int get() = caller.arity
@@ -21,6 +21,9 @@ import kotlin.reflect.KMutableProperty
import kotlin.reflect.KProperty
import kotlin.reflect.full.IllegalPropertyDelegateAccessException
import kotlin.reflect.jvm.internal.JvmPropertySignature.*
import kotlin.reflect.jvm.internal.calls.Caller
import kotlin.reflect.jvm.internal.calls.CallerImpl
import kotlin.reflect.jvm.internal.calls.ThrowingCaller
internal abstract class KPropertyImpl<out R> private constructor(
override val container: KDeclarationContainerImpl,
@@ -99,9 +102,9 @@ internal abstract class KPropertyImpl<out R> private constructor(
override val descriptor: PropertyDescriptor get() = _descriptor()
override val caller: FunctionCaller<*> get() = getter.caller
override val caller: Caller<*> get() = getter.caller
override val defaultCaller: FunctionCaller<*>? get() = getter.defaultCaller
override val defaultCaller: Caller<*>? get() = getter.defaultCaller
override val isLateinit: Boolean get() = descriptor.isLateInit
@@ -128,7 +131,7 @@ internal abstract class KPropertyImpl<out R> private constructor(
override val container: KDeclarationContainerImpl get() = property.container
override val defaultCaller: FunctionCaller<*>? get() = null
override val defaultCaller: Caller<*>? get() = null
override val isBound: Boolean get() = property.isBound
@@ -147,7 +150,7 @@ internal abstract class KPropertyImpl<out R> private constructor(
property.descriptor.getter ?: DescriptorFactory.createDefaultGetter(property.descriptor, Annotations.EMPTY)
}
override val caller: FunctionCaller<*> by ReflectProperties.lazySoft {
override val caller: Caller<*> by ReflectProperties.lazySoft {
computeCallerForAccessor(isGetter = true)
}
}
@@ -160,7 +163,7 @@ internal abstract class KPropertyImpl<out R> private constructor(
property.descriptor.setter ?: DescriptorFactory.createDefaultSetter(property.descriptor, Annotations.EMPTY, Annotations.EMPTY)
}
override val caller: FunctionCaller<*> by ReflectProperties.lazySoft {
override val caller: Caller<*> by ReflectProperties.lazySoft {
computeCallerForAccessor(isGetter = false)
}
}
@@ -171,9 +174,9 @@ internal abstract class KPropertyImpl<out R> private constructor(
}
private fun KPropertyImpl.Accessor<*, *>.computeCallerForAccessor(isGetter: Boolean): FunctionCaller<*> {
private fun KPropertyImpl.Accessor<*, *>.computeCallerForAccessor(isGetter: Boolean): Caller<*> {
if (KDeclarationContainerImpl.LOCAL_PROPERTY_SIGNATURE.matches(property.signature)) {
return FunctionCaller.ThrowingCaller
return ThrowingCaller
}
fun isInsideClassCompanionObject(): Boolean {
@@ -201,33 +204,33 @@ private fun KPropertyImpl.Accessor<*, *>.computeCallerForAccessor(isGetter: Bool
fun isNotNullProperty() =
!TypeUtils.isNullableType(property.descriptor.type)
fun computeFieldCaller(field: Field): FunctionCaller<Field> = when {
fun computeFieldCaller(field: Field): Caller<Field> = when {
isInsideClassCompanionObject() || isInsideInterfaceCompanionObjectWithJvmField() -> {
val klass = (descriptor.containingDeclaration as ClassDescriptor).toJavaClass()!!
if (isGetter)
if (isBound) FunctionCaller.BoundClassCompanionFieldGetter(field, klass)
else FunctionCaller.ClassCompanionFieldGetter(field, klass)
if (isBound) CallerImpl.BoundClassCompanionFieldGetter(field, klass)
else CallerImpl.ClassCompanionFieldGetter(field, klass)
else
if (isBound) FunctionCaller.BoundClassCompanionFieldSetter(field, klass)
else FunctionCaller.ClassCompanionFieldSetter(field, klass)
if (isBound) CallerImpl.BoundClassCompanionFieldSetter(field, klass)
else CallerImpl.ClassCompanionFieldSetter(field, klass)
}
!Modifier.isStatic(field.modifiers) ->
if (isGetter)
if (isBound) FunctionCaller.BoundInstanceFieldGetter(field, property.boundReceiver)
else FunctionCaller.InstanceFieldGetter(field)
if (isBound) CallerImpl.BoundInstanceFieldGetter(field, property.boundReceiver)
else CallerImpl.InstanceFieldGetter(field)
else
if (isBound) FunctionCaller.BoundInstanceFieldSetter(field, isNotNullProperty(), property.boundReceiver)
else FunctionCaller.InstanceFieldSetter(field, isNotNullProperty())
if (isBound) CallerImpl.BoundInstanceFieldSetter(field, isNotNullProperty(), property.boundReceiver)
else CallerImpl.InstanceFieldSetter(field, isNotNullProperty())
isJvmStaticProperty() ->
if (isGetter)
if (isBound) FunctionCaller.BoundJvmStaticInObjectFieldGetter(field)
else FunctionCaller.JvmStaticInObjectFieldGetter(field)
if (isBound) CallerImpl.BoundJvmStaticInObjectFieldGetter(field)
else CallerImpl.JvmStaticInObjectFieldGetter(field)
else
if (isBound) FunctionCaller.BoundJvmStaticInObjectFieldSetter(field, isNotNullProperty())
else FunctionCaller.JvmStaticInObjectFieldSetter(field, isNotNullProperty())
if (isBound) CallerImpl.BoundJvmStaticInObjectFieldSetter(field, isNotNullProperty())
else CallerImpl.JvmStaticInObjectFieldSetter(field, isNotNullProperty())
else ->
if (isGetter) FunctionCaller.StaticFieldGetter(field)
else FunctionCaller.StaticFieldSetter(field, isNotNullProperty())
if (isGetter) CallerImpl.StaticFieldGetter(field)
else CallerImpl.StaticFieldSetter(field, isNotNullProperty())
}
val jvmSignature = RuntimeTypeMapper.mapPropertySignature(property.descriptor)
@@ -250,18 +253,17 @@ private fun KPropertyImpl.Accessor<*, *>.computeCallerForAccessor(isGetter: Bool
when {
accessor == null -> computeFieldCaller(
property.javaField
?: throw KotlinReflectionInternalError("No accessors or field is found for property $property")
property.javaField ?: throw KotlinReflectionInternalError("No accessors or field is found for property $property")
)
!Modifier.isStatic(accessor.modifiers) ->
if (isBound) FunctionCaller.BoundInstanceMethod(accessor, property.boundReceiver)
else FunctionCaller.InstanceMethod(accessor)
if (isBound) CallerImpl.BoundInstanceMethod(accessor, property.boundReceiver)
else CallerImpl.InstanceMethod(accessor)
isJvmStaticProperty() ->
if (isBound) FunctionCaller.BoundJvmStaticInObject(accessor)
else FunctionCaller.JvmStaticInObject(accessor)
if (isBound) CallerImpl.BoundJvmStaticInObject(accessor)
else CallerImpl.JvmStaticInObject(accessor)
else ->
if (isBound) FunctionCaller.BoundStaticMethod(accessor, property.boundReceiver)
else FunctionCaller.StaticMethod(accessor)
if (isBound) CallerImpl.BoundStaticMethod(accessor, property.boundReceiver)
else CallerImpl.StaticMethod(accessor)
}
}
is JavaField -> {
@@ -273,22 +275,21 @@ private fun KPropertyImpl.Accessor<*, *>.computeCallerForAccessor(isGetter: Bool
else jvmSignature.setterMethod ?: throw KotlinReflectionInternalError(
"No source found for setter of Java method property: ${jvmSignature.getterMethod}"
)
if (isBound) FunctionCaller.BoundInstanceMethod(method, property.boundReceiver)
else FunctionCaller.InstanceMethod(method)
if (isBound) CallerImpl.BoundInstanceMethod(method, property.boundReceiver)
else CallerImpl.InstanceMethod(method)
}
is MappedKotlinProperty -> {
val signature =
if (isGetter) jvmSignature.getterSignature
else (jvmSignature.setterSignature
?: throw KotlinReflectionInternalError("No setter found for property $property"))
else (jvmSignature.setterSignature ?: throw KotlinReflectionInternalError("No setter found for property $property"))
val accessor = property.container.findMethodBySignature(
signature.methodName, signature.methodDesc, descriptor.isPublicInBytecode
) ?: throw KotlinReflectionInternalError("No accessor found for property $property")
assert(!Modifier.isStatic(accessor.modifiers)) { "Mapped property cannot have a static accessor: $property" }
return if (isBound) FunctionCaller.BoundInstanceMethod(accessor, property.boundReceiver)
else FunctionCaller.InstanceMethod(accessor)
return if (isBound) CallerImpl.BoundInstanceMethod(accessor, property.boundReceiver)
else CallerImpl.InstanceMethod(accessor)
}
}
}
@@ -1,24 +1,15 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2018 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.jvm.internal
package kotlin.reflect.jvm.internal.calls
import java.lang.reflect.Proxy
import java.lang.reflect.Type
import java.util.*
import kotlin.reflect.KClass
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
import kotlin.reflect.jvm.internal.structure.wrapperByPrimitive
import java.lang.reflect.Method as ReflectMethod
@@ -28,9 +19,15 @@ internal class AnnotationConstructorCaller(
private val callMode: CallMode,
origin: Origin,
private val methods: List<ReflectMethod> = parameterNames.map { name -> jClass.getDeclaredMethod(name) }
) : FunctionCaller<Nothing?>(
null, jClass, null, methods.map { it.genericReturnType }.toTypedArray()
) {
) : Caller<Nothing?> {
override val member: Nothing?
get() = null
override val returnType: Type
get() = jClass
override val parameterTypes: List<Type> = methods.map { it.genericReturnType }
enum class CallMode { CALL_BY_NAME, POSITIONAL_CALL }
enum class Origin { JAVA, KOTLIN }
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2018 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.jvm.internal.calls
import java.lang.reflect.Member
import java.lang.reflect.Type
internal interface Caller<out M : Member?> {
val member: M
val returnType: Type
val parameterTypes: List<Type>
fun checkArguments(args: Array<*>) {
if (arity != args.size) {
throw IllegalArgumentException("Callable expects $arity arguments, but ${args.size} were provided.")
}
}
fun call(args: Array<*>): Any?
}
internal val Caller<*>.arity: Int
get() = parameterTypes.size
@@ -1,20 +1,9 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2018 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.jvm.internal
package kotlin.reflect.jvm.internal.calls
import java.lang.reflect.Member
import java.lang.reflect.Modifier
@@ -23,35 +12,24 @@ import java.lang.reflect.Constructor as ReflectConstructor
import java.lang.reflect.Field as ReflectField
import java.lang.reflect.Method as ReflectMethod
internal abstract class FunctionCaller<out M : Member?>(
internal val member: M,
internal val returnType: Type,
internal val instanceClass: Class<*>?,
internal abstract class CallerImpl<out M : Member>(
final override val member: M,
final override val returnType: Type,
val instanceClass: Class<*>?,
valueParameterTypes: Array<Type>
) {
val parameterTypes: List<Type> =
) : Caller<M> {
override val parameterTypes: List<Type> =
instanceClass?.let { listOf(it, *valueParameterTypes) } ?: valueParameterTypes.toList()
val arity: Int
get() = parameterTypes.size
abstract fun call(args: Array<*>): Any?
protected open fun checkArguments(args: Array<*>) {
if (arity != args.size) {
throw IllegalArgumentException("Callable expects $arity arguments, but ${args.size} were provided.")
}
}
protected fun checkObjectInstance(obj: Any?) {
if (obj == null || !member!!.declaringClass.isInstance(obj)) {
if (obj == null || !member.declaringClass.isInstance(obj)) {
throw IllegalArgumentException("An object member requires the object instance passed as the first argument.")
}
}
// Constructors
class Constructor(constructor: ReflectConstructor<*>) : FunctionCaller<ReflectConstructor<*>>(
class Constructor(constructor: ReflectConstructor<*>) : CallerImpl<ReflectConstructor<*>>(
constructor,
constructor.declaringClass,
constructor.declaringClass.let { klass ->
@@ -69,7 +47,7 @@ internal abstract class FunctionCaller<out M : Member?>(
// TODO fix 'callBy' for bound (and non-bound) inner class constructor references
// See https://youtrack.jetbrains.com/issue/KT-14990
class BoundConstructor(constructor: ReflectConstructor<*>, private val boundReceiver: Any?) :
FunctionCaller<ReflectConstructor<*>>(
CallerImpl<ReflectConstructor<*>>(
constructor, constructor.declaringClass, null,
constructor.genericParameterTypes
) {
@@ -85,7 +63,7 @@ internal abstract class FunctionCaller<out M : Member?>(
method: ReflectMethod,
requiresInstance: Boolean = !Modifier.isStatic(method.modifiers),
parameterTypes: Array<Type> = method.genericParameterTypes
) : FunctionCaller<ReflectMethod>(
) : CallerImpl<ReflectMethod>(
method,
method.genericReturnType,
if (requiresInstance) method.declaringClass else null,
@@ -151,7 +129,7 @@ internal abstract class FunctionCaller<out M : Member?>(
abstract class FieldGetter(
field: ReflectField,
requiresInstance: Boolean = !Modifier.isStatic(field.modifiers)
) : FunctionCaller<ReflectField>(
) : CallerImpl<ReflectField>(
field,
field.genericType,
if (requiresInstance) field.declaringClass else null,
@@ -167,7 +145,7 @@ internal abstract class FunctionCaller<out M : Member?>(
field: ReflectField,
private val notNull: Boolean,
requiresInstance: Boolean = !Modifier.isStatic(field.modifiers)
) : FunctionCaller<ReflectField>(
) : CallerImpl<ReflectField>(
field,
Void.TYPE,
if (requiresInstance) field.declaringClass else null,
@@ -197,7 +175,7 @@ internal abstract class FunctionCaller<out M : Member?>(
}
}
class ClassCompanionFieldGetter(field: ReflectField, klass: Class<*>) : FunctionCaller<ReflectField>(
class ClassCompanionFieldGetter(field: ReflectField, klass: Class<*>) : CallerImpl<ReflectField>(
field, field.genericType, klass, emptyArray()
) {
override fun call(args: Array<*>): Any? {
@@ -237,7 +215,7 @@ internal abstract class FunctionCaller<out M : Member?>(
}
}
class ClassCompanionFieldSetter(field: ReflectField, klass: Class<*>) : FunctionCaller<ReflectField>(
class ClassCompanionFieldSetter(field: ReflectField, klass: Class<*>) : CallerImpl<ReflectField>(
field, Void.TYPE, klass, arrayOf(field.genericType)
) {
override fun call(args: Array<*>): Any? {
@@ -264,7 +242,7 @@ internal abstract class FunctionCaller<out M : Member?>(
}
}
class BoundClassCompanionFieldSetter(field: ReflectField, klass: Class<*>) : FunctionCaller<ReflectField>(
class BoundClassCompanionFieldSetter(field: ReflectField, klass: Class<*>) : CallerImpl<ReflectField>(
field, Void.TYPE, klass, arrayOf(field.genericType)
) {
override fun call(args: Array<*>): Any? {
@@ -273,12 +251,6 @@ internal abstract class FunctionCaller<out M : Member?>(
}
}
object ThrowingCaller : FunctionCaller<Nothing?>(null, Void.TYPE, null, emptyArray()) {
override fun call(args: Array<*>): Any? {
throw UnsupportedOperationException("call/callBy are not supported for this declaration.")
}
}
companion object {
@Suppress("UNCHECKED_CAST")
inline fun <reified T> Array<out T>.dropFirst(): Array<T> =
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2018 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.jvm.internal.calls
import java.lang.reflect.Type
internal object ThrowingCaller : Caller<Nothing?> {
override val member: Nothing?
get() = null
override val parameterTypes: List<Type>
get() = emptyList()
override val returnType: Type
get() = Void.TYPE
override fun call(args: Array<*>): Any? {
throw UnsupportedOperationException("call/callBy are not supported for this declaration.")
}
}
@@ -40,6 +40,7 @@ import kotlin.jvm.internal.FunctionReference
import kotlin.jvm.internal.PropertyReference
import kotlin.reflect.KVisibility
import kotlin.reflect.full.IllegalCallableAccessException
import kotlin.reflect.jvm.internal.calls.createAnnotationInstance
import kotlin.reflect.jvm.internal.components.ReflectAnnotationSource
import kotlin.reflect.jvm.internal.components.ReflectKotlinClass
import kotlin.reflect.jvm.internal.components.RuntimeSourceElementFactory