zencd / groovy2kotlin

Converts Groovy sources to Kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

groovy2kotlin

An utility converting Groovy sources to Kotlin language.

Started Nov 2019 by zencd in order to convert a project of mine because I've not found any existing tools (and it's easy to write one by myself). The result of translation isn't 100% Kotlin-correct usually and user is required to review the result, but a significant amount of work can be saved for free.

The policy:

  • Don't loose any piece of code;
  • Produced code could be partially invalid in syntax/linking;
  • But it is acceptable since an operator can easily review such errors;
  • Silent behavioural changes are highly undesirable;
  • But they are still possible because Groovy can do unpredictable things in runtime;
  • Nevertheless do the best to emit valid, readable code;
  • Prefer val over var when possible;
  • Prefer emitting non-optionals;
  • Prefer emitting non-mutables;
  • No runtime dependencies.

The current implementation's passes:

  • One or multiple modules are parsed into AST by the Groovy itself;
  • All types (including dependent libs) gets resolved as strictly as possible;
  • AST traversed to resolve types of locals and expressions;
  • Inferring optionals;
  • Kotlin source code generated finally.

Feel free to submit a bug, ticket and do all the github things.

Requirements

  • JDK 1.8 (for running the tool)
  • Groovy 2.5.8 (for running the tool + input language level)
  • Kotlin 1.3.60 (produced code level)

Others may be supported but was not tested.

Usage

See here

Structure

  • Gtk.toKotlinAsSingleString() - converts a Groovy script text into Kotlin
  • BulkProcessor - converts a directory recursively
  • GroovyToKotlin - the translator engine
  • test-data/input-output-tests/ - a set of input/output tests showing how the things gets translated

Example

demo comparison

Todo

See here

About

Converts Groovy sources to Kotlin


Languages

Language:Groovy 100.0%