refactor: merge all util functions to factory

This commit is contained in:
2023-09-23 20:57:01 +08:00
parent ac04eacbd4
commit f2b90caab0
9 changed files with 135 additions and 81 deletions

View File

@@ -33,7 +33,7 @@ package com.highcapable.yukireflection.finder.base
import com.highcapable.yukireflection.YukiReflection
import com.highcapable.yukireflection.log.yLoggerE
import com.highcapable.yukireflection.log.yLoggerI
import com.highcapable.yukireflection.utils.await
import com.highcapable.yukireflection.utils.factory.await
import java.lang.reflect.Constructor
import java.lang.reflect.Field
import java.lang.reflect.Member

View File

@@ -49,9 +49,9 @@ import com.highcapable.yukireflection.finder.tools.ReflectionTool
import com.highcapable.yukireflection.finder.type.factory.ModifierConditions
import com.highcapable.yukireflection.finder.type.factory.NameConditions
import com.highcapable.yukireflection.log.yLoggerW
import com.highcapable.yukireflection.utils.await
import com.highcapable.yukireflection.utils.runBlocking
import com.highcapable.yukireflection.utils.toStackTrace
import com.highcapable.yukireflection.utils.factory.await
import com.highcapable.yukireflection.utils.factory.runBlocking
import com.highcapable.yukireflection.utils.factory.toStackTrace
import dalvik.system.BaseDexClassLoader
import java.lang.reflect.Constructor
import java.lang.reflect.Field

View File

@@ -44,7 +44,7 @@ import com.highcapable.yukireflection.finder.type.factory.ObjectsConditions
import com.highcapable.yukireflection.log.yLoggerW
import com.highcapable.yukireflection.type.defined.UndefinedType
import com.highcapable.yukireflection.type.defined.VagueType
import com.highcapable.yukireflection.utils.runBlocking
import com.highcapable.yukireflection.utils.factory.runBlocking
import java.lang.reflect.Constructor
/**

View File

@@ -44,7 +44,7 @@ import com.highcapable.yukireflection.finder.type.factory.ModifierConditions
import com.highcapable.yukireflection.finder.type.factory.NameConditions
import com.highcapable.yukireflection.finder.type.factory.ObjectConditions
import com.highcapable.yukireflection.log.yLoggerW
import com.highcapable.yukireflection.utils.runBlocking
import com.highcapable.yukireflection.utils.factory.runBlocking
import java.lang.reflect.Field
/**

View File

@@ -46,7 +46,7 @@ import com.highcapable.yukireflection.finder.type.factory.ObjectsConditions
import com.highcapable.yukireflection.log.yLoggerW
import com.highcapable.yukireflection.type.defined.UndefinedType
import com.highcapable.yukireflection.type.defined.VagueType
import com.highcapable.yukireflection.utils.runBlocking
import com.highcapable.yukireflection.utils.factory.runBlocking
import java.lang.reflect.Method
/**

View File

@@ -51,13 +51,13 @@ import com.highcapable.yukireflection.type.java.DalvikBaseDexClassLoader
import com.highcapable.yukireflection.type.java.NoClassDefFoundErrorClass
import com.highcapable.yukireflection.type.java.NoSuchFieldErrorClass
import com.highcapable.yukireflection.type.java.NoSuchMethodErrorClass
import com.highcapable.yukireflection.utils.conditions
import com.highcapable.yukireflection.utils.findLastIndex
import com.highcapable.yukireflection.utils.lastIndex
import com.highcapable.yukireflection.utils.let
import com.highcapable.yukireflection.utils.runOrFalse
import com.highcapable.yukireflection.utils.takeIf
import com.highcapable.yukireflection.utils.value
import com.highcapable.yukireflection.utils.factory.conditions
import com.highcapable.yukireflection.utils.factory.findLastIndex
import com.highcapable.yukireflection.utils.factory.lastIndex
import com.highcapable.yukireflection.utils.factory.let
import com.highcapable.yukireflection.utils.factory.runOrFalse
import com.highcapable.yukireflection.utils.factory.takeIf
import com.highcapable.yukireflection.utils.factory.value
import dalvik.system.BaseDexClassLoader
import java.lang.reflect.Constructor
import java.lang.reflect.Field

View File

@@ -0,0 +1,80 @@
/*
* YukiReflection - An efficient Reflection API for Java and Android built in Kotlin.
* Copyright (C) 2019-2023 HighCapable
* https://github.com/fankes/YukiReflection
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is created by fankes on 2023/9/23.
*/
@file:Suppress("unused")
package com.highcapable.yukireflection.utils.factory
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
/**
* 创建当前线程池服务
* @return [ExecutorService]
*/
private val currentThreadPool get() = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
/**
* 启动 [Thread] 延迟等待 [block] 的结果 [T]
* @param delayMs 延迟毫秒 - 默认 1 ms
* @param block 方法块
* @return [T]
*/
internal inline fun <T> T.await(delayMs: Long = 1, crossinline block: (T) -> Unit): T {
currentThreadPool.apply {
execute {
if (delayMs > 0) Thread.sleep(delayMs)
block(this@await)
shutdown()
}
}
return this
}
/**
* 计算方法执行耗时
* @param block 方法块
* @return [RunBlockResult]
*/
internal inline fun <R> runBlocking(block: () -> R): RunBlockResult {
val start = System.currentTimeMillis()
block()
return RunBlockResult(afterMs = System.currentTimeMillis() - start)
}
/**
* 构造耗时计算结果类
* @param afterMs 耗时
*/
internal class RunBlockResult(internal val afterMs: Long) {
/**
* 获取耗时计算结果
* @param result 回调结果 - ([Long] 耗时)
*/
internal inline fun result(result: (Long) -> Unit) = result(afterMs)
}

