Added directly call the reflection class creation method

This commit is contained in:
2022-04-04 01:38:57 +08:00
parent d4e154384c
commit 02119f37d7
2 changed files with 36 additions and 6 deletions

View File

@@ -136,6 +136,36 @@ fun Class<*>.method(initiate: MethodFinder.() -> Unit) = MethodFinder(classSet =
*/ */
fun Class<*>.constructor(initiate: ConstructorFinder.() -> Unit) = ConstructorFinder(classSet = this).apply(initiate).build() fun Class<*>.constructor(initiate: ConstructorFinder.() -> Unit) = ConstructorFinder(classSet = this).apply(initiate).build()
/**
* 调用当前实例中的变量
* @param initiate 查找方法体
* @return [FieldFinder.Result.Instance]
*/
fun Any.field(initiate: FieldFinder.() -> Unit) = javaClass.field(initiate).get(this)
/**
* 调用当前实例中的方法
* @param initiate 查找方法体
* @return [MethodFinder.Result.Instance]
*/
fun Any.method(initiate: MethodFinder.() -> Unit) = javaClass.method(initiate).get(this)
/**
* 通过构造方法创建新实例 - 指定类型 [T]
* @param param 方法参数
* @param initiate 查找方法体
* @return [T] or null
*/
fun <T> Class<*>.construct(vararg param: Any?, initiate: ConstructorFinder.() -> Unit = {}) = constructor(initiate).get().newInstance<T>(*param)
/**
* 通过构造方法创建新实例 - 任意类型 [Any]
* @param param 方法参数
* @param initiate 查找方法体
* @return [Any] or null
*/
fun Class<*>.constructAny(vararg param: Any?, initiate: ConstructorFinder.() -> Unit = {}) = construct<Any>(*param, initiate)
/** /**
* 遍历当前类中的所有方法 * 遍历当前类中的所有方法
* @param callback 回调 - ([Int] 下标,[Method] 实例) * @param callback 回调 - ([Int] 下标,[Method] 实例)

View File

@@ -306,9 +306,9 @@ internal object ReflectionTool {
): Constructor<*> { ): Constructor<*> {
paramTypes?.takeIf { it.isNotEmpty() } paramTypes?.takeIf { it.isNotEmpty() }
?.forEachIndexed { p, it -> if (it == UndefinedType) error("Constructor match paramType[$p] class is not found") } ?.forEachIndexed { p, it -> if (it == UndefinedType) error("Constructor match paramType[$p] class is not found") }
if (orderIndex == null && matchIndex == null && paramCount < 0 && paramTypes == null && modifiers == null) val paramCountR =
error("You must set a condition when finding a Constructor") if (orderIndex == null && matchIndex == null && paramCount < 0 && paramTypes == null && modifiers == null) 0 else paramCount
val hashCode = ("[$orderIndex][$matchIndex][$paramCount][${paramTypes.typeOfString()}][$modifiers][$classSet]").hashCode() val hashCode = ("[$orderIndex][$matchIndex][$paramCountR][${paramTypes.typeOfString()}][$modifiers][$classSet]").hashCode()
return MemberCacheStore.findConstructor(hashCode) ?: let { return MemberCacheStore.findConstructor(hashCode) ?: let {
var constructor: Constructor<*>? = null var constructor: Constructor<*>? = null
classSet?.declaredConstructors?.apply { classSet?.declaredConstructors?.apply {
@@ -316,7 +316,7 @@ internal object ReflectionTool {
var paramCountIndex = -1 var paramCountIndex = -1
var modifyIndex = -1 var modifyIndex = -1
val paramCountLastIndex = val paramCountLastIndex =
if (paramCount >= 0 && matchIndex != null) filter { paramCount == it.parameterTypes.size }.lastIndex else -1 if (paramCountR >= 0 && matchIndex != null) filter { paramCountR == it.parameterTypes.size }.lastIndex else -1
val paramTypeLastIndex = val paramTypeLastIndex =
if (paramTypes != null && matchIndex != null) filter { arrayContentsEq(paramTypes, it.parameterTypes) }.lastIndex else -1 if (paramTypes != null && matchIndex != null) filter { arrayContentsEq(paramTypes, it.parameterTypes) }.lastIndex else -1
val modifyLastIndex = if (modifiers != null && matchIndex != null) filter { modifiers.contains(it) }.lastIndex else -1 val modifyLastIndex = if (modifiers != null && matchIndex != null) filter { modifiers.contains(it) }.lastIndex else -1
@@ -324,7 +324,7 @@ internal object ReflectionTool {
var isMatched = false var isMatched = false
var conditions = true var conditions = true
if (paramCount >= 0) if (paramCount >= 0)
conditions = (it.parameterTypes.size == paramCount).let { conditions = (it.parameterTypes.size == paramCountR).let {
if (it) paramCountIndex++ if (it) paramCountIndex++
isMatched = true isMatched = true
it && (matchIndex == null || it && (matchIndex == null ||
@@ -381,7 +381,7 @@ internal object ReflectionTool {
} }
}] " + }] " +
"paramCount:[${ "paramCount:[${
paramCount.takeIf { it >= 0 || it == -2 } paramCountR.takeIf { it >= 0 || it == -2 }
?.toString()?.replace(oldValue = "-2", newValue = "last") ?: "unspecified" ?.toString()?.replace(oldValue = "-2", newValue = "last") ?: "unspecified"
}] " + }] " +
"paramTypes:[${paramTypes.typeOfString()}] " + "paramTypes:[${paramTypes.typeOfString()}] " +