简体   繁体   中英

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 ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM