From 9ef793695c23739876121c500f273272a39bbedb Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Thu, 20 Jun 2024 10:26:18 +0800 Subject: [PATCH] feat: add SymbolConverterTool to resolve kotlin keywords package name --- .../factory/CodeSourceFileFactory.kt | 7 ++- .../yukihookapi/utils/SymbolConverterTool.kt | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 yukihookapi-ksp-xposed/src/api/kotlin/com/highcapable/yukihookapi/utils/SymbolConverterTool.kt diff --git a/yukihookapi-ksp-xposed/src/api/kotlin/com/highcapable/yukihookapi/factory/CodeSourceFileFactory.kt b/yukihookapi-ksp-xposed/src/api/kotlin/com/highcapable/yukihookapi/factory/CodeSourceFileFactory.kt index 5a76fb65..b1c87dde 100644 --- a/yukihookapi-ksp-xposed/src/api/kotlin/com/highcapable/yukihookapi/factory/CodeSourceFileFactory.kt +++ b/yukihookapi-ksp-xposed/src/api/kotlin/com/highcapable/yukihookapi/factory/CodeSourceFileFactory.kt @@ -25,6 +25,7 @@ package com.highcapable.yukihookapi.factory import com.highcapable.yukihookapi.bean.GenerateData import com.highcapable.yukihookapi.generated.YukiHookAPIProperties +import com.highcapable.yukihookapi.utils.SymbolConverterTool import java.text.SimpleDateFormat import java.util.Date @@ -142,7 +143,7 @@ fun GenerateData.sources() = mapOf( package ${PackageName.ModuleApplication_Impl} - import $entryPackageName.$entryClassName + import ${SymbolConverterTool.process(entryPackageName)}.$entryClassName """.trimIndent() + "\n\n" + createCommentContent(ClassName.ModuleApplication_Impl) + "\n" + """ object ${ClassName.ModuleApplication_Impl} { @@ -295,7 +296,7 @@ fun GenerateData.sources() = mapOf( ClassName.XposedInit to """ @file:Suppress("ClassName", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") - package $entryPackageName + package ${SymbolConverterTool.process(entryPackageName)} import androidx.annotation.Keep import ${ExternalCallerName.YukiXposedEventCaller.first} @@ -331,7 +332,7 @@ fun GenerateData.sources() = mapOf( ClassName.XposedInit_Impl to """ @file:Suppress("ClassName", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") - package $entryPackageName + package ${SymbolConverterTool.process(entryPackageName)} import ${ExternalCallerName.YukiXposedModuleCaller.first} import ${ExternalCallerName.YukiXposedResourcesCaller.first} diff --git a/yukihookapi-ksp-xposed/src/api/kotlin/com/highcapable/yukihookapi/utils/SymbolConverterTool.kt b/yukihookapi-ksp-xposed/src/api/kotlin/com/highcapable/yukihookapi/utils/SymbolConverterTool.kt new file mode 100644 index 00000000..6f1d0aae --- /dev/null +++ b/yukihookapi-ksp-xposed/src/api/kotlin/com/highcapable/yukihookapi/utils/SymbolConverterTool.kt @@ -0,0 +1,56 @@ +/* + * YukiHookAPI - An efficient Hook API and Xposed Module solution built in Kotlin. + * Copyright (C) 2019-2024 HighCapable + * https://github.com/HighCapable/YukiHookAPI + * + * Apache License Version 2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * This file is created by fankes on 2024/6/20. + */ +package com.highcapable.yukihookapi.utils + +/** + * 符号转换工具 + */ +object SymbolConverterTool { + + /** Kotlin 关键字列表 */ + private val kotlinHardKeywords = listOf( + "as", "as?", "break", "class", "continue", "do", + "else", "false", "for", "fun", "if", "in", "!in", "interface", + "is", "!is", "null", "object", "package", "return", "super", + "this", "throw", "true", "try", "typealias", "typeof", "val", + "var", "when", "while" + ) + + /** + * 处理需要转换的内容 + * @param content 内容 + * @return [String] + */ + fun process(content: String) = when { + content.contains(".") && !content.startsWith(".") && !content.endsWith(".") -> + content.split(".").joinToString(".") { if (isKotlinKeyword(it)) "`$it`" else it } + isKotlinKeyword(content) -> "`$content`" + else -> content + } + + /** + * 是否为 Kotlin 关键字 + * @param word 关键字 + * @return [Boolean] + */ + private fun isKotlinKeyword(word: String) = kotlinHardKeywords.contains(word) +} \ No newline at end of file