我想在从 DSL 生成的类中生成一个列表字段,并像这样初始化它:
private List<MyObject> myObjects= Lists.newArrayList();
我知道的唯一方法是将一些文本附加到初始值设定项:
members += appRule.toField("myObjects", appRule.newTypeRef(List, it.newTypeRef(MyObject))) [
initializer = [append('''Lists.newArrayList()''')]
]
但是,使用这种方法 JvmModelInferrer 不会导入 Guava Strings 库,因此会引发编译问题。有什么办法可以克服这个障碍吗?
最佳答案
如果我理解您的问题(因为您指的是代码中未使用的 Guava Strings 库:)),那么您的问题是未导入类引用列表。
对于此类构造,我们在 EMF-IncQuery 中有一个辅助方法,它以与序列化参数相同的方式序列化类型引用。此功能依赖于可注入(inject)的 TypeReferenceSerializer 类。
def referClass(ITreeAppendable appendable, EObject ctx, Class<?> clazz, JvmTypeReference... typeArgs) {
val ref = ctx.newTypeRef(clazz, typeArgs)
if (ref != null) {
appendable.serialize(ref, ctx)
} else {
//Class resolution error - error handling required here
//A fallback to writing out the fqn of the class
appendable.append(clazz.canonicalName)
}
}
def serialize(ITreeAppendable appendable, JvmTypeReference ref, EObject ctx) {
typeReferenceSerializer.serialize(ref, ctx, appendable)
}
关于eclipse - Xtext:JvmModelInferrer 初始化字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19478540/