Home > Software engineering >  adding star projection in kotlin poet
adding star projection in kotlin poet

Time:11-24

I am using KOTLIN Poet to generated some code. I am stuck with adding star projection for a parameterized type. Could not find anything of adding star projection in KOTLIN Poet docs.

Is there a way to add star projection in KOTLIN Poet.

CodePudding user response:

To get a TypeName as a star projection, use:

ClassName("", "StarClass").parameterizedBy(STAR)

In a small example:

val starClass = ClassName("", "StarClass").parameterizedBy(STAR)
val exampleFile = FileSpec.builder("", "StackOverflow")
   .addFunction(
      FunSpec.builder("starFunction")
         .addParameter("starClazz", starClass)
         .addStatement("println(starClazz.toString())", starClass)
         .build()
   )
   .build()
exampleFile.writeTo(System.out)

Outputs:

public fun starFunction(starClazz: StarClass<*>): Unit {
  println(starClazz.toString())
}
  • Related