androidkotlincode-generationkotlinpoet

Kotlin Poet super interface with type argument


I'm trying to generate an object which implements an interface with type parameter like the following example:

object HelloWorld : Feature<Intent>

I can generate the object that implements my interface like the following code:

val typeSpecBuilder = TypeSpec.objectBuilder("HelloWorld")
typeSpecBuilder.addSuperinterface(
      ClassName(
               "com.example.mylib",
               "Feature"
      )

How can I pass the type argument to the interface?


Solution

  • Import plusParameter manually:

    import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.plusParameter
    

    And use like below:

    val typeSpecBuilder = TypeSpec.objectBuilder(feature.featureName)
    typeSpecBuilder.addSuperinterface(
          ClassName(
                "com.raqun.icarus.core",
                "Feature"
          ).plusParameter(ClassName("com.example.myawesomeclass", "MyAwesomeClass"))
    )