I've got some legacy Java code that generates some...
# getting-started
y
I've got some legacy Java code that generates something that should probably be
enum class Names(val name: String)
(or a
sealed interface
, etc.) but is instead
Copy code
public class Generated {
  public static final String names[] = new String[] { /* ... */ }
  
  // repeat this line for each String in `names`, where the name of the const is the String, and `i` is the index into `names`
  public static final int NAME = i;
}
is there a way of generating an `enum class`/`sealed interface` from this? for example, to exhaustively match on in
when
expressions.
l
What do you mean by 'generate'? You have the Java class file, and you want to somehow create a Kotlin sealed interface from it?
y
yes, this class is itself generated by some library and the definition (the array and the consts) may change. we have a new feature that now needs to interface with these consts and so it would be useful to be able to make it sealed.
c
use #C013BA8EQSE and Kotlin Poet to generate your own implementation of the “class”.
thank you color 1
s
you don't need a plugin for this, since you only need the sourcecode for the Kotlin version once (i assume). I wrote you the generator code on Kotlin-Playground https://pl.kotl.in/N0cLyVzJ4 Given the names array in Java, copy it into the names list. for my dummy names list, the output is:
Copy code
enum class Generated(val name: String) {
  first("1"),
  second("2"),
  third("3")
}