From f2b90caab0af498d42069de166669ca7fcafee7d Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Sat, 23 Sep 2023 20:57:01 +0800 Subject: [PATCH] refactor: merge all util functions to factory --- .../finder/base/MemberBaseFinder.kt | 2 +- .../finder/classes/DexClassFinder.kt | 6 +- .../finder/members/ConstructorFinder.kt | 2 +- .../finder/members/FieldFinder.kt | 2 +- .../finder/members/MethodFinder.kt | 2 +- .../finder/tools/ReflectionTool.kt | 14 ++-- .../utils/factory/ThreadFactory.kt | 80 +++++++++++++++++++ .../utils/factory/ThrowableFactory.kt | 39 +++++++++ .../VariableFactory.kt} | 69 +--------------- 9 files changed, 135 insertions(+), 81 deletions(-) create mode 100644 yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/ThreadFactory.kt create mode 100644 yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/ThrowableFactory.kt rename yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/{UtilsFactory.kt => factory/VariableFactory.kt} (76%) diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/base/MemberBaseFinder.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/base/MemberBaseFinder.kt index 2234d16..333b723 100644 --- a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/base/MemberBaseFinder.kt +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/base/MemberBaseFinder.kt @@ -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 diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/classes/DexClassFinder.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/classes/DexClassFinder.kt index 1b267e2..b11cafb 100644 --- a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/classes/DexClassFinder.kt +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/classes/DexClassFinder.kt @@ -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 diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/ConstructorFinder.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/ConstructorFinder.kt index 3df9ac3..663c4cf 100644 --- a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/ConstructorFinder.kt +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/ConstructorFinder.kt @@ -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 /** diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/FieldFinder.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/FieldFinder.kt index e96860f..821cf38 100644 --- a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/FieldFinder.kt +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/FieldFinder.kt @@ -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 /** diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/MethodFinder.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/MethodFinder.kt index 7d44a94..323f6ac 100644 --- a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/MethodFinder.kt +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/members/MethodFinder.kt @@ -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 /** diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/tools/ReflectionTool.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/tools/ReflectionTool.kt index 65a36a8..28ced0a 100644 --- a/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/tools/ReflectionTool.kt +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/finder/tools/ReflectionTool.kt @@ -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 diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/ThreadFactory.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/ThreadFactory.kt new file mode 100644 index 0000000..a5b0957 --- /dev/null +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/ThreadFactory.kt @@ -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.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 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) +} \ No newline at end of file diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/ThrowableFactory.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/ThrowableFactory.kt new file mode 100644 index 0000000..07fcfa5 --- /dev/null +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/ThrowableFactory.kt @@ -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() \ No newline at end of file diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/UtilsFactory.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/VariableFactory.kt similarity index 76% rename from yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/UtilsFactory.kt rename to yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/VariableFactory.kt index cb20644..78c14e8 100644 --- a/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/UtilsFactory.kt +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/factory/VariableFactory.kt @@ -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?.unit() = let {} - -/** - * 启动 [Thread] 延迟等待 [block] 的结果 [T] - * @param delayMs 延迟毫秒 - 默认 1 ms - * @param block 方法块 - * @return [T] - */ -internal inline fun 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 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 方法体