如何让 javapoet 生成下面的 java 代码?
class B<T extends U> implements A<T> {
}
我知道有一个class WildcardTypeName
,但它只能生成?extends U
或? super U
。
我想要的是 T extends U
在你的描述,U
而A
应该是存在的类。您可以使用以下代码。
public static void main(String[] args) throws IOException {
TypeVariableName t = TypeVariableName.get("T").withBounds(U.class);
TypeSpec type = TypeSpec.classBuilder("B")
.addTypeVariable(t)
.addSuperinterface(ParameterizedTypeName.get(ClassName.get(A.class), t))
.build();
JavaFile.builder("", type).build().writeTo(System.out);
}
它的输出是
import yourpackage.A;
import yourpackage.U;
class B<T extends U> implements A<T> {
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句