adding star projection in kotlin poet

249 views Asked by At

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.

1

There are 1 answers

0
Simon Jacobs On BEST ANSWER

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())
}