Kotlin Poet super interface with type argument

1.3k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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"))
)
0
On

You can use parameterizedBy() method to use generic type. If it is not detected by IDE, you can import manually.