1

I'm trying to generate a subclass from an annotated class and getting methods parameters using code below, My problem is that I always get java types, not kotlin types which conflict with parent class causing override error, How do I get correct types kotlin or java types,this exactly happens with String,Int and List

 method.parameters.onEach { variableElement ->
                        if (!variableElement.asType().asTypeName()
                                .toString()
                                .contains("Continuation")
                        ) {
                            val types = ElementFilter.typesIn(variableElement.enclosedElements)
                            val parameterBuilder = ParameterSpec.builder(
                                variableElement.simpleName.toString(),
                                variableElement.asType().asTypeName()
                            )
                            function.addParameter(parameterBuilder.build())
                        }
                    }
Islam Assem
  • 1,376
  • 13
  • 21

2 Answers2

1

KotlinPoet will output kotlin.String. Take a look at the kotlinpoet unit tests for a lot of examples, for example:

 @Test fun topLevelMembersRetainOrder() {
    val source = FileSpec.builder(tacosPackage, "Taco")
      .addFunction(FunSpec.builder("a").addModifiers(KModifier.PUBLIC).build())
      .addType(TypeSpec.classBuilder("B").build())
      .addProperty(
        PropertySpec.builder("c", String::class, KModifier.PUBLIC)
          .initializer("%S", "C")
          .build()
      )
      .addFunction(FunSpec.builder("d").build())
      .addType(TypeSpec.classBuilder("E").build())
      .addProperty(
        PropertySpec.builder("f", String::class, KModifier.PUBLIC)
          .initializer("%S", "F")
          .build()
      )
      .build()
    assertThat(source.toString()).isEqualTo(
      """
        |package com.squareup.tacos
        |
        |import kotlin.String
        |import kotlin.Unit
        |
        |public fun a(): Unit {
        |}
        |
        |public class B
        |
        |public val c: String = "C"
        |
        |public fun d(): Unit {
        |}
        |
        |public class E
        |
        |public val f: String = "F"
        |""".trimMargin()
    )

Taken from here:

https://github.com/square/kotlinpoet/blob/7c6f4dbc324fff1019d3e625f8a5dbace6c7905c/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt#L49

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30320571) – Siddharth Kamaria Nov 12 '21 at 10:29
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted](//stackoverflow.com/help/deleted-answers). –  Dec 15 '21 at 22:16
0

Since Kotlin types are just aliases for the underlying Java types you indeed won't get the correct type information without reading it from the @Metadata class annotation. Try the interop:kotlinx-metadata artifact that contains helpers for extracting this kind of information from the metadata.

Egor
  • 39,695
  • 10
  • 113
  • 130