Add new function

This commit is contained in:
2022-03-25 00:21:50 +08:00
parent 2c11f1bde5
commit 6bf1b55d14
3 changed files with 141 additions and 8 deletions

View File

@@ -59,7 +59,7 @@ class ConstructorFinder(
private var remedyPlansCallback: (() -> Unit)? = null
/** [Constructor] 参数数组 */
private var params: Array<out Class<*>>? = null
private var paramTypes: Array<out Class<*>>? = null
/**
* [Constructor] 参数
@@ -71,7 +71,7 @@ class ConstructorFinder(
*/
fun param(vararg paramType: Class<*>) {
if (paramType.isEmpty()) error("paramType is empty, please delete param() method")
params = paramType
paramTypes = paramType
}
/**
@@ -80,8 +80,8 @@ class ConstructorFinder(
* @throws NoSuchMethodError 如果找不到构造方法
*/
private val result
get() = if (params != null)
ReflectionUtils.findConstructorExact(classSet, *params!!)
get() = if (paramTypes != null)
ReflectionUtils.findConstructorExact(classSet, *paramTypes!!)
else ReflectionUtils.findConstructorExact(classSet)
/**

View File

@@ -141,6 +141,78 @@ class FieldFinder(
*/
fun <T> of(instance: Any? = null) = get(instance).self as? T?
/**
* 得到变量的 [Int] 实例
*
* - ❗请确认目标变量的类型 - 发生错误会返回默认值
* @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null
* @return [Int] 取不到返回 0
*/
fun ofInt(instance: Any? = null) = of(instance) ?: 0
/**
* 得到变量的 [Long] 实例
*
* - ❗请确认目标变量的类型 - 发生错误会返回默认值
* @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null
* @return [Long] 取不到返回 0L
*/
fun ofLong(instance: Any? = null) = of(instance) ?: 0L
/**
* 得到变量的 [Short] 实例
*
* - ❗请确认目标变量的类型 - 发生错误会返回默认值
* @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null
* @return [Short] 取不到返回 0
*/
fun ofShort(instance: Any? = null) = of<Short?>(instance) ?: 0
/**
* 得到变量的 [Double] 实例
*
* - ❗请确认目标变量的类型 - 发生错误会返回默认值
* @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null
* @return [Double] 取不到返回 0.0
*/
fun ofDouble(instance: Any? = null) = of(instance) ?: 0.0
/**
* 得到变量的 [Float] 实例
*
* - ❗请确认目标变量的类型 - 发生错误会返回默认值
* @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null
* @return [Float] 取不到返回 0f
*/
fun ofFloat(instance: Any? = null) = of(instance) ?: 0f
/**
* 得到变量的 [String] 实例
*
* - ❗请确认目标变量的类型 - 发生错误会返回默认值
* @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null
* @return [String] 取不到返回 ""
*/
fun ofString(instance: Any? = null) = of(instance) ?: ""
/**
* 得到变量的 [Boolean] 实例
*
* - ❗请确认目标变量的类型 - 发生错误会返回默认值
* @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null
* @return [Boolean] 取不到返回 false
*/
fun ofBoolean(instance: Any? = null) = of(instance) ?: false
/**
* 得到变量的 [Any] 实例
*
* - ❗请确认目标变量的类型 - 发生错误会返回默认值
* @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null
* @return [Any] or null
*/
fun ofAny(instance: Any? = null) = of<Any?>(instance)
/**
* 得到变量本身
* @return [Field] or null

View File

@@ -60,7 +60,7 @@ class MethodFinder(
private var remedyPlansCallback: (() -> Unit)? = null
/** [Method] 参数数组 */
private var params: Array<out Class<*>>? = null
private var paramTypes: Array<out Class<*>>? = null
/**
* [Method] 名称
@@ -86,7 +86,7 @@ class MethodFinder(
*/
fun param(vararg paramType: Class<*>) {
if (paramType.isEmpty()) error("paramType is empty, please delete param() method")
params = paramType
paramTypes = paramType
}
/**
@@ -96,8 +96,8 @@ class MethodFinder(
* @throws NoSuchMethodError 如果找不到方法
*/
private val result
get() = if (params != null)
ReflectionUtils.findMethodBestMatch(classSet, returnType, name, *params!!)
get() = if (paramTypes != null)
ReflectionUtils.findMethodBestMatch(classSet, returnType, name, *paramTypes!!)
else ReflectionUtils.findMethodNoParam(classSet, returnType, name)
/**
@@ -332,6 +332,67 @@ class MethodFinder(
*/
fun <T> invoke(vararg param: Any?) = baseCall(*param) as? T?
/**
* 执行方法 - 指定 [Int] 返回值类型
*
* - ❗请确认目标方法的返回值 - 发生错误会返回默认值
* @param param 方法参数
* @return [Int] 取不到返回 0
*/
fun callInt(vararg param: Any?) = invoke(param) ?: 0
/**
* 执行方法 - 指定 [Long] 返回值类型
*
* - ❗请确认目标方法的返回值 - 发生错误会返回默认值
* @param param 方法参数
* @return [Long] 取不到返回 0
*/
fun callLong(vararg param: Any?) = invoke(param) ?: 0L
/**
* 执行方法 - 指定 [Short] 返回值类型
*
* - ❗请确认目标方法的返回值 - 发生错误会返回默认值
* @param param 方法参数
* @return [Short] 取不到返回 0
*/
fun callShort(vararg param: Any?) = invoke<Short?>(param) ?: 0
/**
* 执行方法 - 指定 [Double] 返回值类型
*
* - ❗请确认目标方法的返回值 - 发生错误会返回默认值
* @param param 方法参数
* @return [Double] 取不到返回 0
*/
fun callDouble(vararg param: Any?) = invoke(param) ?: 0.0
/**
* 执行方法 - 指定 [Float] 返回值类型
*
* - ❗请确认目标方法的返回值 - 发生错误会返回默认值
* @param param 方法参数
* @return [Float] 取不到返回 0f
*/
fun callFloat(vararg param: Any?) = invoke(param) ?: 0f
/**
* 执行方法 - 指定 [String] 返回值类型
* @param param 方法参数
* @return [String] 取不到返回 ""
*/
fun callString(vararg param: Any?) = invoke(param) ?: ""
/**
* 执行方法 - 指定 [Boolean] 返回值类型
*
* - ❗请确认目标方法的返回值 - 发生错误会返回默认值
* @param param 方法参数
* @return [Boolean] 取不到返回 false
*/
fun callBoolean(vararg param: Any?) = invoke(param) ?: false
override fun toString() =
"[${(memberInstance as? Method?)?.name ?: "<empty>"}] in [${instance?.javaClass?.name ?: "<empty>"}]"
}