关于Xtend,我有一个非常具体的问题。
在我读到的关于xText/xTend的每个示例中,我都看到了类似这样的内容:
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
for(e: resource.allContents.toIterable.filter(typeof(Entity))) {
fsa.generateFile(
e.fullyQualifiedName.toString("/") + ".java",
e.compile)
}
}更确切地说,是resource.allContents.toIterable.filter(typeof(Entity)))这一行引起了我的问题。我想知道如何从所有Entitys向下查找资源树,而不是Entity的子类。方法filter获取资源中entity类型及其子类的所有对象,但我只想省略子类,只获取实体。
发布于 2012-11-06 20:57:20
请尝试以下表达式:
allContents.toIterable.filter(typeof(Entity)).filter[ getClass == typeof(Entity) ]第一个过滤器表达式在您描述的意义上是类型安全的(它返回一个Iterable,而第二个过滤器表达式确保您不会产生任何子类型。
如果使用EMF,这将不会产生任何结果,因为Entity是一个接口,而具体的类将类似于EntityImpl。在这种情况下,我建议使用EMF来过滤所有“真实”实体:
allContents.toIterable
.filter(typeof(Entity))
.filter[ eClass == MyEPackage$Literals::ENTITY ]https://stackoverflow.com/questions/13251362
复制相似问题