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:
@@ -15,16 +15,17 @@ import java.lang.reflect.WildcardType
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.coroutines.Continuation
|
import kotlin.coroutines.Continuation
|
||||||
import kotlin.reflect.*
|
import kotlin.reflect.*
|
||||||
|
import kotlin.reflect.jvm.internal.calls.Caller
|
||||||
import kotlin.reflect.jvm.javaType
|
import kotlin.reflect.jvm.javaType
|
||||||
|
|
||||||
internal abstract class KCallableImpl<out R> : KCallable<R> {
|
internal abstract class KCallableImpl<out R> : KCallable<R> {
|
||||||
abstract val descriptor: CallableMemberDescriptor
|
abstract val descriptor: CallableMemberDescriptor
|
||||||
|
|
||||||
// The instance which is used to perform a positional call, i.e. `call`
|
// 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`
|
// 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
|
abstract val container: KDeclarationContainerImpl
|
||||||
|
|
||||||
|
|||||||
@@ -25,11 +25,12 @@ import java.lang.reflect.Modifier
|
|||||||
import kotlin.jvm.internal.CallableReference
|
import kotlin.jvm.internal.CallableReference
|
||||||
import kotlin.jvm.internal.FunctionBase
|
import kotlin.jvm.internal.FunctionBase
|
||||||
import kotlin.reflect.KFunction
|
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.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(
|
internal class KFunctionImpl private constructor(
|
||||||
override val container: KDeclarationContainerImpl,
|
override val container: KDeclarationContainerImpl,
|
||||||
@@ -56,7 +57,7 @@ internal class KFunctionImpl private constructor(
|
|||||||
|
|
||||||
override val name: String get() = descriptor.name.asString()
|
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 jvmSignature = RuntimeTypeMapper.mapSignature(descriptor)
|
||||||
val member: Member? = when (jvmSignature) {
|
val member: Member? = when (jvmSignature) {
|
||||||
is KotlinConstructor -> {
|
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 jvmSignature = RuntimeTypeMapper.mapSignature(descriptor)
|
||||||
val member: Member? = when (jvmSignature) {
|
val member: Member? = when (jvmSignature) {
|
||||||
is KotlinFunction -> {
|
is KotlinFunction -> {
|
||||||
@@ -132,16 +133,16 @@ internal class KFunctionImpl private constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun createStaticMethodCaller(member: Method) =
|
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) =
|
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) =
|
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<*>) =
|
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
|
override val arity: Int get() = caller.arity
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ import kotlin.reflect.KMutableProperty
|
|||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
import kotlin.reflect.full.IllegalPropertyDelegateAccessException
|
import kotlin.reflect.full.IllegalPropertyDelegateAccessException
|
||||||
import kotlin.reflect.jvm.internal.JvmPropertySignature.*
|
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(
|
internal abstract class KPropertyImpl<out R> private constructor(
|
||||||
override val container: KDeclarationContainerImpl,
|
override val container: KDeclarationContainerImpl,
|
||||||
@@ -99,9 +102,9 @@ internal abstract class KPropertyImpl<out R> private constructor(
|
|||||||
|
|
||||||
override val descriptor: PropertyDescriptor get() = _descriptor()
|
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
|
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 container: KDeclarationContainerImpl get() = property.container
|
||||||
|
|
||||||
override val defaultCaller: FunctionCaller<*>? get() = null
|
override val defaultCaller: Caller<*>? get() = null
|
||||||
|
|
||||||
override val isBound: Boolean get() = property.isBound
|
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)
|
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)
|
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)
|
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)
|
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)) {
|
if (KDeclarationContainerImpl.LOCAL_PROPERTY_SIGNATURE.matches(property.signature)) {
|
||||||
return FunctionCaller.ThrowingCaller
|
return ThrowingCaller
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isInsideClassCompanionObject(): Boolean {
|
fun isInsideClassCompanionObject(): Boolean {
|
||||||
@@ -201,33 +204,33 @@ private fun KPropertyImpl.Accessor<*, *>.computeCallerForAccessor(isGetter: Bool
|
|||||||
fun isNotNullProperty() =
|
fun isNotNullProperty() =
|
||||||
!TypeUtils.isNullableType(property.descriptor.type)
|
!TypeUtils.isNullableType(property.descriptor.type)
|
||||||
|
|
||||||
fun computeFieldCaller(field: Field): FunctionCaller<Field> = when {
|
fun computeFieldCaller(field: Field): Caller<Field> = when {
|
||||||
isInsideClassCompanionObject() || isInsideInterfaceCompanionObjectWithJvmField() -> {
|
isInsideClassCompanionObject() || isInsideInterfaceCompanionObjectWithJvmField() -> {
|
||||||
val klass = (descriptor.containingDeclaration as ClassDescriptor).toJavaClass()!!
|
val klass = (descriptor.containingDeclaration as ClassDescriptor).toJavaClass()!!
|
||||||
if (isGetter)
|
if (isGetter)
|
||||||
if (isBound) FunctionCaller.BoundClassCompanionFieldGetter(field, klass)
|
if (isBound) CallerImpl.BoundClassCompanionFieldGetter(field, klass)
|
||||||
else FunctionCaller.ClassCompanionFieldGetter(field, klass)
|
else CallerImpl.ClassCompanionFieldGetter(field, klass)
|
||||||
else
|
else
|
||||||
if (isBound) FunctionCaller.BoundClassCompanionFieldSetter(field, klass)
|
if (isBound) CallerImpl.BoundClassCompanionFieldSetter(field, klass)
|
||||||
else FunctionCaller.ClassCompanionFieldSetter(field, klass)
|
else CallerImpl.ClassCompanionFieldSetter(field, klass)
|
||||||
}
|
}
|
||||||
!Modifier.isStatic(field.modifiers) ->
|
!Modifier.isStatic(field.modifiers) ->
|
||||||
if (isGetter)
|
if (isGetter)
|
||||||
if (isBound) FunctionCaller.BoundInstanceFieldGetter(field, property.boundReceiver)
|
if (isBound) CallerImpl.BoundInstanceFieldGetter(field, property.boundReceiver)
|
||||||
else FunctionCaller.InstanceFieldGetter(field)
|
else CallerImpl.InstanceFieldGetter(field)
|
||||||
else
|
else
|
||||||
if (isBound) FunctionCaller.BoundInstanceFieldSetter(field, isNotNullProperty(), property.boundReceiver)
|
if (isBound) CallerImpl.BoundInstanceFieldSetter(field, isNotNullProperty(), property.boundReceiver)
|
||||||
else FunctionCaller.InstanceFieldSetter(field, isNotNullProperty())
|
else CallerImpl.InstanceFieldSetter(field, isNotNullProperty())
|
||||||
isJvmStaticProperty() ->
|
isJvmStaticProperty() ->
|
||||||
if (isGetter)
|
if (isGetter)
|
||||||
if (isBound) FunctionCaller.BoundJvmStaticInObjectFieldGetter(field)
|
if (isBound) CallerImpl.BoundJvmStaticInObjectFieldGetter(field)
|
||||||
else FunctionCaller.JvmStaticInObjectFieldGetter(field)
|
else CallerImpl.JvmStaticInObjectFieldGetter(field)
|
||||||
else
|
else
|
||||||
if (isBound) FunctionCaller.BoundJvmStaticInObjectFieldSetter(field, isNotNullProperty())
|
if (isBound) CallerImpl.BoundJvmStaticInObjectFieldSetter(field, isNotNullProperty())
|
||||||
else FunctionCaller.JvmStaticInObjectFieldSetter(field, isNotNullProperty())
|
else CallerImpl.JvmStaticInObjectFieldSetter(field, isNotNullProperty())
|
||||||
else ->
|
else ->
|
||||||
if (isGetter) FunctionCaller.StaticFieldGetter(field)
|
if (isGetter) CallerImpl.StaticFieldGetter(field)
|
||||||
else FunctionCaller.StaticFieldSetter(field, isNotNullProperty())
|
else CallerImpl.StaticFieldSetter(field, isNotNullProperty())
|
||||||
}
|
}
|
||||||
|
|
||||||
val jvmSignature = RuntimeTypeMapper.mapPropertySignature(property.descriptor)
|
val jvmSignature = RuntimeTypeMapper.mapPropertySignature(property.descriptor)
|
||||||
@@ -250,18 +253,17 @@ private fun KPropertyImpl.Accessor<*, *>.computeCallerForAccessor(isGetter: Bool
|
|||||||
|
|
||||||
when {
|
when {
|
||||||
accessor == null -> computeFieldCaller(
|
accessor == null -> computeFieldCaller(
|
||||||
property.javaField
|
property.javaField ?: throw KotlinReflectionInternalError("No accessors or field is found for property $property")
|
||||||
?: throw KotlinReflectionInternalError("No accessors or field is found for property $property")
|
|
||||||
)
|
)
|
||||||
!Modifier.isStatic(accessor.modifiers) ->
|
!Modifier.isStatic(accessor.modifiers) ->
|
||||||
if (isBound) FunctionCaller.BoundInstanceMethod(accessor, property.boundReceiver)
|
if (isBound) CallerImpl.BoundInstanceMethod(accessor, property.boundReceiver)
|
||||||
else FunctionCaller.InstanceMethod(accessor)
|
else CallerImpl.InstanceMethod(accessor)
|
||||||
isJvmStaticProperty() ->
|
isJvmStaticProperty() ->
|
||||||
if (isBound) FunctionCaller.BoundJvmStaticInObject(accessor)
|
if (isBound) CallerImpl.BoundJvmStaticInObject(accessor)
|
||||||
else FunctionCaller.JvmStaticInObject(accessor)
|
else CallerImpl.JvmStaticInObject(accessor)
|
||||||
else ->
|
else ->
|
||||||
if (isBound) FunctionCaller.BoundStaticMethod(accessor, property.boundReceiver)
|
if (isBound) CallerImpl.BoundStaticMethod(accessor, property.boundReceiver)
|
||||||
else FunctionCaller.StaticMethod(accessor)
|
else CallerImpl.StaticMethod(accessor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is JavaField -> {
|
is JavaField -> {
|
||||||
@@ -273,22 +275,21 @@ private fun KPropertyImpl.Accessor<*, *>.computeCallerForAccessor(isGetter: Bool
|
|||||||
else jvmSignature.setterMethod ?: throw KotlinReflectionInternalError(
|
else jvmSignature.setterMethod ?: throw KotlinReflectionInternalError(
|
||||||
"No source found for setter of Java method property: ${jvmSignature.getterMethod}"
|
"No source found for setter of Java method property: ${jvmSignature.getterMethod}"
|
||||||
)
|
)
|
||||||
if (isBound) FunctionCaller.BoundInstanceMethod(method, property.boundReceiver)
|
if (isBound) CallerImpl.BoundInstanceMethod(method, property.boundReceiver)
|
||||||
else FunctionCaller.InstanceMethod(method)
|
else CallerImpl.InstanceMethod(method)
|
||||||
}
|
}
|
||||||
is MappedKotlinProperty -> {
|
is MappedKotlinProperty -> {
|
||||||
val signature =
|
val signature =
|
||||||
if (isGetter) jvmSignature.getterSignature
|
if (isGetter) jvmSignature.getterSignature
|
||||||
else (jvmSignature.setterSignature
|
else (jvmSignature.setterSignature ?: throw KotlinReflectionInternalError("No setter found for property $property"))
|
||||||
?: throw KotlinReflectionInternalError("No setter found for property $property"))
|
|
||||||
val accessor = property.container.findMethodBySignature(
|
val accessor = property.container.findMethodBySignature(
|
||||||
signature.methodName, signature.methodDesc, descriptor.isPublicInBytecode
|
signature.methodName, signature.methodDesc, descriptor.isPublicInBytecode
|
||||||
) ?: throw KotlinReflectionInternalError("No accessor found for property $property")
|
) ?: throw KotlinReflectionInternalError("No accessor found for property $property")
|
||||||
|
|
||||||
assert(!Modifier.isStatic(accessor.modifiers)) { "Mapped property cannot have a static accessor: $property" }
|
assert(!Modifier.isStatic(accessor.modifiers)) { "Mapped property cannot have a static accessor: $property" }
|
||||||
|
|
||||||
return if (isBound) FunctionCaller.BoundInstanceMethod(accessor, property.boundReceiver)
|
return if (isBound) CallerImpl.BoundInstanceMethod(accessor, property.boundReceiver)
|
||||||
else FunctionCaller.InstanceMethod(accessor)
|
else CallerImpl.InstanceMethod(accessor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-17
@@ -1,24 +1,15 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2016 JetBrains s.r.o.
|
* 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.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package kotlin.reflect.jvm.internal
|
package kotlin.reflect.jvm.internal.calls
|
||||||
|
|
||||||
import java.lang.reflect.Proxy
|
import java.lang.reflect.Proxy
|
||||||
|
import java.lang.reflect.Type
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
|
||||||
import kotlin.reflect.jvm.internal.structure.wrapperByPrimitive
|
import kotlin.reflect.jvm.internal.structure.wrapperByPrimitive
|
||||||
import java.lang.reflect.Method as ReflectMethod
|
import java.lang.reflect.Method as ReflectMethod
|
||||||
|
|
||||||
@@ -28,9 +19,15 @@ internal class AnnotationConstructorCaller(
|
|||||||
private val callMode: CallMode,
|
private val callMode: CallMode,
|
||||||
origin: Origin,
|
origin: Origin,
|
||||||
private val methods: List<ReflectMethod> = parameterNames.map { name -> jClass.getDeclaredMethod(name) }
|
private val methods: List<ReflectMethod> = parameterNames.map { name -> jClass.getDeclaredMethod(name) }
|
||||||
) : FunctionCaller<Nothing?>(
|
) : Caller<Nothing?> {
|
||||||
null, jClass, null, methods.map { it.genericReturnType }.toTypedArray()
|
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 CallMode { CALL_BY_NAME, POSITIONAL_CALL }
|
||||||
|
|
||||||
enum class Origin { JAVA, KOTLIN }
|
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
|
||||||
+18
-46
@@ -1,20 +1,9 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* 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.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package kotlin.reflect.jvm.internal
|
package kotlin.reflect.jvm.internal.calls
|
||||||
|
|
||||||
import java.lang.reflect.Member
|
import java.lang.reflect.Member
|
||||||
import java.lang.reflect.Modifier
|
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.Field as ReflectField
|
||||||
import java.lang.reflect.Method as ReflectMethod
|
import java.lang.reflect.Method as ReflectMethod
|
||||||
|
|
||||||
internal abstract class FunctionCaller<out M : Member?>(
|
internal abstract class CallerImpl<out M : Member>(
|
||||||
internal val member: M,
|
final override val member: M,
|
||||||
internal val returnType: Type,
|
final override val returnType: Type,
|
||||||
internal val instanceClass: Class<*>?,
|
val instanceClass: Class<*>?,
|
||||||
valueParameterTypes: Array<Type>
|
valueParameterTypes: Array<Type>
|
||||||
) {
|
) : Caller<M> {
|
||||||
val parameterTypes: List<Type> =
|
override val parameterTypes: List<Type> =
|
||||||
instanceClass?.let { listOf(it, *valueParameterTypes) } ?: valueParameterTypes.toList()
|
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?) {
|
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.")
|
throw IllegalArgumentException("An object member requires the object instance passed as the first argument.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
class Constructor(constructor: ReflectConstructor<*>) : FunctionCaller<ReflectConstructor<*>>(
|
class Constructor(constructor: ReflectConstructor<*>) : CallerImpl<ReflectConstructor<*>>(
|
||||||
constructor,
|
constructor,
|
||||||
constructor.declaringClass,
|
constructor.declaringClass,
|
||||||
constructor.declaringClass.let { klass ->
|
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
|
// TODO fix 'callBy' for bound (and non-bound) inner class constructor references
|
||||||
// See https://youtrack.jetbrains.com/issue/KT-14990
|
// See https://youtrack.jetbrains.com/issue/KT-14990
|
||||||
class BoundConstructor(constructor: ReflectConstructor<*>, private val boundReceiver: Any?) :
|
class BoundConstructor(constructor: ReflectConstructor<*>, private val boundReceiver: Any?) :
|
||||||
FunctionCaller<ReflectConstructor<*>>(
|
CallerImpl<ReflectConstructor<*>>(
|
||||||
constructor, constructor.declaringClass, null,
|
constructor, constructor.declaringClass, null,
|
||||||
constructor.genericParameterTypes
|
constructor.genericParameterTypes
|
||||||
) {
|
) {
|
||||||
@@ -85,7 +63,7 @@ internal abstract class FunctionCaller<out M : Member?>(
|
|||||||
method: ReflectMethod,
|
method: ReflectMethod,
|
||||||
requiresInstance: Boolean = !Modifier.isStatic(method.modifiers),
|
requiresInstance: Boolean = !Modifier.isStatic(method.modifiers),
|
||||||
parameterTypes: Array<Type> = method.genericParameterTypes
|
parameterTypes: Array<Type> = method.genericParameterTypes
|
||||||
) : FunctionCaller<ReflectMethod>(
|
) : CallerImpl<ReflectMethod>(
|
||||||
method,
|
method,
|
||||||
method.genericReturnType,
|
method.genericReturnType,
|
||||||
if (requiresInstance) method.declaringClass else null,
|
if (requiresInstance) method.declaringClass else null,
|
||||||
@@ -151,7 +129,7 @@ internal abstract class FunctionCaller<out M : Member?>(
|
|||||||
abstract class FieldGetter(
|
abstract class FieldGetter(
|
||||||
field: ReflectField,
|
field: ReflectField,
|
||||||
requiresInstance: Boolean = !Modifier.isStatic(field.modifiers)
|
requiresInstance: Boolean = !Modifier.isStatic(field.modifiers)
|
||||||
) : FunctionCaller<ReflectField>(
|
) : CallerImpl<ReflectField>(
|
||||||
field,
|
field,
|
||||||
field.genericType,
|
field.genericType,
|
||||||
if (requiresInstance) field.declaringClass else null,
|
if (requiresInstance) field.declaringClass else null,
|
||||||
@@ -167,7 +145,7 @@ internal abstract class FunctionCaller<out M : Member?>(
|
|||||||
field: ReflectField,
|
field: ReflectField,
|
||||||
private val notNull: Boolean,
|
private val notNull: Boolean,
|
||||||
requiresInstance: Boolean = !Modifier.isStatic(field.modifiers)
|
requiresInstance: Boolean = !Modifier.isStatic(field.modifiers)
|
||||||
) : FunctionCaller<ReflectField>(
|
) : CallerImpl<ReflectField>(
|
||||||
field,
|
field,
|
||||||
Void.TYPE,
|
Void.TYPE,
|
||||||
if (requiresInstance) field.declaringClass else null,
|
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()
|
field, field.genericType, klass, emptyArray()
|
||||||
) {
|
) {
|
||||||
override fun call(args: Array<*>): Any? {
|
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)
|
field, Void.TYPE, klass, arrayOf(field.genericType)
|
||||||
) {
|
) {
|
||||||
override fun call(args: Array<*>): Any? {
|
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)
|
field, Void.TYPE, klass, arrayOf(field.genericType)
|
||||||
) {
|
) {
|
||||||
override fun call(args: Array<*>): Any? {
|
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 {
|
companion object {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
inline fun <reified T> Array<out T>.dropFirst(): Array<T> =
|
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.jvm.internal.PropertyReference
|
||||||
import kotlin.reflect.KVisibility
|
import kotlin.reflect.KVisibility
|
||||||
import kotlin.reflect.full.IllegalCallableAccessException
|
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.ReflectAnnotationSource
|
||||||
import kotlin.reflect.jvm.internal.components.ReflectKotlinClass
|
import kotlin.reflect.jvm.internal.components.ReflectKotlinClass
|
||||||
import kotlin.reflect.jvm.internal.components.RuntimeSourceElementFactory
|
import kotlin.reflect.jvm.internal.components.RuntimeSourceElementFactory
|
||||||
|
|||||||
Reference in New Issue
Block a user