Run Java to Kotlin converter from the command line?

自作多情 提交于 2020-07-18 10:19:04

问题


You can use the Kotlin plug-ins for Intellij and Eclipse to convert Java files to Kotlin. Can this conversion be done from the command line some how without either of these IDEs?


回答1:


It seems that it's possible, but there's no tool for that, just code. There are tests in j2k module in Kotlin Github repository, the example below is taken from the tests (AbstractJavaToKotlinConverterSingleFileTest):

private fun fileToKotlin(text: String, settings: ConverterSettings, project: Project): String {
    val file = createJavaFile(text)
    val converter = JavaToKotlinConverter(project, settings,
                                          IdeaReferenceSearcher, IdeaResolverForConverter)
    return converter.filesToKotlin(listOf(file), J2kPostProcessor(formatCode = true)).results.single()
}

I suppose, you can make a .jar of that module and write your own simple wrapper for JavaToKotlinConverter to make it work on files.



来源:https://stackoverflow.com/questions/31494013/run-java-to-kotlin-converter-from-the-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!