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

com.itangcent.common.utils.JavaClassHelper.kt Maven / Gradle / Ivy

package com.itangcent.common.utils

object JavaClassHelper {

//    private val EMPTY_CLASS_ARRAYCLASS_ARRAY = arrayOfNulls>(0)

    fun  newInstance(type: Class): T {
        try {
            val constructor = type.getConstructor()
            return constructor.newInstance()
        } catch (e: NoSuchMethodException) {
            throw RuntimeException("Can't initialize class ${type.name}, no ()", e)
        }
    }

    fun  newInstance(type: Class, vararg params: Pair>): T {
        val (argList, argTypes) = params.unzip()

        try {
            val constructor = type.getConstructor(*argTypes.toTypedArray())
            return constructor.newInstance(*argList.toTypedArray())
        } catch (e: NoSuchMethodException) {
            throw RuntimeException("Can't initialize class ${type.name}, no (${argTypes.joinToString()})", e)
        }
    }

//
//    fun  newInstance(type: Class, vararg params: Any): Any {
//        var constructor: Constructor<*>? = null
//        try {
//            if (params.isEmpty()) {
//                constructor = type.getConstructor(*ClassHelper.EMPTY_CLASS_ARRAY)
//            } else {
//                val paramTypes: Array> =
//                        params.toList()
//                                .stream()
//                                .map { param -> param::class.java }
//                                .toTypedArray()
//                constructor = type.getConstructor(*paramTypes)
//            }
//        } catch (e: NoSuchMethodException) {
//            // ignore
//        }
//
//
//        if (constructor == null) {
//            val constructors = type.constructors
//            if (constructors == null || constructors.size == 0) {
//                throw IllegalArgumentException("Illegal params to instance Class[" + type.name + "]")
//            }
//            for (cst in constructors) {
//                val paramCls = cst.parameterTypes
//                if (isAssignable(params, paramCls)) {
//                    constructor = cst
//                    break
//                }
//            }
//        }
//        if (constructor == null) {
//            throw IllegalArgumentException("Illegal params to instance Class[" + type.name + "]")
//        }
//        try {
//            return constructor.newInstance(*params)
//        } catch (e: InstantiationException) {
//            throw IllegalArgumentException("Illegal params to instance Class[" + type.name + "]")
//        } catch (e: IllegalAccessException) {
//            throw IllegalArgumentException("Illegal params to instance Class[" + type.name + "]")
//        } catch (e: InvocationTargetException) {
//            throw IllegalArgumentException("Illegal params to instance Class[" + type.name + "]")
//        }
//
//    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy