如何让javapoet生成下面的java代码?
class B<T extends U> implements A<T> {
}我知道有一个class WildcardTypeName,但它只能生成?extends U或? super U。
我想要的是T extends U
发布于 2018-08-20 15:57:20
在您描述中,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> {
}https://stackoverflow.com/questions/51915662
复制相似问题