Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 848d8c258631e0623538dcbdb49654cbdcf71f29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
 * Copyright (c) 2008-2014, 2016 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 *    Stefan Winkler - Bug 332912 - Caching subtype-relationships in the CDOPackageRegistry
 *    Erdal Karaca - added support for HASHMAP CDO Type
 *    Christian W. Damus (CEA) - don't validate cross-references in EAnnotations
 */
package org.eclipse.emf.cdo.common.model;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.internal.common.bundle.OM;
import org.eclipse.emf.cdo.internal.common.messages.Messages;
import org.eclipse.emf.cdo.internal.common.model.CDOClassInfoImpl;
import org.eclipse.emf.cdo.internal.common.model.CDOPackageInfoImpl;
import org.eclipse.emf.cdo.internal.common.model.CDOPackageRegistryImpl;
import org.eclipse.emf.cdo.internal.common.model.CDOPackageUnitImpl;
import org.eclipse.emf.cdo.internal.common.model.CDOTypeImpl;

import org.eclipse.net4j.util.io.ExtendedDataInput;
import org.eclipse.net4j.util.io.ExtendedDataOutput;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.emf.ecore.EGenericType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.util.FeatureMapUtil;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Various static helper methods for dealing with CDO meta models.
 *
 * @author Eike Stepper
 * @since 2.0
 * @apiviz.uses {@link CDOType} - - provides
 */
public final class CDOModelUtil implements CDOModelConstants
{
  private static final EClass ROOT_TYPE = EcorePackage.eINSTANCE.getEObject();

  private static CDOType[] coreTypes;

  static
  {
    List<CDOType> types = new ArrayList<CDOType>();
    registerCoreType(types, EcorePackage.eINSTANCE.getEBigDecimal(), CDOType.BIG_DECIMAL);
    registerCoreType(types, EcorePackage.eINSTANCE.getEBigInteger(), CDOType.BIG_INTEGER);
    registerCoreType(types, EcorePackage.eINSTANCE.getEBooleanObject(), CDOType.BOOLEAN_OBJECT);
    registerCoreType(types, EcorePackage.eINSTANCE.getEBoolean(), CDOType.BOOLEAN);
    registerCoreType(types, EcorePackage.eINSTANCE.getEByteArray(), CDOType.BYTE_ARRAY);
    registerCoreType(types, EcorePackage.eINSTANCE.getEByteObject(), CDOType.BYTE_OBJECT);
    registerCoreType(types, EcorePackage.eINSTANCE.getEByte(), CDOType.BYTE);
    registerCoreType(types, EcorePackage.eINSTANCE.getECharacterObject(), CDOType.CHARACTER_OBJECT);
    registerCoreType(types, EcorePackage.eINSTANCE.getEChar(), CDOType.CHAR);
    registerCoreType(types, EcorePackage.eINSTANCE.getEDate(), CDOType.DATE);
    registerCoreType(types, EcorePackage.eINSTANCE.getEDoubleObject(), CDOType.DOUBLE_OBJECT);
    registerCoreType(types, EcorePackage.eINSTANCE.getEDouble(), CDOType.DOUBLE);
    registerCoreType(types, EcorePackage.eINSTANCE.getEFloatObject(), CDOType.FLOAT_OBJECT);
    registerCoreType(types, EcorePackage.eINSTANCE.getEFloat(), CDOType.FLOAT);
    registerCoreType(types, EcorePackage.eINSTANCE.getEIntegerObject(), CDOType.INTEGER_OBJECT);
    registerCoreType(types, EcorePackage.eINSTANCE.getEInt(), CDOType.INT);
    registerCoreType(types, EcorePackage.eINSTANCE.getEJavaClass(), CDOType.JAVA_CLASS);
    registerCoreType(types, EcorePackage.eINSTANCE.getEJavaObject(), CDOType.JAVA_OBJECT);
    registerCoreType(types, EcorePackage.eINSTANCE.getELongObject(), CDOType.LONG_OBJECT);
    registerCoreType(types, EcorePackage.eINSTANCE.getELong(), CDOType.LONG);
    registerCoreType(types, EcorePackage.eINSTANCE.getEShortObject(), CDOType.SHORT_OBJECT);
    registerCoreType(types, EcorePackage.eINSTANCE.getEShort(), CDOType.SHORT);
    registerCoreType(types, EcorePackage.eINSTANCE.getEString(), CDOType.STRING);
    coreTypes = types.toArray(new CDOType[types.size()]);
  }

