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())
}
}
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.
KotlinPoet will output kotlin.String. Take a look at the kotlinpoet unit tests for a lot of examples, for example here:
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.