EMF中的参考类型

时间:2013-09-16 17:57:40

标签: eclipse-emf

如何在EMF(Eclipse Modeling Framework)中获得“哪种类型的引用将引用特定的类对象”?有时,对象将引用3或4个引用类型。我想获得针对该特定对象引用的引用数量。 要么 如何在EMF中获取父对象的子类

1 个答案:

答案 0 :(得分:0)

没有直接的方法来获取特定EClass的子类列表。你必须自己做这项工作。基本上遍历所有已注册包的所有类并测试继承树:

/**
 * Returns a list of all <em>known</em> sub-classes for the specified class.
 * 
 * @param cls the super-class
 * @return list of all sub-classes - possibly <code>null</code>
 */
public static Collection<EClass> getSubClasses(EClass cls) {
   if (SUB_CLASSES.containsKey(cls)) return SUB_CLASSES.get(cls);

   Collection<EClass> l = null;
   final Registry registry = EPackage.Registry.INSTANCE;
   for (final Object v : registry.values()) {
      if (!(v instanceof EPackage)) {
     continue;
      }
      final EPackage ep = (EPackage) v;

      for (final EClassifier c : ep.getEClassifiers()) {
     if (!(c instanceof EClass)) {
        continue;
     }
     final EClass cl = (EClass) c;

     if (cl.getESuperTypes().contains(cls)) {
        if (l == null) {
           l = new ArrayList<EClass>();
        }
        l.add(cl);
     }
      }
   }
   SUB_CLASSES.put(cls, l);

   return l;
}

static final Map<EClass, Collection<EClass>> SUB_CLASSES = new HashMap<EClass, Collection<EClass>>();