  private static void registerCoreType(List<CDOType> types, EClassifier classifier, CDOType type)
  {
    int index = classifier.getClassifierID();
    while (index >= types.size())
    {
      types.add(null);
    }

    types.set(index, type);
  }

  private CDOModelUtil()
  {
  }

  /**
   * @since 2.0
   */
  public static boolean isCorePackage(EPackage ePackage)
  {
    if (CDOPackageRegistryImpl.SYSTEM_ELEMENTS[0] != null)
    {
      return CDOPackageRegistryImpl.SYSTEM_ELEMENTS[0] == ePackage;
    }

    String nsURI = ePackage.getNsURI();
    return CORE_PACKAGE_URI.equals(nsURI);
  }

  /**
   * @since 2.0
   */
  public static boolean isRoot(EClass eClass)
  {
    if (CDOPackageRegistryImpl.SYSTEM_ELEMENTS[1] != null)
    {
      return CDOPackageRegistryImpl.SYSTEM_ELEMENTS[1] == eClass;
    }

    if (isCorePackage(eClass.getEPackage()))
    {
      String name = eClass.getName();
      return ROOT_CLASS_NAME.equals(name);
    }

    return false;
  }

  /**
   * @since 2.0
   */
  public static boolean isResourcePackage(EPackage ePackage)
  {
    if (CDOPackageRegistryImpl.SYSTEM_ELEMENTS[2] != null)
    {
      return CDOPackageRegistryImpl.SYSTEM_ELEMENTS[2] == ePackage;
    }

    String nsURI = ePackage.getNsURI();
    return RESOURCE_PACKAGE_URI.equals(nsURI);
  }

  /**
   * @since 2.0
   */
  public static boolean isResource(EClass eClass)
  {
    if (CDOPackageRegistryImpl.SYSTEM_ELEMENTS[3] != null)
    {
      return CDOPackageRegistryImpl.SYSTEM_ELEMENTS[3] == eClass;
    }

    if (isResourcePackage(eClass.getEPackage()))
    {
      String name = eClass.getName();
      return RESOURCE_CLASS_NAME.equals(name);
    }

    return false;
  }

  /**
   * @since 2.0
   */
  public static boolean isResourceFolder(EClass eClass)
  {
    if (CDOPackageRegistryImpl.SYSTEM_ELEMENTS[4] != null)
    {
      return CDOPackageRegistryImpl.SYSTEM_ELEMENTS[4] == eClass;
    }

    if (isResourcePackage(eClass.getEPackage()))
    {
      String name = eClass.getName();
      return RESOURCE_FOLDER_CLASS_NAME.equals(name);
    }

    return false;
  }

  /**
   * @since 2.0
   */
  public static boolean isResourceNode(EClass eClass)
  {
    return isResourcePackage(eClass.getEPackage());
  }

  /**
   * @since 4.3
   */
  public static boolean isResourcePathFeature(EStructuralFeature eStructuralFeature)
  {
    if (CDOPackageRegistryImpl.SYSTEM_ELEMENTS[8] == eStructuralFeature)
    {
      return true;
    }

    if (CDOPackageRegistryImpl.SYSTEM_ELEMENTS[9] == eStructuralFeature)
    {
      return true;
    }

    if (isResourceNode(eStructuralFeature.eClass()))
    {
      String name = eStructuralFeature.getName();
      if (RESOURCE_NODE_FOLDER_REFERENCE.equals(name) || RESOURCE_NODE_NAME_ATTRIBUTE.equals(name))
      {
        return true;
      }
    }

    return false;
  }

  /**
   * @since 4.0
   */
  public static boolean isTypesPackage(EPackage ePackage)
  {
    if (CDOPackageRegistryImpl.SYSTEM_ELEMENTS[5] != null)
    {
      return CDOPackageRegistryImpl.SYSTEM_ELEMENTS[5] == ePackage;
    }

    String nsURI = ePackage.getNsURI();
    return TYPES_PACKAGE_URI.equals(nsURI);
  }

  /**
   * @since 2.0
   */
  public static boolean isSystemPackage(EPackage ePackage)
  {
    return isCorePackage(ePackage) || isResourcePackage(ePackage) || isTypesPackage(ePackage);
  }

