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?

推荐答案

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.

Kotlin相关问答推荐

只能在元素区域中点击的Jetpack Compose列

我如何测试一个可组合组件没有显示,但如果它不存在也接受?

如何确保Kotlin子类已完成初始化?

在Kotlin中,我是否可以访问已知的WHEN子句值?

有没有办法在 jetpack compose 中将 TextField 密码点图标增加得更大?

在协程上下文中重新抛出异常

使用 kotlin 流删除 map 中具有某些相似性的值

Kotlin 可空泛型

如何在 jOOQ 中两次加入同一张表?

这是什么 Kotlin 类型:(String..String?)

如何为你的 Flutter 元素添加 Kotlin 支持?

在 Kotlin 中使用 @Parcelize 注释时如何忽略字段

使用 Kotlin 创建新目录,Mkdir() 不起作用

如何在 kotlin 中生成 json 对象?

Kotlin:如何在活页夹中返回正在运行的服务实例?

如何使用协调器布局和行为在CardView上完成此动画?

Kotlin - computed var 属性的用处?

Jetpack Compose 折叠工具栏

@StringRes、@DrawableRes、@LayoutRes等android注释使用kotlin参数进行判断

如何在 IntelliJ IDEA 中禁用粘贴时将 Java 转换为 Kotlin?