0

如何在 EMF(Eclipse 建模框架)中获得“哪种类型的引用将引用特定的类对象”?有时对象会引用 3 或 4 个引用类型。我想获取为该特定对象引用的引用数量。或如何在 EMF 中获取父对象的子类

4

1 回答 1

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>>();
于 2013-09-18T14:19:30.960 回答