  /**
   * @since 4.0
   */
  public static boolean isLob(EClassifier eClassifier)
  {
    if (CDOPackageRegistryImpl.SYSTEM_ELEMENTS[6] != null && CDOPackageRegistryImpl.SYSTEM_ELEMENTS[7] != null)
    {
      return CDOPackageRegistryImpl.SYSTEM_ELEMENTS[6] == eClassifier || CDOPackageRegistryImpl.SYSTEM_ELEMENTS[7] == eClassifier;
    }

    if (isTypesPackage(eClassifier.getEPackage()))
    {
      String name = eClassifier.getName();
      return BLOB_CLASS_NAME.equals(name) || CLOB_CLASS_NAME.equals(name);
    }

    return false;
  }

  /**
   * @since 2.0
   */
  public static CDOType getType(EStructuralFeature feature)
  {
    if (FeatureMapUtil.isFeatureMap(feature))
    {
      return CDOType.FEATURE_MAP_ENTRY;
    }

    return getType(feature.getEType());
  }

  /**
   * @since 4.0
   */
  public static CDOType getType(byte typeID)
  {
    return CDOTypeImpl.getType(typeID);
  }

  /**
   * @since 2.0
   */
  public static CDOType getType(EClassifier classifier)
  {
    if (classifier instanceof EClass)
    {
      return CDOType.OBJECT;
    }

    if (classifier instanceof EEnum)
    {
      return CDOType.ENUM_ORDINAL;
    }

    EDataType eDataType = (EDataType)classifier;
    EPackage ePackage = eDataType.getEPackage();

    if (isCorePackage(ePackage))
    {
      CDOType type = getCoreType(eDataType);
      if (type != null)
      {
        return type;
      }
    }
    else if (isTypesPackage(ePackage))
    {
      String name = eDataType.getName();
      if (BLOB_CLASS_NAME.equals(name))
      {
        return CDOType.BLOB;
      }

      if (CLOB_CLASS_NAME.equals(name))
      {
        return CDOType.CLOB;
      }

      throw new IllegalArgumentException("Illegal data type: " + eDataType);
    }

    return CDOType.CUSTOM;
  }

  /**
   * Core types includes also complex data like EAnnotation, and EEnum
   *
   * @since 2.0
   */
  public static CDOType getCoreType(EClassifier eDataType)
  {
    int index = eDataType.getClassifierID();
    if (0 <= index && index < coreTypes.length)
    {
      return coreTypes[index];
    }

    return null;
  }

  /**
   * @since 2.0
   */
  public static CDOType getPrimitiveType(Class<? extends Object> primitiveType)
  {
    if (primitiveType == String.class)
    {
      return CDOType.STRING;
    }

    if (primitiveType == Boolean.class)
    {
      return CDOType.BOOLEAN;
    }

    if (primitiveType == Integer.class)
    {
      return CDOType.INT;
    }

    if (primitiveType == Double.class)
    {
      return CDOType.DOUBLE;
    }

    if (primitiveType == Float.class)
    {
      return CDOType.FLOAT;
    }

    if (primitiveType == Long.class)
    {
      return CDOType.LONG;
    }

    if (primitiveType == Date.class)
    {
      return CDOType.DATE;
    }

    if (primitiveType == Byte.class)
    {
      return CDOType.BYTE;
    }

    if (primitiveType == Character.class)
    {
      return CDOType.CHAR;
    }

    if (primitiveType == Object[].class)
    {
      return CDOType.OBJECT_ARRAY;
    }

    if (EEnumLiteral.class.isAssignableFrom(primitiveType) || Enumerator.class.isAssignableFrom(primitiveType))
    {
      return CDOType.ENUM_LITERAL;
    }

    if (Map.class.isAssignableFrom(primitiveType))
    {
      return CDOType.MAP;
    }

    if (Set.class.isAssignableFrom(primitiveType))
    {
      return CDOType.SET;
    }

    if (List.class.isAssignableFrom(primitiveType))
    {
      return CDOType.LIST;
    }

    throw new IllegalArgumentException(MessageFormat.format(Messages.getString("CDOModelUtil.7"), primitiveType)); //$NON-NLS-1$
  }

