All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.reflect.jvm.internal.FunctionCaller.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * 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.
 */

package kotlin.reflect.jvm.internal

import java.lang.reflect.Member
import java.lang.reflect.Modifier
import java.lang.reflect.Type
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(
        internal val member: M,
        internal val returnType: Type,
        internal val instanceClass: Class<*>?,
        valueParameterTypes: Array
) {
    internal val parameterTypes: List =
            instanceClass?.let { listOf(it, *valueParameterTypes) } ?:
            valueParameterTypes.toList()

    abstract fun call(args: Array<*>): Any?

    protected open fun checkArguments(args: Array<*>) {
        if (parameterTypes.size != args.size) {
            throw IllegalArgumentException("Callable expects ${parameterTypes.size} arguments, but ${args.size} were provided.")
        }
    }

    protected fun checkObjectInstance(obj: Any?) {
        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>(
            constructor,
            constructor.declaringClass,
            constructor.declaringClass.let { klass ->
                val outerClass = klass.declaringClass
                if (outerClass != null && !Modifier.isStatic(klass.modifiers)) outerClass else null
            },
            constructor.genericParameterTypes
    ) {
        override fun call(args: Array<*>): Any? {
            checkArguments(args)
            return member.newInstance(*args)
        }
    }

    // Methods

    abstract class Method(
            method: ReflectMethod,
            requiresInstance: Boolean = !Modifier.isStatic(method.modifiers)
    ) : FunctionCaller(
            method,
            method.genericReturnType,
            if (requiresInstance) method.declaringClass else null,
            method.genericParameterTypes
    ) {
        private val isVoidMethod = returnType == Void.TYPE

        protected fun callMethod(instance: Any?, args: Array<*>): Any? {
            val result = member.invoke(instance, *args)

            // If this is a Unit function, the method returns void, Method#invoke returns null, while we should return Unit
            return if (isVoidMethod) Unit else result
        }
    }

    class StaticMethod(method: ReflectMethod) : Method(method) {
        override fun call(args: Array<*>): Any? {
            checkArguments(args)
            return callMethod(null, args)
        }
    }

    class InstanceMethod(method: ReflectMethod) : Method(method) {
        override fun call(args: Array<*>): Any? {
            checkArguments(args)
            return callMethod(args[0], args.asList().subList(1, args.size).toTypedArray())
        }
    }

    class JvmStaticInObject(method: ReflectMethod) : Method(method, requiresInstance = true) {
        override fun call(args: Array<*>): Any? {
            checkArguments(args)
            checkObjectInstance(args.firstOrNull())
            return callMethod(null, args.asList().subList(1, args.size).toTypedArray())
        }
    }

    // Field accessors

    abstract class FieldGetter(
            field: ReflectField,
            requiresInstance: Boolean = !Modifier.isStatic(field.modifiers)
    ) : FunctionCaller(
            field,
            field.genericType,
            if (requiresInstance) field.declaringClass else null,
            emptyArray()
    ) {
        override fun call(args: Array<*>): Any? {
            checkArguments(args)
            return member.get(if (instanceClass != null) args.first() else null)
        }
    }

    abstract class FieldSetter(
            field: ReflectField,
            private val notNull: Boolean,
            requiresInstance: Boolean = !Modifier.isStatic(field.modifiers)
    ) : FunctionCaller(
            field,
            Void.TYPE,
            if (requiresInstance) field.declaringClass else null,
            arrayOf(field.genericType)
    ) {
        override fun checkArguments(args: Array<*>) {
            super.checkArguments(args)
            if (notNull && args.last() == null) {
                throw IllegalArgumentException("null is not allowed as a value for this property.")
            }
        }

        override fun call(args: Array<*>): Any? {
            checkArguments(args)
            return member.set(if (instanceClass != null) args.first() else null, args.last())
        }
    }

    class StaticFieldGetter(field: ReflectField) : FieldGetter(field)

    class InstanceFieldGetter(field: ReflectField) : FieldGetter(field)

    class JvmStaticInObjectFieldGetter(field: ReflectField) : FieldGetter(field, requiresInstance = true) {
        override fun checkArguments(args: Array<*>) {
            super.checkArguments(args)
            checkObjectInstance(args.firstOrNull())
        }
    }

    class ClassCompanionFieldGetter(
            field: ReflectField,
            klass: Class<*>
    ) : FunctionCaller(
            field,
            field.genericType,
            klass,
            emptyArray()
    ) {
        override fun call(args: Array<*>): Any? {
            checkArguments(args)
            return member.get(args.first())
        }
    }

    class StaticFieldSetter(field: ReflectField, notNull: Boolean) : FieldSetter(field, notNull)

    class InstanceFieldSetter(field: ReflectField, notNull: Boolean) : FieldSetter(field, notNull)

    class JvmStaticInObjectFieldSetter(field: ReflectField, notNull: Boolean) : FieldSetter(field, notNull, requiresInstance = true) {
        override fun checkArguments(args: Array<*>) {
            super.checkArguments(args)
            checkObjectInstance(args.firstOrNull())
        }
    }

    class ClassCompanionFieldSetter(
            field: ReflectField,
            klass: Class<*>
    ) : FunctionCaller(
            field,
            Void.TYPE,
            klass,
            arrayOf(field.genericType)
    ) {
        override fun call(args: Array<*>): Any? {
            checkArguments(args)
            return member.set(instanceClass, args.last())
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy