泛型的代码模型狭窄

codemodel narrow for generics

我想生成一个新的 class 如下:

public class QueryBuilder_QueryField
extends AbstractQueryBuilder<QueryBuilder_QueryField>

我试过了,但它并没有缩小扩展...

JCodeModel model = new JCodeModel();
String fqcn = packageName + "." + className;
JDefinedClass builderClass = model._class(fqcn);
builderClass._extends(AbstractQueryBuilder.class).narrow(builderClass);

但结果却像

public class QueryBuilder_QueryField
extends AbstractQueryBuilder

有什么想法吗?

我认为在将 class 传递给 _extends() 之前您需要缩小范围:

JCodeModel model = new JCodeModel();
String fqcn = packageName + "." + className;
JDefinedClass builderClass = model._class(fqcn);
builderClass._extends(model.ref(AbstractQueryBuilder.class).narrow(builderClass));

生成:

public class QueryBuilder_QueryField
extends AbstractQueryBuilder<QueryBuilder_QueryField>