diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreator.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreator.kt index 53f13a8c..63cbe682 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreator.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreator.kt @@ -32,10 +32,10 @@ package com.highcapable.yukihookapi.hook.core import com.highcapable.yukihookapi.YukiHookAPI import com.highcapable.yukihookapi.annotation.CauseProblemsApi import com.highcapable.yukihookapi.hook.bean.HookClass +import com.highcapable.yukihookapi.hook.core.finder.base.MemberBaseFinder import com.highcapable.yukihookapi.hook.core.finder.members.ConstructorFinder import com.highcapable.yukihookapi.hook.core.finder.members.FieldFinder import com.highcapable.yukihookapi.hook.core.finder.members.MethodFinder -import com.highcapable.yukihookapi.hook.core.finder.base.MemberBaseFinder import com.highcapable.yukihookapi.hook.factory.* import com.highcapable.yukihookapi.hook.log.yLoggerE import com.highcapable.yukihookapi.hook.log.yLoggerI @@ -325,12 +325,12 @@ class YukiMemberHookCreator(@PublishedApi internal val packageParam: PackagePara * @param initiate 方法体 * @return [MethodFinder.Process] */ - inline fun method(initiate: MethodCondition) = try { + inline fun method(initiate: MethodCondition) = runCatching { isHookMemberSetup = true MethodFinder(hookInstance = this, hookClass.instance).apply(initiate).apply { finder = this }.process() - } catch (e: Throwable) { - findingThrowable = e - MethodFinder(hookInstance = this).denied(e) + }.getOrElse { + findingThrowable = it + MethodFinder(hookInstance = this).denied(it) } /** @@ -340,12 +340,12 @@ class YukiMemberHookCreator(@PublishedApi internal val packageParam: PackagePara * @param initiate 方法体 * @return [ConstructorFinder.Process] */ - inline fun constructor(initiate: ConstructorCondition = { emptyParam() }) = try { + inline fun constructor(initiate: ConstructorCondition = { emptyParam() }) = runCatching { isHookMemberSetup = true ConstructorFinder(hookInstance = this, hookClass.instance).apply(initiate).apply { finder = this }.process() - } catch (e: Throwable) { - findingThrowable = e - ConstructorFinder(hookInstance = this).denied(e) + }.getOrElse { + findingThrowable = it + ConstructorFinder(hookInstance = this).denied(it) } /** @@ -556,16 +556,16 @@ class YukiMemberHookCreator(@PublishedApi internal val packageParam: PackagePara val replaceMent = object : YukiMemberReplacement(priority) { override fun replaceHookedMember(param: Param) = replaceHookParam.assign(param).let { assign -> - try { + runCatching { replaceHookCallback?.invoke(assign).also { checkingReturnType((param.member as? Method?)?.returnType, it?.javaClass) if (replaceHookCallback != null) onHookLogMsg(msg = "Replace Hook Member [${this@hook}] done [$tag]") HookParam.invoke() } - } catch (e: Throwable) { - onConductFailureCallback?.invoke(assign, e) - onAllFailureCallback?.invoke(e) - if (onConductFailureCallback == null && onAllFailureCallback == null) onHookFailureMsg(e) + }.getOrElse { + onConductFailureCallback?.invoke(assign, it) + onAllFailureCallback?.invoke(it) + if (onConductFailureCallback == null && onAllFailureCallback == null) onHookFailureMsg(it) /** 若发生异常则会自动调用未经 Hook 的原始 [Member] 保证 Hook APP 正常运行 */ assign.callOriginal() } diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/ConstructorFinder.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/ConstructorFinder.kt index c8ec4841..6179e634 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/ConstructorFinder.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/ConstructorFinder.kt @@ -207,21 +207,21 @@ class ConstructorFinder @PublishedApi internal constructor( } @YukiPrivateApi - override fun build() = try { + override fun build() = runCatching { build(isBind = false) Result() - } catch (e: Throwable) { - onFailureMsg(throwable = e) - Result(isNoSuch = true, e) + }.getOrElse { + onFailureMsg(throwable = it) + Result(isNoSuch = true, it) } @YukiPrivateApi - override fun process() = try { + override fun process() = runCatching { build(isBind = true) Process() - } catch (e: Throwable) { - onFailureMsg(throwable = e) - Process(isNoSuch = true, e) + }.getOrElse { + onFailureMsg(throwable = it) + Process(isNoSuch = true, it) } @YukiPrivateApi diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/FieldFinder.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/FieldFinder.kt index 021061c2..4c4a8080 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/FieldFinder.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/FieldFinder.kt @@ -188,7 +188,7 @@ class FieldFinder @PublishedApi internal constructor( } @YukiPrivateApi - override fun build() = try { + override fun build() = runCatching { if (classSet != null) { classSet.checkingInternal() runBlocking { @@ -198,9 +198,7 @@ class FieldFinder @PublishedApi internal constructor( } Result() } else Result(isNoSuch = true, Throwable("classSet is null")) - } catch (e: Throwable) { - Result(isNoSuch = true, e).await { onFailureMsg(throwable = e) } - } + }.getOrElse { e -> Result(isNoSuch = true, e).await { onFailureMsg(throwable = e) } } @YukiPrivateApi override fun process() = error("FieldFinder does not contain this usage") diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/MethodFinder.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/MethodFinder.kt index 79ef8062..65dff17d 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/MethodFinder.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/members/MethodFinder.kt @@ -279,21 +279,21 @@ class MethodFinder @PublishedApi internal constructor( } @YukiPrivateApi - override fun build() = try { + override fun build() = runCatching { build(isBind = false) Result() - } catch (e: Throwable) { - onFailureMsg(throwable = e) - Result(isNoSuch = true, e) + }.getOrElse { + onFailureMsg(throwable = it) + Result(isNoSuch = true, it) } @YukiPrivateApi - override fun process() = try { + override fun process() = runCatching { build(isBind = true) Process() - } catch (e: Throwable) { - onFailureMsg(throwable = e) - Process(isNoSuch = true, e) + }.getOrElse { + onFailureMsg(throwable = it) + Process(isNoSuch = true, it) } @YukiPrivateApi diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt index 865f00e6..d1d16062 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt @@ -109,12 +109,7 @@ inline fun classOf(loader: ClassLoader? = null) = loader?.let { clas * @param loader [Class] 所在的 [ClassLoader] - 不填使用默认 [ClassLoader] * @return [Boolean] 是否存在 */ -fun String.hasClass(loader: ClassLoader? = null) = try { - classOf(name = this, loader) - true -} catch (_: Throwable) { - false -} +fun String.hasClass(loader: ClassLoader? = null) = runCatching { classOf(name = this, loader); true }.getOrNull() ?: false /** * 查找变量是否存在 diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt index d69ce8b0..30baeb86 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt @@ -331,11 +331,8 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper: * @param loader 当前 [ClassLoader] - 默认使用 [appClassLoader] - 设为 null 使用默认 [ClassLoader] * @return [HookClass] */ - fun findClass(name: String, loader: ClassLoader? = appClassLoader) = try { - classOf(name, loader).hookClass - } catch (e: Throwable) { - HookClass(name = name, throwable = e) - } + fun findClass(name: String, loader: ClassLoader? = appClassLoader) = + runCatching { classOf(name, loader).hookClass }.getOrElse { HookClass(name = name, throwable = it) } /** * 查找并装载 [Class] @@ -414,11 +411,8 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper: * @return [HookClass] */ @PublishedApi - internal fun VariousClass.hookClass(loader: ClassLoader? = null) = try { - get(loader).hookClass - } catch (e: Throwable) { - HookClass(name = "VariousClass", throwable = Throwable(e.message)) - } + internal fun VariousClass.hookClass(loader: ClassLoader? = null) = + runCatching { get(loader).hookClass }.getOrElse { HookClass(name = "VariousClass", throwable = Throwable(it.message)) } /** * [Class] 转换为 [HookClass] diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.kt index fdeb8309..bb2a5b98 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.kt @@ -61,7 +61,7 @@ import java.io.File * * - 若你正在使用 [PreferenceFragmentCompat] - 请迁移到 [ModulePreferenceFragment] 以适配上述功能特性 * - * - 详情请参考 [API 文档 - YukiHookModulePrefs](https://fankes.github.io/YukiHookAPI/#/api/document?id=yukihookmoduleprefs-class) + * - 详情请参考 [API 文档 - YukiHookModulePrefs](https://fankes.github.io/YukiHookAPI/zh-cn/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs) * @param context 上下文实例 - 默认空 */ class YukiHookModulePrefs private constructor(private var context: Context? = null) { @@ -171,10 +171,10 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu */ private val sPrefs get() = checkApi().let { - try { + runCatching { context?.getSharedPreferences(prefsName, Context.MODE_WORLD_READABLE).also { isUsingNewXSharedPreferences = true } ?: error("YukiHookModulePrefs missing Context instance") - } catch (_: Throwable) { + }.getOrElse { context?.getSharedPreferences(prefsName, Context.MODE_PRIVATE).also { isUsingNewXSharedPreferences = false } ?: error("YukiHookModulePrefs missing Context instance") } diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/prefs/ui/ModulePreferenceFragment.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/prefs/ui/ModulePreferenceFragment.kt index 8df37df0..e4b96b77 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/prefs/ui/ModulePreferenceFragment.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/prefs/ui/ModulePreferenceFragment.kt @@ -39,6 +39,7 @@ import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceManager import androidx.preference.PreferenceScreen import com.highcapable.yukihookapi.YukiHookAPI +import com.highcapable.yukihookapi.hook.utils.unit import com.highcapable.yukihookapi.hook.xposed.prefs.YukiHookModulePrefs /** @@ -50,7 +51,7 @@ import com.highcapable.yukihookapi.hook.xposed.prefs.YukiHookModulePrefs * * 然后请将重写方法由 [onCreatePreferences] 替换为 [onCreatePreferencesInModuleApp] 即可 * - * 详情请参考 [API 文档 - ModulePreferenceFragment](https://fankes.github.io/YukiHookAPI/#/api/document?id=modulepreferencefragment-class) + * 详情请参考 [API 文档 - ModulePreferenceFragment](https://fankes.github.io/YukiHookAPI/zh-cn/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/ModulePreferenceFragment) */ abstract class ModulePreferenceFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener { @@ -101,9 +102,7 @@ abstract class ModulePreferenceFragment : PreferenceFragmentCompat(), SharedPref abstract fun onCreatePreferencesInModuleApp(savedInstanceState: Bundle?, rootKey: String?) /** 设置自动适配模块 Sp 存储全局可读可写 */ - private fun makeNewXShareReadableIfPossible() = try { + private fun makeNewXShareReadableIfPossible() = runCatching { currentActivity.getSharedPreferences(prefsName, Context.MODE_WORLD_READABLE) - } catch (_: Throwable) { - YukiHookModulePrefs.makeWorldReadable(currentActivity, prefsFileName = "$prefsName.xml") - } + }.onFailure { YukiHookModulePrefs.makeWorldReadable(currentActivity, prefsFileName = "$prefsName.xml") }.unit() } \ No newline at end of file