Jvm 如何生成Kotlin的构造函数参数;单位「;是否使用带有kotlinpoet的单个类型参数键入?
这对于在这里发布可能有点太具体了,但我正试图用kotlinpoet生成一个类似这样的类:Jvm 如何生成Kotlin的构造函数参数;单位「;是否使用带有kotlinpoet的单个类型参数键入?,jvm,kotlin,template-meta-programming,kotlinpoet,Jvm,Kotlin,Template Meta Programming,Kotlinpoet,这对于在这里发布可能有点太具体了,但我正试图用kotlinpoet生成一个类似这样的类: class Query<out E: Model>(val onSuccess: (E) -> Unit, val onError: (Int, String) -> Unit = { i, m -> }) class查询(val-onSuccess:(E)->Unit,val-onError:(Int,String)->Unit={i,m->}) 如何使用kotlinpo
class Query<out E: Model>(val onSuccess: (E) -> Unit, val onError: (Int, String) -> Unit = { i, m -> })
class查询(val-onSuccess:(E)->Unit,val-onError:(Int,String)->Unit={i,m->})
如何使用kotlinpoet创建该类型/构造函数参数?确实有“
Unit
”类型与基元类型一起列出,因此这似乎是一个特例。很容易,它是通过使用类来完成的。在习惯了kotlin的函数类型风格而不是java严格的函数接口之后,这有点令人困惑。以下是我使用的:
val typeVariable = TypeVariableName.invoke("E")
.withBounds(QueryData::class)
ParameterSpec.builder("onSuccess",
LambdaTypeName.get(null, listOf(typeVariable), UNIT)).build()
这会生成(当然是类生成器):
类查询{
建造商(成功时:(E)->单位){
}
}
下面是一个程序,它可以生成您需要的输出:
class Model
fun main(args: Array<String>) {
val onSuccessType = LambdaTypeName.get(
parameters = TypeVariableName(name = "E"),
returnType = Unit::class.asTypeName())
val onErrorType = LambdaTypeName.get(
parameters = listOf(Int::class.asTypeName(), String::class.asTypeName()),
returnType = Unit::class.asTypeName())
val primaryConstructor = FunSpec.constructorBuilder()
.addParameter(ParameterSpec.builder(name = "onSuccess", type = onSuccessType)
.build())
.addParameter(ParameterSpec.builder(name = "onError", type = onErrorType)
.defaultValue("{ i, m -> }")
.build())
.build()
val querySpec = TypeSpec.classBuilder("Query")
.addTypeVariable(TypeVariableName(name = "out E", bounds = Model::class))
.addProperty(PropertySpec.builder(name = "onSuccess", type = onSuccessType)
.initializer("onSuccess")
.build())
.addProperty(PropertySpec.builder(name = "onError", type = onErrorType)
.initializer("onError")
.build())
.primaryConstructor(primaryConstructor)
.build()
val file = KotlinFile.builder(packageName = "", fileName = "test")
.addType(querySpec)
.build()
file.writeTo(System.out)
}
类模型
趣味主线(args:Array){
val onSuccessType=LambdaTypeName.get(
parameters=TypeVariableName(name=“E”),
returnType=Unit::class.asTypeName()
val onErrorType=LambdaTypeName.get(
parameters=listOf(Int::class.asTypeName(),String::class.asTypeName()),
returnType=Unit::class.asTypeName()
val primaryConstructor=FunSpec.constructorBuilder()
.addParameter(ParameterSpec.builder(name=“onSuccess”,type=onSuccessType)
.build())
.addParameter(ParameterSpec.builder(name=“onError”,type=onErrorType)
.defaultValue(“{i,m->}”)
.build())
.build()
val querySpec=TypeSpec.classBuilder(“查询”)
.addTypeVariable(TypeVariableName(name=“out E”,bounds=Model::class))
.addProperty(PropertySpec.builder(name=“onSuccess”,type=onSuccessType)
.初始值设定项(“成功”)
.build())
.addProperty(PropertySpec.builder(name=“onError”,type=onErrorType)
.初始值设定人(“onError”)
.build())
.primaryConstructor(primaryConstructor)
.build()
val file=kotlinfle.builder(packageName=“”,fileName=“test”)
.addType(querySpec)
.build()
file.writeTo(System.out)
}
这将打印(不包括生成的导入)以下内容:
class Query<out E : Model>(val onSuccess: (E) -> Unit,
val onError: (Int, String) -> Unit = { i, m -> })
类查询(val onSuccess:(E)->单元,
val onError:(Int,String)->Unit={i,m->})
我正在将
TypeVariableName
作为out E
进行黑客攻击,因为当时似乎没有更好的解决方案。我还使用了0.4.0-SNAPSHOT
版本。您应该能够将TypeVariableName.Companion.invoke(“E”)
缩短为TypeVariableName(“E”)
@KirillRakhman,我想这是因为我在“自动完成编程”,即点击Ctrl+Space来学习API
class Query<out E : Model>(val onSuccess: (E) -> Unit,
val onError: (Int, String) -> Unit = { i, m -> })