  /**
   * @since 4.0
   */
  public static CDOType getTypeOfObject(Object object)
  {
    if (object == null || object instanceof CDOID || object instanceof CDORevision)
    {
      return CDOType.OBJECT;
    }

    Class<? extends Object> objectClass = object.getClass();
    if (objectClass == String.class)
    {
      return CDOType.STRING;
    }

    if (objectClass == Integer.class)
    {
      return CDOType.INTEGER_OBJECT;
    }

    if (objectClass == Long.class)
    {
      return CDOType.LONG_OBJECT;
    }

    if (objectClass == Boolean.class)
    {
      return CDOType.BOOLEAN_OBJECT;
    }

    if (objectClass == Character.class)
    {
      return CDOType.CHARACTER_OBJECT;
    }

    if (objectClass == Double.class)
    {
      return CDOType.DOUBLE_OBJECT;
    }

    if (objectClass == Float.class)
    {
      return CDOType.FLOAT_OBJECT;
    }

    if (objectClass == Short.class)
    {
      return CDOType.SHORT_OBJECT;
    }

    if (objectClass == Byte.class)
    {
      return CDOType.BYTE_OBJECT;
    }

    if (objectClass == byte[].class)
    {
      return CDOType.BYTE_ARRAY;
    }

    if (objectClass == Date.class || object instanceof Date)
    {
      return CDOType.DATE;
    }

    if (object instanceof EEnumLiteral)
    {
      return CDOType.ENUM_LITERAL;
    }

    if (objectClass == BigDecimal.class || object instanceof BigDecimal)
    {
      return CDOType.BIG_DECIMAL;
    }

    if (objectClass == BigInteger.class || object instanceof BigInteger)
    {
      return CDOType.BIG_INTEGER;
    }

    if (object instanceof FeatureMap.Entry)
    {
      return CDOType.FEATURE_MAP_ENTRY;
    }

    if (object instanceof Throwable)
    {
      return CDOType.EXCEPTION;
    }

    throw new IllegalArgumentException("Object type " + objectClass.getName() + " is not supported.");
  }

  /**
   * @since 2.0
   */
  public static CDOPackageInfo getPackageInfo(Object value, CDOPackageRegistry packageRegistry)
  {
    if (value instanceof EPackage)
    {
      return packageRegistry.getPackageInfo((EPackage)value);
    }

    if (value instanceof CDOPackageInfo)
    {
      CDOPackageInfo packageInfo = (CDOPackageInfo)value;
      if (packageInfo.getPackageUnit().getPackageRegistry() == packageRegistry)
      {
        return packageInfo;
      }
    }

    return null;
  }

  /**
   * Returns additional CDO infos for an {@link EClass}.
   * <p>
   * This operation is somewhat expensive because it synchronizes on the EClass and iterates over all adapters.
   * Whenever possible use {@link CDORevision#getClassInfo()} or <code>InternalCDOObject.getClassInfo()</code>.
   *
   * @since 2.0
   */
  public static CDOClassInfo getClassInfo(EClass eClass)
  {
    synchronized (eClass)
    {
      EList<Adapter> adapters = eClass.eAdapters();
      CDOClassInfo classInfo = (CDOClassInfo)EcoreUtil.getAdapter(adapters, CDOClassInfo.class);
      if (classInfo == null)
      {
        classInfo = new CDOClassInfoImpl();
        adapters.add(0, classInfo);
      }

      return classInfo;
    }
  }

  /**
   * Returns all persistent {@link EStructuralFeature features} of an {@link EClass}.
   * <p>
   * This operation is somewhat expensive because it synchronizes on the EClass and iterates over all adapters.
   *
   * @since 2.0
   * @deprecated As of 4.2 use <code>CDOModelUtil.getClassInfo(eClass).getAllPersistentFeatures()</code>.
   * @see #getClassInfo(EClass)
   */
  @Deprecated
  public static EStructuralFeature[] getAllPersistentFeatures(EClass eClass)
  {
    CDOClassInfo classInfo = getClassInfo(eClass);
    return classInfo.getAllPersistentFeatures();
  }

  /**
   * @since 2.0
   */
  public static CDOPackageUnit createPackageUnit()
  {
    return new CDOPackageUnitImpl();
  }

  /**
   * @since 2.0
   */
  public static CDOPackageInfo createPackageInfo()
  {
    return new CDOPackageInfoImpl();
  }

  /**
   * @since 3.0
   */
  public static EPackage readPackage(ExtendedDataInput in, ResourceSet resourceSet, boolean lookForResource) throws IOException
  {
    String uri = in.readString();
    boolean zipped = in.readBoolean();
    byte[] bytes = in.readByteArray();
    return EMFUtil.createEPackage(uri, bytes, zipped, resourceSet, lookForResource);
  }

