1

i have a very specific Question about Xtend.

In every example i read about xText/xTend i see something like this:

override void doGenerate(Resource resource, IFileSystemAccess fsa) {
 for(e: resource.allContents.toIterable.filter(typeof(Entity))) {
   fsa.generateFile(
   e.fullyQualifiedName.toString("/") + ".java",
   e.compile)
 }
}

Well so be more specific the line resource.allContents.toIterable.filter(typeof(Entity))) is the one that causes me problems. I want to know how to go down the resource tree from all the Entitys without the subclasses of the entities. The method filter gets all the objects of the type entity and its subclasses within the resource but i just want to leave out the subclasses and only get the entities.

1 Answers1

2

Please try the following expression:

allContents.toIterable.filter(typeof(Entity)).filter[ getClass == typeof(Entity) ]

The first filter expression is typesafe in the sense that you describe (it returns an Iterable whereas the second filter expression ensures that you don't yield any subtypes.

If you use EMF, this will not yield any results since Entity is an interface and the concrete class would be something like EntityImpl. In that case, I recommend to use the EMF APIs to filter for all 'real' entities:

allContents.toIterable
  .filter(typeof(Entity))
  .filter[ eClass == MyEPackage$Literals::ENTITY ]
Sebastian Zarnekow
  • 6,609
  • 20
  • 23
  • First of all thank you for your answer. I tried your expression but the result of this is that there are no entities in this iterable. It is empty. :( –  Nov 08 '12 at 08:24
  • I updated the answer to reflect the differences between Java class and EMF EClass. – Sebastian Zarnekow Nov 08 '12 at 08:30
  • Yes thank you found that out a minute ago the class is org.xtext.example.mydsl.myDsl.impl.EntityImpl like you said. But what should be imported to use MyEPackage$Literals? –  Nov 08 '12 at 08:42
  • The EPackage that defines your class Entity. Please dive into EMF for details (or use EntityImpl). – Sebastian Zarnekow Nov 08 '12 at 08:59
  • Well one solution for my problem (not one of the best i would say...) is something like this if(e.eClass().name.toString.equals("Entity")) This doesn't filter the Entity at all but to make sure its an Entity i can use this... –  Nov 08 '12 at 09:53
  • Sebastian i really dont unterstand what you answered. The EPackage of Entity is org.xtext.example.mydsl.myDsl.Entity or am i wrong? So the line is .filter( eClass == org.xtext.example.mydsl.myDsl::Entity)? I only get errors... –  Nov 11 '12 at 20:36