如何生成具有以下签名的方法?
public <T extends MyClass> void doSomething(T t)到目前为止,我已经:
MethodSpec.methodBuilder("doSomething")
.addModifiers(Modifier.PUBLIC)
.addTypeVariable(TypeVariableName.get("T", MyClass.class))
.build()编辑--这就是上面的代码生成的内容(我不知道如何添加参数):
public <T extends Myclass> void doSomething()发布于 2015-06-21 22:38:56
将生成的TypeVariableName提取到变量中,以便重用其值。
TypeVariableName typeVariableName = TypeVariableName.get("T", MyClass.class);然后添加该类型的参数。
MethodSpec spec = MethodSpec.methodBuilder("doSomething")
.addModifiers(Modifier.PUBLIC)
.addTypeVariable(typeVariableName)
.addParameter(typeVariableName, "t") // you can also add modifiers
.build();发布于 2016-09-19 07:29:11
如果要传递泛型类型化结构,请使用以下方式。
MethodSpec loadListInteger = MethodSpec.methodBuilder("loadListInteger")
.addModifiers(Modifier.PUBLIC)
.returns(void.class)
.addParameter(ParameterizedTypeName.get(List.class, Integer.class), "list")
.build();https://stackoverflow.com/questions/30969986
复制相似问题