  /**
   * @since 2.0
   */
  public static void writePackage(ExtendedDataOutput out, EPackage ePackage, boolean zipped, EPackage.Registry packageRegistry) throws IOException
  {
    checkCrossResourceURIs(ePackage);

    byte[] bytes = EMFUtil.getEPackageBytes(ePackage, zipped, packageRegistry);
    out.writeString(ePackage.getNsURI());
    out.writeBoolean(zipped);
    out.writeByteArray(bytes);
  }

  /**
   * @since 3.0
   */
  public static void checkCrossResourceURIs(EPackage ePackage)
  {
    TreeIterator<EObject> it = ePackage.eAllContents();
    while (it.hasNext())
    {
      EObject e = it.next();

      if (e instanceof EAnnotation)
      {
        // we don't need to validate the structure of annotations. The applications that
        // define annotations will have to take what they can get
        it.prune();
      }
      else
      {
        for (EObject r : e.eCrossReferences())
        {
          EObject refTarget = null;

          if (r.eIsProxy())
          {
            String msg = "Package '%s' contains unresolved proxy '%s'";
            msg = String.format(msg, ePackage.getNsURI(), ((InternalEObject)r).eProxyURI());
            throw new IllegalStateException(msg);
          }

          if (r.eResource() != null && r.eResource() != e.eResource())
          {
            // It's a ref into another resource
            EPackage pkg = null;
            if (r instanceof EClassifier)
            {
              refTarget = r;
              pkg = ((EClassifier)r).getEPackage();
            }
            else if (r instanceof EStructuralFeature)
            {
              refTarget = r;
              EStructuralFeature feature = (EStructuralFeature)r;
              EClass ownerClass = (EClass)feature.eContainer();
              pkg = ownerClass.getEPackage();
            }
            else if (r instanceof EGenericType)
            {
              EGenericType genType = (EGenericType)r;
              EClassifier c = genType.getEClassifier();
              if (c != null)
              {
                refTarget = c;
                pkg = c.getEPackage();
              }
            }

            if (pkg == null)
            {
              continue;
            }

            while (pkg.getESuperPackage() != null)
            {
              pkg = pkg.getESuperPackage();
            }

            String resourceURI = refTarget.eResource().getURI().toString();
            if (!resourceURI.toString().equals(pkg.getNsURI()))
            {
              String msg = "URI of the resource (%s) does not match the nsURI (%s) of the top-level package;\n"
                  + "this can be fixed by calling Resource.setURI(URI) after loading the packages,\n"
                  + "or by configuring a URI mapping from nsURI's to location URI's before loading the packages,\n"
                  + "and then loading them with their nsURI's";
              msg = String.format(msg, resourceURI, pkg.getNsURI());
              throw new IllegalStateException(msg);
            }
          }
        }
      }
    }
  }

  /**
   * @since 4.0
   */
  public static Map<EClass, List<EClass>> getSubTypes(EPackage.Registry packageRegistry)
  {
    Map<EClass, List<EClass>> result = new HashMap<EClass, List<EClass>>();
    for (String nsURI : packageRegistry.keySet())
    {
      EPackage ePackage = packageRegistry.getEPackage(nsURI);
      getSubTypes(ePackage, result);
    }

    return result;
  }

  private static void getSubTypes(EPackage ePackage, Map<EClass, List<EClass>> result)
  {
    for (EClassifier classifier : ePackage.getEClassifiers())
    {
      if (classifier instanceof EClass)
      {
        EClass eClass = (EClass)classifier;
        getSubType(eClass, ROOT_TYPE, result);

        for (EClass eSuperType : eClass.getEAllSuperTypes())
        {
          getSubType(eClass, eSuperType, result);
        }
      }
    }
  }

  private static void getSubType(EClass eClass, EClass eSuperType, Map<EClass, List<EClass>> result)
  {
    if (eSuperType.eIsProxy())
    {
      OM.LOG.warn("getSubTypes encountered a proxy EClass which will be ignored: " + eSuperType);
      return;
    }

    List<EClass> list = result.get(eSuperType);
    if (list == null)
    {
      list = new ArrayList<EClass>();
      result.put(eSuperType, list);
    }

    list.add(eClass);
  }
}

Back to the top