View File

@@ -0,0 +1,39 @@
/*
* YukiReflection - An efficient Reflection API for Java and Android built in Kotlin.
* Copyright (C) 2019-2023 HighCapable
* https://github.com/fankes/YukiReflection
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is created by fankes on 2023/9/23.
*/
@file:Suppress("unused")
package com.highcapable.yukireflection.utils.factory
import java.io.ByteArrayOutputStream
import java.io.PrintStream
/**
* 获取完整的异常堆栈内容
* @return [String]
*/
internal fun Throwable.toStackTrace() = ByteArrayOutputStream().also { printStackTrace(PrintStream(it)) }.toString()

View File

@@ -23,46 +23,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is created by fankes on 2022/2/5.
* This file is modified by fankes on 2023/1/21.
* This file is created by fankes on 2023/9/23.
*/
@file:Suppress("unused")
package com.highcapable.yukireflection.utils
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
/**
* 创建当前线程池服务
* @return [ExecutorService]
*/
private val currentThreadPool get() = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
/**
* [T] 返回无返回值的 [Unit]
* @return [Unit]
*/
internal fun <T> T?.unit() = let {}
/**
* 启动 [Thread] 延迟等待 [block] 的结果 [T]
* @param delayMs 延迟毫秒 - 默认 1 ms
* @param block 方法块
* @return [T]
*/
internal inline fun <T> T.await(delayMs: Long = 1, crossinline block: (T) -> Unit): T {
currentThreadPool.apply {
execute {
if (delayMs > 0) Thread.sleep(delayMs)
block(this@await)
shutdown()
}
}
return this
}
package com.highcapable.yukireflection.utils.factory
/**
* 获取数组内容依次列出的字符串表示
@@ -117,36 +82,6 @@ internal inline fun runOrTrue(block: () -> Boolean) = runCatching { block() }.ge
*/
internal inline fun runOrFalse(block: () -> Boolean) = runCatching { block() }.getOrNull() ?: false
/**
* 获取完整的异常堆栈内容
* @return [String]
*/
internal fun Throwable.toStackTrace() = ByteArrayOutputStream().also { printStackTrace(PrintStream(it)) }.toString()
/**
* 计算方法执行耗时
* @param block 方法块
* @return [RunBlockResult]
*/
internal inline fun <R> runBlocking(block: () -> R): RunBlockResult {
val start = System.currentTimeMillis()
block()
return RunBlockResult(afterMs = System.currentTimeMillis() - start)
}
/**
* 构造耗时计算结果类
* @param afterMs 耗时
*/
internal class RunBlockResult(internal val afterMs: Long) {
/**
* 获取耗时计算结果
* @param result 回调结果 - ([Long] 耗时)
*/
internal inline fun result(result: (Long) -> Unit) = result(afterMs)
}
/**
* 创建多项条件判断 - 条件对象 [T]
* @param initiate 方法体