Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b0ebdee7994a6a7daf64f18028056d83ac384f55 (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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
/*****************************************************************************
 * Copyright (c) 2010, 2016 CEA LIST, Christian W. Damus, 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - filter out EObjects that are Resources (CDO)
 *  Christian W. Damus (CEA) - Support read-only state at object level (CDO)
 *  Christian W. Damus (CEA) - bugs 323802, 429826, 408491, 432813, 422257
 *  Christian W. Damus - bugs 469188, 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.emf.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
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.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.papyrus.infra.core.resource.IReadOnlyHandler;
import org.eclipse.papyrus.infra.core.resource.IReadOnlyHandler2;
import org.eclipse.papyrus.infra.core.resource.ReadOnlyAxis;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.emf.Activator;
import org.eclipse.papyrus.infra.tools.util.PlatformHelper;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

/**
 * A Helper class for manipulating EMF Objects
 *
 * @author Camille Letavernier
 */
// TODO : Check implementations. Most of them are old and don't always match the specification
public class EMFHelper {

	/**
	 * Returns the EClass corresponding to the given nsUri and className
	 *
	 * @param nsUri
	 *            The NSURI of the EClass' EPackage
	 * @param className
	 *            The EClass' name
	 * @return
	 * 		The EClass instance, or null if the EClass couldn't be found
	 */
	public static EClass getEClass(final String nsUri, final String className) {
		EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(nsUri);
		if (ePackage == null) {
			Activator.log.warn("Cannot find an EPackage matching the nsURI " + nsUri); //$NON-NLS-1$
			return null;
		}
		return getEClass(ePackage, className);
	}

	/**
	 * Return the EClass corresponding to the given EPackage and className
	 *
	 * @param metamodel
	 *            The EClass' EPackage
	 * @param className
	 *            The EClass' name
	 * @return
	 * 		The EClass instance, or null if the EClass couldn't be found
	 */
	public static EClass getEClass(final EPackage metamodel, final String className) {
		EClassifier classifier = metamodel.getEClassifier(className);
		if (classifier == null) {
			Activator.log.warn("Classifier " + className + " not found in metamodel " + metamodel.getName() + " (" + metamodel.getNsURI() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		}
		if (classifier instanceof EClass) {
			return (EClass) classifier;
		} else {
			Activator.log.warn("Classifier " + className + " in " + metamodel.getName() + " (" + metamodel.getNsURI() + ") is not an EClass"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		}

		return null;
	}

	/**
	 * Tests if an Object is an instance of the given EClass
	 *
	 * @param element
	 *            The EObject to test
	 * @param className
	 *            The name of the EClass
	 * @param metamodel
	 *            The EPackage owning the EClass
	 * @return
	 * 		True if the EObject is an instance of the EClass, or of one of the EClass' subtypes
	 */
	public static boolean isInstance(final EObject element, final String className, final EPackage metamodel) {

		EClassifier theClass = metamodel.getEClassifier(className);

		if (theClass == null) {
			String message = String.format("Class %s not found in  metamodel: %s (%s)", className, metamodel.getName(), metamodel.getNsURI());//$NON-NLS-1$
			Activator.log.warn(message);
			return false;
		}

		return theClass.isInstance(element);
	}


	/**
	 * Tests if an Object is an instance of the given EClass
	 *
	 * @param element
	 *            The EObject to test
	 * @param className
	 *            The name of the EClass
	 * @param metamodel
	 *            The URI of the EPackage owning the EClass
	 * @return
	 * 		True if the EObject is an instance of the EClass, or of one of the EClass' subtypes
	 */
	public static boolean isInstance(EObject selectedItem, String className, String nsUri) {
		EClass actualEClass = selectedItem.eClass();

		// Exact match
		if (isExactMatch(actualEClass, className, nsUri)) {
			return true;
		}

		List<EClass> allSuperTypes = actualEClass.getEAllSuperTypes();
		for (EClass eClass : allSuperTypes) {
			if (isExactMatch(eClass, className, nsUri)) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Tests whether the given EClass has the given className and belongs to the EPackage represented by nsUri
	 *
	 * @param eClass
	 *            The EClass to test
	 * @param className
	 *
	 * @param nsUri
	 * @return
	 * 		True if the EClass' name is className and the EClass' EPackage's nsURI is nsUri
	 */
	private static boolean isExactMatch(EClass eClass, String className, String nsUri) {
		return className.equals(eClass.getName()) && nsUri.equals(eClass.getEPackage().getNsURI());
	}

	/**
	 * Tests if the given eClass is a Subclass of fromClass
	 * Also returns true when eClass == fromClass
	 *
	 * @param eClass
	 * @param fromClass
	 * @return
	 * 		true if eClass is a subclass of fromClass
	 */
	public static boolean isSubclass(final EClass eClass, final EClass fromClass) {
		// Everything is an EObject
		if (eClass != null && fromClass == EcorePackage.eINSTANCE.getEObject()) {
			return true;
		}

		if (eClass == fromClass) {
			return true;
		}

		List<EClass> superTypes = eClass.getEAllSuperTypes();
		if (superTypes.contains(fromClass)) {
			return true;
		}

		return false;
	}

	/**
	 * Returns the EObject corresponding to the input object
	 * Tests if the input is an EObject, or if it is Adaptable
	 * to an EObject
	 *
	 * @param source
	 * @return An EObject corresponding to the input source, or null
	 *         if the EObject could not be resolved
	 */
	public static EObject getEObject(final Object source) {

		// General case
		if (isEMFModelElement(source)) {
			return (EObject) source;
		}

		// Try to get an intrinsic adapter
		if (source instanceof IAdaptable) {
			EObject eObject = ((IAdaptable) source).getAdapter(EObject.class);
			if (eObject == null) { // EMF Facet 0.1
				eObject = ((IAdaptable) source).getAdapter(EReference.class);
			}

			if (eObject != null) {
				return asEMFModelElement(eObject); // in case the adapter is a CDOResource
			}
		}

		// External adapter (last ditch case)
		if (source != null) {
			return asEMFModelElement(Platform.getAdapterManager().getAdapter(source, EObject.class));
		}

		return null;
	}

	/**
	 * Queries whether an {@code object} is an EMF model element, an instance of
	 * some {@link EClass} from an EMF model. This isn't as simple as checking
	 * whether the object is an {@link EObject} because there are edge cases
	 * where objects are {@code EObject}s but shouldn't be treated as
	 * "model content". But, a minimum requirement is that the {@code object} is
	 * an {@link EObject}.
	 *
	 * @param object
	 *            an object
	 * @return whether it is "model content"
	 *
	 * @see EMFHelper#asEMFModelElement(Object)
	 */
	public static boolean isEMFModelElement(Object object) {
		return (object instanceof EObject) && !(object instanceof Resource);
	}

	/**
	 * Casts an {@code object} as an EMF model element, if appropriate.
	 *
	 * @param object
	 *            an object
	 * @return the object as an EMF model element, or {@code null} if it is not
	 *         an EMF model element
	 *
	 * @see #isEMFModelElement(Object)
	 */
	public static EObject asEMFModelElement(Object object) {
		return isEMFModelElement(object) ? (EObject) object : null;
	}

	/**
	 * Retrieve the EditingDomain for the given source object. The object is first
	 * resolved to an EObject through #getEObject when possible.
	 *
	 * @param source
	 * @return
	 * 		The source object's editing domain, or null if it couldn't be found
	 */
	public static EditingDomain resolveEditingDomain(final Object source) {
		return resolveEditingDomain(getEObject(source));
	}

	/**
	 * Retrieve the EditingDomain for the given source EObject
	 *
	 * @param source
	 * @return
	 * 		The source eObject's editing domain, or null if it couldn't be found
	 */
	public static EditingDomain resolveEditingDomain(final EObject source) {
		EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor(source);
		if (domain == null) {
			try {
				domain = ServiceUtils.getInstance().getTransactionalEditingDomain(null);
			} catch (ServiceException e) {
				// Ignore: We cannot find the domain
			}
		}
		return domain;
	}

	/**
	 * Return the eClassifier' qualified name. The qualified name is obtained by the concatenation
	 * of its package hierarchy with the class name, separated by the given separator
	 *
	 * @param eClassifier
	 * @param separator
	 *            The separator used between each package name
	 * @return
	 * 		The EClassifier' qualified name
	 */
	public static String getQualifiedName(final EClassifier eClassifier, final String separator) {
		return getQualifiedName(eClassifier.getEPackage(), separator) + separator + eClassifier.getName();
	}

	/**
	 * Return the ePackage's qualified name. The qualified name is obtained by the concatenation
	 * of its superPackage hierarchy with the ePackage name, separated by the given separator
	 *
	 * @param ePackage
	 * @param separator
	 *            The separator used between each package name
	 * @return
	 * 		The EPackage's qualified name
	 */
	public static String getQualifiedName(final EPackage ePackage, final String separator) {
		if (ePackage.getESuperPackage() == null) {
			return ePackage.getName();
		}
		return getQualifiedName(ePackage.getESuperPackage(), separator) + separator + ePackage.getName();
	}


	/**
	 * Loads and returns the first EObject at the given URI.
	 * The EObject is loaded in the given resourceSet.
	 *
	 * @param resourceSet
	 *            The ResourceSet in which the model will be loaded
	 * @param uri
	 *            The URI describing the location of the model to load
	 * @return
	 * 		The first EObject located at the given URI
	 * @throws IOException
	 *             When the URI cannot be loaded
	 */
	public static EObject loadEMFModel(ResourceSet resourceSet, final URI uri) throws IOException {
		assert resourceSet != null : "null resourceSet"; //$NON-NLS-1$
		if (resourceSet == null) {
			Activator.log.warn("Created a new resourceSet to load an EMF model in " + Activator.log.getCallerMethod()); //$NON-NLS-1$
			resourceSet = new ResourceSetImpl();
		}

		try {
			Resource resource = resourceSet.getResource(uri, true);
			if (resource != null) {
				if (!resource.getContents().isEmpty()) {
					return resource.getContents().get(0);
				}
			}
		} catch (Exception ex) {
			IOException exception = new IOException(ex.toString());
			exception.initCause(ex);
			throw exception;
		}

		return null;
	}

	/**
	 * Completely unloads a resource set so that it and all the models it contained may be reclaimed by the
	 * Java garbage collector. This includes, at least:
	 * <ul>
	 * <li>unloading all resources in the set, which converts all model elements to proxies and removes all adapters from them</li>
	 * <li>removing all resources from the set</li>
	 * <li>removing all adapters from all resources</li>
	 * <li>removing all adapters from the resource set</li>
	 * </ul>
	 *
	 * @param resourceSet
	 *            the resource set to purge
	 */
	public static void unload(ResourceSet resourceSet) {
		List<Resource> resources = ImmutableList.copyOf(resourceSet.getResources());
		resourceSet.getResources().clear();
		for (Resource next : resources) {
			next.unload();
			next.eAdapters().clear();
		}
		resourceSet.eAdapters().clear();

		// Clear the package registry (it may contain dynamic profile EPackages that we don't
		// want to leak in BasicExtendedMetaData instances attached to static EPackages)
		// Works around EMF bug 433108
		EPackage.Registry packageRegistry = resourceSet.getPackageRegistry();
		if (packageRegistry != null) {
			packageRegistry.clear();
		}
	}

	/**
	 * Return the root package containing the given package, or the package
	 * itself if it is already the root
	 *
	 * @param ePackage
	 * @return
	 * 		The Root package
	 */
	public static EPackage getRootPackage(final EPackage ePackage) {
		if (ePackage == null) {
			return null;
		}

		if (ePackage.getESuperPackage() == null) {
			return ePackage;
		}
		return getRootPackage(ePackage.getESuperPackage());
	}

	/**
	 * Gets the object of a given {@code type} containing an {@code object}, or the
	 * {@code object} itself if it is of that {@code type}.
	 * 
	 * @param object
	 *            the object for which to search for a container
	 * @param type
	 *            the type of container to find
	 * 
	 * @return the container of the requested {@code type}, or {@code null} if none
	 */
	public static <T extends EObject> T getContainer(EObject object, EClass type) {
		T result = null;

		for (EObject next = object; (next != null) && (result == null); next = next.eContainer()) {
			if (type.isInstance(next)) {
				@SuppressWarnings("unchecked")
				T nextAsT = (T) next;
				result = nextAsT;
			}
		}

		return result;
	}

	/**
	 * Gets the object of a given {@code type} containing an {@code object}, or the
	 * {@code object} itself if it is of that {@code type}.
	 * 
	 * @param object
	 *            the object for which to search for a container
	 * @param type
	 *            the type of container to find
	 * 
	 * @return the container of the requested {@code type}, or {@code null} if none
	 */
	public static <T extends EObject> T getContainer(EObject object, Class<T> type) {
		T result = null;

		for (EObject next = object; (next != null) && (result == null); next = next.eContainer()) {
			if (type.isInstance(next)) {
				result = type.cast(next);
			}
		}

		return result;
	}

	/**
	 * Return the list of EClasses that are subtypes
	 * of the given EClass
	 *
	 * @param type
	 * @param concreteClassesOnly
	 *            If true, only Concrete EClasses will be returned. Abstract and Interface EClasses will be filtered
	 * @return
	 * 		The list of EClasses implementing or extending the given EClass
	 */
	public static List<EClass> getSubclassesOf(final EClass type, final boolean concreteClassesOnly) {
		Set<EClass> result = new LinkedHashSet<EClass>();
		if (!concreteClassesOnly || (!type.isAbstract() && !type.isInterface())) {
			result.add(type);
		}

		EPackage ePackage = getRootPackage(type.getEPackage());
		getSubclassesOf(type, ePackage, result, concreteClassesOnly);
		return new LinkedList<EClass>(result);
	}

	/**
	 * Return the list of EClasses that are sub types
	 * of the given EClass
	 *
	 * @param type
	 * @param concreteClassesOnly
	 *            If true, only Concrete EClasses will be returned. Abstract and Interface EClasses will be filtered
	 * @param packagesToBrowse
	 *            The EPackages in which the EClasses should be retrieved
	 * @return
	 * 		The list of EClasses implementing or extending the given EClass
	 */
	public static List<EClass> getSubclassesOf(final EClass type, final boolean concreteClassesOnly, Collection<EPackage> packagesToBrowse) {
		Set<EClass> result = new LinkedHashSet<EClass>();
		if (!concreteClassesOnly || (!type.isAbstract() && !type.isInterface())) {
			result.add(type);
		}

		for (EPackage ePackage : packagesToBrowse) {
			getSubclassesOf(type, ePackage, result, concreteClassesOnly);
		}

		return new LinkedList<EClass>(result);
	}

	/**
	 * Return the list of EClasses that are sub types of the given EClass
	 *
	 * @param type
	 * @param concreteClassesOnly
	 *            If true, only Concrete EClasses will be returned. Abstract and Interface EClasses will be filtered
	 * @param browseAllRegisteredPackages
	 *            If true, all registered EPackages will be navigated to retrieve the matching EClasses. Otherwise,
	 *            only the current EPackage will be used.
	 * @return
	 * 		The list of EClasses implementing or extending the given EClass
	 */
	public static List<EClass> getSubclassesOf(final EClass type, final boolean concreteClassesOnly, final boolean browseAllRegisteredPackages) {
		// If the current package is a dynamic package, it may not be registered (?). Add it directly
		EPackage currentPackage = getRootPackage(type.getEPackage());

		Set<EPackage> allPackages = new LinkedHashSet<EPackage>();
		allPackages.add(currentPackage);

		if (browseAllRegisteredPackages) {
			// FIXME // WARNING: This loop will load all EPackages. The first call is expensive.
			Set<String> allUris = new HashSet<String>(EPackage.Registry.INSTANCE.keySet());

			for (String nsURI : allUris) {
				allPackages.add(EPackage.Registry.INSTANCE.getEPackage(nsURI));
			}
		}

		return getSubclassesOf(type, concreteClassesOnly, allPackages);
	}

	private static void getSubclassesOf(final EClass type, final EPackage fromPackage, final Set<EClass> result, final boolean concreteClassesOnly) {
		for (EClassifier classifier : fromPackage.getEClassifiers()) {
			if (classifier instanceof EClass) {
				EClass eClass = (EClass) classifier;
				if (eClass.getEAllSuperTypes().contains(type)) {
					if (!concreteClassesOnly || (!eClass.isAbstract() && !eClass.isInterface())) {
						result.add(eClass);
					}
				}
			}
		}

		for (EPackage subPackage : fromPackage.getESubpackages()) {
			getSubclassesOf(type, subPackage, result, concreteClassesOnly);
		}
	}

	/**
	 * Tests if an EObject is read only on any {@linkplain ReadOnlyAxis axis}.
	 * Delegates to the EObject's editing domain if it can be found
	 *
	 * @param eObject
	 * @return
	 * 		True if the EObject is read only on any axis
	 * @see #isReadOnly(Set, EObject, EditingDomain)
	 */
	public static boolean isReadOnly(final EObject eObject) {
		return isReadOnly(ReadOnlyAxis.anyAxis(), eObject);
	}

	/**
	 * Tests if an EObject is read only on any of the specified {@code axes}.
	 * Delegates to the EObject's editing domain if it can be found
	 *
	 * @param axes
	 *            a set if orthogonal axes of read-only-ness to consider. May be empty, but that would not be especially useful
	 * @param eObject
	 * @return
	 * 		True if the EObject is read only on any of the given {@code axes}
	 */
	public static boolean isReadOnly(Set<ReadOnlyAxis> axes, final EObject eObject) {
		EditingDomain domain = resolveEditingDomain(eObject);
		return isReadOnly(axes, eObject, domain);
	}

	/**
	 * Tests if an EObject is read only on any {@linkplain ReadOnlyAxis axis}.
	 * Delegates to the given editing domain if it isn't null
	 *
	 * @param eObject
	 * @param domain
	 * @return
	 * 		True if the EObject is read only on any axis
	 */
	public static boolean isReadOnly(final EObject eObject, final EditingDomain domain) {
		return isReadOnly(ReadOnlyAxis.anyAxis(), eObject, domain);
	}

	/**
	 * Tests if an EObject is read only on any of the specified {@code axes}.
	 * Delegates to the given editing domain if it isn't null
	 *
	 * @param axes
	 *            a set if orthogonal axes of read-only-ness to consider. May be empty, but that would not be especially useful
	 * @param eObject
	 *
	 * @param domain
	 * @return
	 * 		True if the EObject is read only
	 */
	public static boolean isReadOnly(Set<ReadOnlyAxis> axes, final EObject eObject, final EditingDomain domain) {
		if (domain != null) {
			Object handler = PlatformHelper.getAdapter(domain, IReadOnlyHandler.class);
			if (handler instanceof IReadOnlyHandler2) {
				return ((IReadOnlyHandler2) handler).isReadOnly(axes, eObject).get();
			} else if (handler instanceof IReadOnlyHandler) {
				// these handlers only deal with permission-based read-only-ness
				return axes.contains(ReadOnlyAxis.PERMISSION) && ((IReadOnlyHandler) handler).isReadOnly(eObject).get();
			}

			if (eObject.eResource() != null) {
				return domain.isReadOnly(eObject.eResource());
			}
		}
		return false;
	}

	/**
	 * Tests if the Resource is read only on any {@linkplain ReadOnlyAxis axis}.
	 * Delegates to the given editing domain if it isn't null
	 *
	 * @param resource
	 * @param domain
	 * @return
	 * 		True if the Resource is read only on any axis
	 */
	public static boolean isReadOnly(final Resource resource, final EditingDomain domain) {
		return isReadOnly(ReadOnlyAxis.anyAxis(), resource, domain);
	}

	/**
	 * Tests if the Resource is read only on any of the given {@code axes}.
	 * Delegates to the given editing domain if it isn't null
	 *
	 * @param axes
	 *            a set if orthogonal axes of read-only-ness to consider. May be empty, but that would not be especially useful
	 * @param resource
	 * @param domain
	 * @return
	 * 		True if the Resource is read only on any of the given {@code axes}
	 */
	public static boolean isReadOnly(Set<ReadOnlyAxis> axes, final Resource resource, final EditingDomain domain) {
		if (resource == null) {
			return false;
		}

		if (domain != null && resource.getURI() != null) {
			Object handler = PlatformHelper.getAdapter(domain, IReadOnlyHandler.class);
			if (handler instanceof IReadOnlyHandler2) {
				return ((IReadOnlyHandler2) handler).anyReadOnly(axes, new URI[] { resource.getURI() }).get();
			} else if (handler instanceof IReadOnlyHandler) {
				// these handlers only deal with permission-based read-only-ness
				return axes.contains(ReadOnlyAxis.PERMISSION) && ((IReadOnlyHandler) handler).anyReadOnly(new URI[] { resource.getURI() }).get();
			}
			return domain.isReadOnly(resource);
		}

		// no editing domain : use file system attribute
		ResourceSet resourceSet = resource.getResourceSet();

		if (resourceSet == null) {
			return false;
		}

		Map<String, ?> attributes = resourceSet.getURIConverter().getAttributes(resource.getURI(), null);
		Boolean readOnly = (Boolean) attributes.get(URIConverter.ATTRIBUTE_READ_ONLY);

		return readOnly == null ? false : readOnly;
	}

	/**
	 * Tests if an object that is read only could possibly be made writable by some means (file system attributes, team provider hook, database
	 * permissions, etc.)
	 *
	 * @param eObject
	 *            an object that is assumed to be read-only
	 * @param domain
	 *            the editing domain context of the {@link eObject}
	 * @return
	 * 		whether the {@code eObject} could be made writable
	 */
	public static boolean canMakeWritable(final EObject eObject, final EditingDomain domain) {
		return canMakeWritable(ReadOnlyAxis.anyAxis(), eObject, domain);
	}

	/**
	 * Tests if an object that is read only could possibly be made writable according to any of
	 * the specified {@code axes} of read-only-ness.
	 *
	 * @param axes
	 *            a set if orthogonal axes of read-only-ness to consider. May be empty, but that would not be especially useful
	 * @param eObject
	 *            an object that is assumed to be read-only
	 * @param domain
	 *            the editing domain context of the {@link eObject}
	 * @return
	 * 		whether the {@code eObject} could be made writable
	 */
	public static boolean canMakeWritable(Set<ReadOnlyAxis> axes, final EObject eObject, final EditingDomain domain) {
		if (domain != null) {
			Object handler = PlatformHelper.getAdapter(domain, IReadOnlyHandler.class);
			if (handler instanceof IReadOnlyHandler2) {
				return ((IReadOnlyHandler2) handler).canMakeWritable(axes, eObject).or(false);
			}
		}
		return false;
	}

	/**
	 * Tests if a resource that is read only could possibly be made writable by some means (file system attributes, team provider hook, database
	 * permissions, etc.)
	 *
	 * @param resource
	 *            a resource that is assumed to be read-only
	 * @param domain
	 *            the editing domain context of the {@link resource}
	 * @return
	 * 		whether the {@code resource} could be made writable
	 */
	public static boolean canMakeWritable(final Resource resource, final EditingDomain domain) {
		return canMakeWritable(ReadOnlyAxis.anyAxis(), resource, domain);
	}

	/**
	 * Tests if a resource that is read only could possibly be made writable according to any of
	 * the specified {@code axes} of read-only-ness.
	 *
	 * @param axes
	 *            a set if orthogonal axes of read-only-ness to consider. May be empty, but that would not be especially useful
	 * @param resource
	 *            a resource that is assumed to be read-only
	 * @param domain
	 *            the editing domain context of the {@link resource}
	 * @return
	 * 		whether the {@code resource} could be made writable
	 */
	public static boolean canMakeWritable(Set<ReadOnlyAxis> axes, final Resource resource, final EditingDomain domain) {
		if (domain != null) {
			Object handler = PlatformHelper.getAdapter(domain, IReadOnlyHandler.class);
			if (handler instanceof IReadOnlyHandler2) {
				return ((IReadOnlyHandler2) handler).canMakeWritable(axes, new URI[] { resource.getURI() }).or(false);
			}
		}
		return false;
	}

	/**
	 * Tests if the given EStructuralFeature is required (ie. should always
	 * have a value)
	 *
	 * A feature is required if at least of one the following conditions if
	 * true :
	 *
	 * - It has a defaultValue
	 * - Its lowerBound is at least 1
	 * - It is an enumeration (Enumerations always have a default value)
	 * - It is a Java primitive type, and is not marked as Unsettable
	 *
	 * @param feature
	 *            the feature to test
	 * @return
	 * 		true if the feature is required, false otherwise
	 */
	public static boolean isRequired(final EStructuralFeature feature) {
		EClassifier eType = feature.getEType();
		if (eType == null) {
			return false;
		}

		// EEnums are always required, as an EEnum always has a default value
		if (eType instanceof EEnum) {
			return true;
		}

		// At least one value means it is required
		if (feature.getLowerBound() >= 1) {
			return true;
		}

		// Java primitive types cannot have a null value
		// if the feature is not specifically marked as unsettable, then it is required
		if (eType.getInstanceClass() != null && eType.getInstanceClass().isPrimitive() && !feature.isUnsettable()) {
			return true;
		}

		// If there is a default value, there is always a value
		if (feature.getDefaultValueLiteral() != null) {
			return true;
		}

		return false; // The property if not required
	}

	/**
	 * Returns all objects of type T contained in the resource
	 *
	 * @param resource
	 * @param type
	 * @return
	 */
	public static <T> Set<T> allInstances(final Resource resource, Class<T> type) {
		TreeIterator<EObject> iterator = resource.getAllContents();
		Set<T> result = new LinkedHashSet<T>();

		while (iterator.hasNext()) {
			EObject element = iterator.next();
			if (type.isInstance(element)) {
				result.add(type.cast(element));
			}
		}

		return result;
	}

	/**
	 * Returns all the EPackages and nested EPackages contained in this resource
	 *
	 * @param resource
	 * @return
	 */
	public static Set<EPackage> getAllEPackages(final Resource resource) {
		Set<EPackage> result = new LinkedHashSet<EPackage>();

		for (EObject rootElement : resource.getContents()) {
			if (rootElement instanceof EPackage) {
				result.add((EPackage) rootElement);
				result.addAll(getAllNestedPackages((EPackage) rootElement));
			}
		}

		return result;
	}

	/**
	 * Returns all packages nested in the given EPackage (recursively). Does not
	 * include the base EPackage.
	 *
	 * @param basePackage
	 * @return
	 */
	public static Set<EPackage> getAllNestedPackages(EPackage basePackage) {
		Set<EPackage> result = new LinkedHashSet<EPackage>();

		for (EPackage nestedPackage : basePackage.getESubpackages()) {
			result.add(nestedPackage);
			result.addAll(getAllNestedPackages(nestedPackage));
		}

		return result;
	}

	/**
	 *
	 * @param resource
	 *            a resource
	 *
	 * @return
	 * 		the list of the metamodels known by the resource
	 */
	public static Set<EPackage> getMetamodels(final Resource resource) {
		Set<EPackage> metamodels = new HashSet<EPackage>();
		if (resource != null) {
			final List<EObject> contents = new ArrayList<EObject>(resource.getContents());
			for (final EObject current : contents) {
				metamodels.add(current.eClass().getEPackage());
			}
		}
		return metamodels;
	}

	/**
	 *
	 * Returns the XMI ID of the given {@link EObject} or <code>null</code> if it cannot be resolved.
	 *
	 * @param object
	 *            Object which we seek the XMI ID of.
	 * @return <code>object</code>'s XMI ID, <code>null</code> if not applicable.
	 */
	public static final String getXMIID(final EObject object) {
		String objectID = null;
		if (object != null && object.eResource() instanceof XMIResource) {
			objectID = ((XMIResource) object.eResource()).getID(object);
		}
		return objectID;
	}



	/**
	 * Gets the usages.
	 *
	 * @param source
	 *            the source
	 *
	 * @return the usages or null if there is no usages
	 */
	public static Collection<Setting> getUsages(EObject source) {
		// the functional code is defined in core because we need it in infra.core
		// but infra.core can't depend on infra.emf (circular dependency)
		return org.eclipse.papyrus.infra.core.utils.EMFHelper.getUsages(source);
	}


	/**
	 * <pre>
	 * Test if the used element is referenced by other elements than the known
	 * referencer (except its container). It ignores references from an other meta-model.
	 * </pre>
	 *
	 * @param usedObject
	 *            the used object
	 * @param knownReferencer
	 *            the known referencer
	 * @return true if the known referencer is the only referencer.
	 */
	public static boolean isOnlyUsage(EObject usedObject, EObject knownReferencer) {
		boolean isUsed = false;
		EPackage mmPackage = usedObject.eClass().getEPackage();

		// Retrieve the list of elements referencing the usedObject.
		Set<EObject> crossReferences = new HashSet<EObject>();
		for (Setting setting : getUsages(usedObject)) {
			EObject eObj = setting.getEObject();
			if (eObj.eClass().getEPackage().equals(mmPackage)) {
				crossReferences.add(eObj);
			}
		}

		// Remove the container of used object.
		crossReferences.remove(usedObject.eContainer());
		// Remove the knownReferencer from the list of references.
		crossReferences.remove(knownReferencer);

		// If no referencer remains in the list, the known element is the only
		// usage.
		if (crossReferences.isEmpty()) {
			isUsed = true;
		}

		return isUsed;
	}

	/**
	 *
	 * @param superType
	 *            an eclassifier
	 * @param subType
	 *            another eClassifier
	 * @return
	 *         <code>true</code> if the 2nd {@link EClassifier} is a subtype of the first one
	 */
	public static boolean isSuperType(final EClassifier superType, final EClassifier subType) {
		if (superType == subType) {
			return true;
		}

		if (superType instanceof EClass && subType instanceof EClass) {
			// special case because isSuperTypeOf doesn't handle it
			if (superType == EcorePackage.eINSTANCE.getEObject()) {
				return true;
			}

			EClass superTypeEClass = (EClass) superType;
			EClass subTypeEClass = (EClass) subType;
			return superTypeEClass.isSuperTypeOf(subTypeEClass);
		}

		// manage EDtataType
		if (superType == EcorePackage.eINSTANCE.getEDataType() && subType instanceof EDataType) {
			return true;
		}

		return false;
	}

	/**
	 * Computes the path from the root EObject to the given element, as a List of EObjects
	 *
	 * @param element
	 * @return
	 */
	public static List<EObject> getContainmentPath(EObject element) {
		List<EObject> result;
		if (element.eContainer() == null) {
			result = new LinkedList<EObject>();
			result.add(element);
			return result;
		} else {
			result = getContainmentPath(element.eContainer());
			result.add(element);
		}
		return result;
	}

	/**
	 * Returns the given element, reloaded into the resource set of the context element,
	 * or the source element itself if not possible.
	 *
	 * Use this method for e.g. loading an element from a shared resource set into another resource set
	 * (Apply a registered profile/library, drop an element from the project explorer, ...)
	 *
	 * @param element
	 * @param contextElement
	 * @return
	 */
	public static <T extends EObject> T reloadIntoContext(T element, EObject contextElement) {
		ResourceSet sourceResourceSet = getResourceSet(element);
		ResourceSet loadingContext = getResourceSet(contextElement);

		if (sourceResourceSet == loadingContext || loadingContext == null) {
			return element;
		}

		URI sourceURI = EcoreUtil.getURI(element);
		EObject result = loadingContext.getEObject(sourceURI, true);

		return (T) result;
	}

	/**
	 * Returns the resourceSet owning this eObject, or null if it is detached
	 *
	 * @param eObject
	 */
	public static ResourceSet getResourceSet(EObject eObject) {
		Resource resource = (eObject == null) ? null : eObject.eResource();
		return (resource == null) ? null : resource.getResourceSet();
	}

	/**
	 * Best-effort loads a resource, returning the first root element of the requested {@code type}. Unlike the {@link #loadChecked(ResourceSet, URI, Class) loadChecked} method, this will never throw an exception.
	 *
	 * @param rset
	 *            a resource set in which to load the resource
	 * @param uri
	 *            the URI of the resource to load
	 * @param type
	 *            the type of root element to retrieve
	 *
	 * @return the requested root element, or {@code null} if the resource does not contain such an element or could not be loaded
	 *
	 * @see #loadChecked(ResourceSet, URI, Class)
	 */
	public static <T extends EObject> T load(ResourceSet rset, URI uri, Class<T> type) {
		T result = null;

		try {
			result = loadChecked(rset, uri, type);
		} catch (Exception e) {
			Activator.log.error("Exception in loading resource " + uri, e); //$NON-NLS-1$

			// Maybe it was partially loaded? If so, try again
			Resource res = rset.getResource(uri, false);
			if ((res != null) && res.isLoaded()) {
				result = Iterables.getFirst(Iterables.filter(res.getContents(), type), null);
			}
		}

		return result;
	}

	/**
	 * Best-effort loads a resource, returning the first root element of the requested {@code type}.
	 *
	 * @param rset
	 *            a resource set in which to load the resource
	 * @param uri
	 *            the URI of the resource to load
	 * @param type
	 *            the type of root element to retrieve
	 *
	 * @return the requested root element, or {@code null} if the resource does not contain such an element or could not be loaded
	 * @throws IOException
	 *             on an I/O problem in loading the resource
	 * @throw RuntimeException on any other unforeseen (usually programming error) problem
	 *
	 * @see #load(ResourceSet, URI, Class)
	 */
	public static <T extends EObject> T loadChecked(ResourceSet rset, URI uri, Class<T> type) throws IOException {
		try {
			return Iterables.getFirst(Iterables.filter(rset.getResource(uri, true).getContents(), type), null);
		} catch (WrappedException e) {
			if (e.exception() instanceof IOException) {
				throw (IOException) e.exception();
			} else if (e.exception() instanceof RuntimeException) {
				throw (RuntimeException) e.exception();
			} else {
				throw e;
			}
		}
	}
}

Back to the top