简体   繁体   中英

How do I get Kotlin classes instead of java classes

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:

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

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