现在的android studio提供了java自动转kotlin的实现,非常方便。

那android的java和kotlin有没有可能转成鸿蒙arkts呢。

本质上都是字符串文件,讲道理应该是可行的。

先从最简单的model转换开始,把kotlin的model转成ets。

通过插件的形式把kt/java文件转成ets文件,贴个kt2ets的方法:

fun convertKt2Ets(ktCode: String): String {
        val lines = ktCode.split("\n")
        var inClass = false
        var hasBigQuo = false
        var dataClassName = ""
        val tsLines = mutableListOf<String>()
        for (line in lines) {
            when {
                // class
                line.startsWith("class ") -> {
                    // class content
                    inClass = true
                    // 是个data class
                    if (line.contains("{")) {
                        hasBigQuo = true
                    }
                    dataClassName = line.substringAfter("class").substringBefore("(")
                    if (dataClassName != null) {
                        tsLines.add("export interface ${dataClassName.trim()} {")
                    }
                }
                // fields
                inClass && (line.contains("val") || line.contains("var")) -> {
                    val fields = line.split(",")
                    fields.forEach { field ->
                        val pureCode = field.replace("val", "").replace("var", "")
                        val parts = pureCode.split(":")
                        if (parts.size == 2) {
                            val name = parts[0].trim()
                            val hasQue = parts[1].contains("?")
                            val useQue = if (hasQue) {
                                "?"
                            } else {
                                ""
                            }
                            var type = convertKtType2EtsType(parts[1].replace("?", "").trim())
                            // 数组变量
                            if (type.contains("List<") || type.contains("ArrayList<")) {
                                type = type.replace("List<", "")
                                    .replace("ArrayList<", "")
                                    .replace(">", "")
                                type = "${type}[]"
                            }
                            tsLines.add("    $name${useQue}: $type")
                        } else {
                            println("not format field line")
                        }
                    }
                }
                // fun 2 function
                line.contains("fun ") -> {
                    val functionName = line.substringAfter("fun ").substringBefore("(")
                    val params = line.substringAfter("(").substringBefore(")").split(",").joinToString(", ") {
                        val (paramName, paramType) = it.trim().split(":")
                        "${paramName.trim()}: ${convertKtType2EtsType(paramType.trim())}"
                    }
                    val returnType = convertKtType2EtsType(line.substringAfter("):").substringBefore("{").trim())
                    tsLines.add("function $functionName($params): $returnType {")
                }
                (inClass && line.contains("}")) -> {
                    tsLines.add(line)
                    inClass = false
                    hasBigQuo = false
                }
                (inClass && !hasBigQuo && line.contains(")")) -> {
                    tsLines.add("}")
                    inClass = false
                    hasBigQuo = false
                }
                (line.startsWith("package") || line.startsWith("import ")) -> {
                    // remove line
                }
                 else -> {
                     tsLines.add(line)
                 }
            }
        }
        return tsLines.joinToString("\n")
    }

    private fun convertKtType2EtsType(type: String): String {
        return type.replace("String", "string")
            .replace("Int", "number")
            .replace("Float", "number")
            .replace("Double", "number")
            .replace("Boolean", "boolean")
            .replace("Long", "number")
    }

后续公布源码

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部