Skip to content

Commit 9ef7936

Browse files
committed
feat: add SymbolConverterTool to resolve kotlin keywords package name
1 parent 5c946f7 commit 9ef7936

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

yukihookapi-ksp-xposed/src/api/kotlin/com/highcapable/yukihookapi/factory/CodeSourceFileFactory.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package com.highcapable.yukihookapi.factory
2525

2626
import com.highcapable.yukihookapi.bean.GenerateData
2727
import com.highcapable.yukihookapi.generated.YukiHookAPIProperties
28+
import com.highcapable.yukihookapi.utils.SymbolConverterTool
2829
import java.text.SimpleDateFormat
2930
import java.util.Date
3031

@@ -142,7 +143,7 @@ fun GenerateData.sources() = mapOf(
142143
143144
package ${PackageName.ModuleApplication_Impl}
144145
145-
import $entryPackageName.$entryClassName
146+
import ${SymbolConverterTool.process(entryPackageName)}.$entryClassName
146147
""".trimIndent() + "\n\n" + createCommentContent(ClassName.ModuleApplication_Impl) + "\n" + """
147148
object ${ClassName.ModuleApplication_Impl} {
148149
@@ -295,7 +296,7 @@ fun GenerateData.sources() = mapOf(
295296
ClassName.XposedInit to """
296297
@file:Suppress("ClassName", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
297298
298-
package $entryPackageName
299+
package ${SymbolConverterTool.process(entryPackageName)}
299300
300301
import androidx.annotation.Keep
301302
import ${ExternalCallerName.YukiXposedEventCaller.first}
@@ -331,7 +332,7 @@ fun GenerateData.sources() = mapOf(
331332
ClassName.XposedInit_Impl to """
332333
@file:Suppress("ClassName", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
333334
334-
package $entryPackageName
335+
package ${SymbolConverterTool.process(entryPackageName)}
335336
336337
import ${ExternalCallerName.YukiXposedModuleCaller.first}
337338
import ${ExternalCallerName.YukiXposedResourcesCaller.first}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* YukiHookAPI - An efficient Hook API and Xposed Module solution built in Kotlin.
3+
* Copyright (C) 2019-2024 HighCapable
4+
* https://github.com/HighCapable/YukiHookAPI
5+
*
6+
* Apache License Version 2.0
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* https://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
* This file is created by fankes on 2024/6/20.
21+
*/
22+
package com.highcapable.yukihookapi.utils
23+
24+
/**
25+
* 符号转换工具
26+
*/
27+
object SymbolConverterTool {
28+
29+
/** Kotlin 关键字列表 */
30+
private val kotlinHardKeywords = listOf(
31+
"as", "as?", "break", "class", "continue", "do",
32+
"else", "false", "for", "fun", "if", "in", "!in", "interface",
33+
"is", "!is", "null", "object", "package", "return", "super",
34+
"this", "throw", "true", "try", "typealias", "typeof", "val",
35+
"var", "when", "while"
36+
)
37+
38+
/**
39+
* 处理需要转换的内容
40+
* @param content 内容
41+
* @return [String]
42+
*/
43+
fun process(content: String) = when {
44+
content.contains(".") && !content.startsWith(".") && !content.endsWith(".") ->
45+
content.split(".").joinToString(".") { if (isKotlinKeyword(it)) "`$it`" else it }
46+
isKotlinKeyword(content) -> "`$content`"
47+
else -> content
48+
}
49+
50+
/**
51+
* 是否为 Kotlin 关键字
52+
* @param word 关键字
53+
* @return [Boolean]
54+
*/
55+
private fun isKotlinKeyword(word: String) = kotlinHardKeywords.contains(word)
56+
}

0 commit comments

Comments
 (0)