英文:
How to add a parameterized return type of function in Java Poet
问题
public ServiceA<? extends ServiceB> anyFunctionName() {
// code
}
Problem:
不确定如何添加返回类型:`ServiceA<? extends ServiceB>`
假设我有ServiceA和ServiceB的完全限定类名。
我尝试的代码如下,但不起作用。
MethodSpec.Builder builder =
MethodSpec.methodBuilder("anyFunctionName")
.addModifiers(Modifier.PRIVATE)
.returns(ParameterSpec.builder(ParameterizedTypeName.get(
ClassName.bestGuess("com.learn.ServiceA"),
WildcardTypeName.subtypeOf(Object.class),
ClassName.bestGuess("com.learn.ServiceB")).build());
请注意,上述内容是您提供的原始文本的翻译部分。如果您有任何其他翻译需求,请随时提问。
英文:
How would I generate a method with the following signature?
public ServiceA<? extends ServiceB> anyFunctionName() {
// code
}
Problem:
Not sure how to add return type : ServiceA<? extends ServiceB>
Assuming I have FQCN for ServiceA and ServiceB.
What I am trying is code below, but does'nt work.
MethodSpec.Builder builder =
MethodSpec.methodBuilder("anyFunctionName")
.addModifiers(Modifier.PRIVATE)
.returns(ParameterSpec.builder(ParameterizedTypeName.get(
ClassName.bestGuess("com.learn.ServiceA"),
WildcardTypeName.subtypeOf(Object.class),
ClassName.bestGuess("com.learn.ServiceB")).build());
The above code generates the following code:
public ServiceA<? , ServiceB> anyFunctionName() {
// code
}
Any lead would be appreciated.
答案1
得分: 1
非常感谢 @Michael 指出了我所犯的错误。
以下代码有效。
MethodSpec.Builder builder =
MethodSpec.methodBuilder("anyFunctionName")
.addModifiers(Modifier.PRIVATE)
.returns(ParameterizedTypeName.get(
ClassName.bestGuess("com.learn.ServiceA"),
WildcardTypeName.subtypeOf(ClassName.bestGuess("com.learn.ServiceB")));
英文:
Thanks a lot, @Michael for pointing out the mistake I was doing.
Following code worked.
MethodSpec.Builder builder =
MethodSpec.methodBuilder("anyFunctionName")
.addModifiers(Modifier.PRIVATE)
.returns(ParameterizedTypeName.get(
ClassName.bestGuess("com.learn.ServiceA"),
WildcardTypeName.subtypeOf(ClassName.bestGuess("com.learn.ServiceB")));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论