/
UMLUtil.java
14008 lines (11341 loc) · 417 KB
/
UMLUtil.java
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
/*
* Copyright (c) 2005, 2019 IBM Corporation, Embarcadero Technologies, CEA, Christian W. Damus, EclipseSource and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM - initial API and implementation
* Kenn Hussey (Embarcadero Technologies) - 199624, 184249, 204406, 208125, 204200, 213218, 213903, 220669, 208016, 226396, 271470
* Nicolas Rouquette (JPL) - 260120, 313837
* Kenn Hussey - 286329, 313601, 314971, 344907, 236184, 335125, 528925, 529564, 535301, 542789
* Kenn Hussey (CEA) - 327039, 358792, 364419, 366350, 307343, 382637, 273949, 389542, 389495, 316165, 392833, 399544, 322715, 163556, 212765, 397324, 204658, 408612, 411731, 269598, 422000, 416833, 424568, 427167, 418466, 419324, 429994, 433157, 439915, 446388, 454864, 458906, 461374, 463066, 468230, 481712, 491587, 495564, 512439, 512520, 514386, 514624, 517384
* Yann Tanguy (CEA) - 350402
* Christian W. Damus (CEA) - 392833, 251963, 405061, 409396, 176998, 180744, 403374, 416833, 420338, 405065, 431342
* E.D.Willink - 420338, 512439
* Christian W. Damus - 444588, 497359, 501740
* Camille Letavernier - 528925, 529564
* Camille Letavernier (EclipseSource) - 544487, 545578
*/
package org.eclipse.uml2.uml.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.emf.common.CommonPlugin;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.BasicEMap;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.UniqueEList;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EAttribute;
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.EModelElement;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EParameter;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.ETypeParameter;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreSwitch;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.ExtendedMetaData;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.emf.ecore.xmi.impl.EMOFExtendedMetaData;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.uml2.common.util.CacheAdapter;
import org.eclipse.uml2.common.util.UML2Util;
import org.eclipse.uml2.types.TypesFactory;
import org.eclipse.uml2.types.TypesPackage;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.AttributeOwner;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.ClassifierTemplateParameter;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.DirectedRelationship;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.EnumerationLiteral;
import org.eclipse.uml2.uml.Extension;
import org.eclipse.uml2.uml.Feature;
import org.eclipse.uml2.uml.Generalization;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.InterfaceRealization;
import org.eclipse.uml2.uml.LiteralInteger;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.LiteralUnlimitedNatural;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.MultiplicityElement;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.OpaqueBehavior;
import org.eclipse.uml2.uml.OpaqueExpression;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.OperationOwner;
import org.eclipse.uml2.uml.PackageMerge;
import org.eclipse.uml2.uml.Parameter;
import org.eclipse.uml2.uml.ParameterDirectionKind;
import org.eclipse.uml2.uml.ParameterableElement;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.RedefinableElement;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.StringExpression;
import org.eclipse.uml2.uml.StructuralFeature;
import org.eclipse.uml2.uml.TemplateBinding;
import org.eclipse.uml2.uml.TemplateParameter;
import org.eclipse.uml2.uml.TemplateParameterSubstitution;
import org.eclipse.uml2.uml.TemplateSignature;
import org.eclipse.uml2.uml.TemplateableElement;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.TypedElement;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.UMLPlugin;
import org.eclipse.uml2.uml.ValueSpecification;
import org.eclipse.uml2.uml.VisibilityKind;
import org.eclipse.uml2.uml.resource.CMOF202UMLResource;
import org.eclipse.uml2.uml.resource.CMOF242UMLResource;
import org.eclipse.uml2.uml.resource.CMOF2UMLExtendedMetaData;
import org.eclipse.uml2.uml.resource.CMOF2UMLResource;
import org.eclipse.uml2.uml.resource.UML212UMLExtendedMetaData;
import org.eclipse.uml2.uml.resource.UML212UMLResource;
import org.eclipse.uml2.uml.resource.UML22UMLExtendedMetaData;
import org.eclipse.uml2.uml.resource.UML22UMLResource;
import org.eclipse.uml2.uml.resource.UML302UMLExtendedMetaData;
import org.eclipse.uml2.uml.resource.UML302UMLResource;
import org.eclipse.uml2.uml.resource.UML402UMLExtendedMetaData;
import org.eclipse.uml2.uml.resource.UML402UMLResource;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.eclipse.uml2.uml.resource.XMI212UMLResource;
import org.eclipse.uml2.uml.resource.XMI222UMLResource;
import org.eclipse.uml2.uml.resource.XMI242UMLResource;
import org.eclipse.uml2.uml.resource.XMI2UMLExtendedMetaData;
import org.eclipse.uml2.uml.resource.XMI2UMLResource;
/**
* Utilities for working with UML elements and resources.
*
* @since 2.0
*/
public class UMLUtil
extends UML2Util {
/**
* The ProfileApplicationHelper can be overridden to change the way packages
* are traversed when looking for profile applications relevant to an
* element in a given package.
*
* @since 3.0
*/
public static class ProfileApplicationHelper {
/**
* @deprecated Since the 5.1 release, direct reliance on this shared
* helper instance is deprecated. Instead, use the
* {@link #getInstance(Notifier)} method to obtain a helper
* specific to the particular model context
*/
@Deprecated
public static final ProfileApplicationHelper INSTANCE = createProfileApplicationHelper();
/**
* Obtains the most appropriate {@link ProfileApplicationHelper}
* instance for managing profile applications in the given
* {@code context}. This helper should always be preferred over using
* the default {@code INSTANCE}.
*
* @param context
* a {@link ResourceSet}, {@link Resource}, or
* {@link EObject} in the context of which the caller is
* working with profile applications
*
* @return the specific helper associated with a {@code ResourceSet} if
* there is such reachable from the {@code context}, or else the
* default {@link #INSTANCE}. The result is never {@code null}
*
* @since 5.1
*/
public static ProfileApplicationHelper getInstance(Notifier context) {
if (context == null) {
return INSTANCE;
} else if (context instanceof ResourceSet) {
return getInstance((ResourceSet) context);
} else if (context instanceof Resource) {
return getInstance(((Resource) context).getResourceSet());
} else if (context instanceof EObject) {
return getInstance(((EObject) context).eResource());
} else {
return INSTANCE;
}
}
/**
* Assigns a particular helper {@code instance} for management of
* profile applications in the scope of a resource set. This supersedes
* the default {@link #INSTANCE} for all profile-application operations
* on packages in the resource set.
*
* @param resourceSet
* a resource set. Must not be {@code null}
* @param instance
* a profile application helper to assign. Must not be
* {@code null}. May be the default {@link #INSTANCE}
*
* @return the helper that was previously in effect for the resource
* set, which could be the default {@link #INSTANCE} if no
* specific helper was assigned
*
* @since 5.1
*/
public static ProfileApplicationHelper setInstance(
ResourceSet resourceSet, ProfileApplicationHelper instance) {
ProfileApplicationHelper result = HelperLink.link(resourceSet,
ProfileApplicationHelper.class, instance);
// If no helper was associated, the shared instance was implied
return (result == null)
? INSTANCE
: result;
}
private static ProfileApplicationHelper getInstance(
ResourceSet resourceSet) {
HelperLink<ProfileApplicationHelper> link = (resourceSet == null)
? null
: HelperLink.getInstance(resourceSet,
ProfileApplicationHelper.class);
return (link != null)
? link.getHelper()
: INSTANCE;
}
private static ProfileApplicationHelper createProfileApplicationHelper() {
ProfileApplicationHelper profileApplicationHelper = UML2Util
.loadClassFromSystemProperty("org.eclipse.uml2.uml.util.UMLUtil$ProfileApplicationHelper.INSTANCE"); //$NON-NLS-1$
if (profileApplicationHelper != null) {
return profileApplicationHelper;
}
return new ProfileApplicationHelper();
}
public Iterable<org.eclipse.uml2.uml.Package> getOtherApplyingPackages(
org.eclipse.uml2.uml.Package package_) {
return package_.allOwningPackages();
}
}
/**
* The StereotypeApplicationHelper can be overridden to change the default
* location of applied stereotypes.
* Typically, stereotype applications are placed in the same location as
* the element to which the stereotype is applied, however, stereotype
* applications may be placed in other resources.
*
* @since 3.0
*/
public static class StereotypeApplicationHelper {
/**
* @deprecated Since the 5.1 release, direct reliance on this shared
* helper instance is deprecated. Instead, use the
* {@link #getInstance(Notifier)} method to obtain a helper
* specific to the particular model context
*/
@Deprecated
public static final StereotypeApplicationHelper INSTANCE = createStereotypeApplicationHelper();
/**
* Obtains the most appropriate {@link StereotypeApplicationHelper}
* instance for managing stereotype applications in the given
* {@code context}. This helper should always be preferred over using
* the default {@code INSTANCE}.
*
* @param context
* a {@link ResourceSet}, {@link Resource}, or
* {@link EObject} in the context of which the caller is
* working with stereotype applications
*
* @return the specific helper associated with a {@code ResourceSet} if
* there is such reachable from the {@code context}, or else the
* default {@link #INSTANCE}. The result is never {@code null}
*
* @since 5.1
*/
public static StereotypeApplicationHelper getInstance(
Notifier context) {
if (context == null) {
return INSTANCE;
} else if (context instanceof ResourceSet) {
return getInstance((ResourceSet) context);
} else if (context instanceof Resource) {
return getInstance(((Resource) context).getResourceSet());
} else if (context instanceof EObject) {
return getInstance(((EObject) context).eResource());
} else {
return INSTANCE;
}
}
/**
* Assigns a particular helper {@code instance} for management of
* stereotype applications in the scope of a resource set. This
* supersedes the default {@link #INSTANCE} for all stereotype
* operations on model elements in the resource set.
*
* @param resourceSet
* a resource set. Must not be {@code null}
* @param instance
* a stereotype application helper to assign. Must not be
* {@code null}. May be the default {@link #INSTANCE}
*
* @return the helper that was previously in effect for the resource
* set, which could be the default {@link #INSTANCE} if no
* specific helper was assigned
*
* @since 5.1
*/
public static StereotypeApplicationHelper setInstance(
ResourceSet resourceSet, StereotypeApplicationHelper instance) {
StereotypeApplicationHelper result = HelperLink.link(resourceSet,
StereotypeApplicationHelper.class, instance);
// If no helper was associated, the shared instance was implied
return (result == null)
? INSTANCE
: result;
}
private static StereotypeApplicationHelper getInstance(
ResourceSet resourceSet) {
HelperLink<StereotypeApplicationHelper> link = (resourceSet == null)
? null
: HelperLink.getInstance(resourceSet,
StereotypeApplicationHelper.class);
return (link != null)
? link.getHelper()
: INSTANCE;
}
private static StereotypeApplicationHelper createStereotypeApplicationHelper() {
StereotypeApplicationHelper stereotypeApplicationHelper = UML2Util
.loadClassFromSystemProperty(
"org.eclipse.uml2.uml.util.UMLUtil$StereotypeApplicationHelper.INSTANCE"); //$NON-NLS-1$
if (stereotypeApplicationHelper != null) {
return stereotypeApplicationHelper;
}
return new StereotypeApplicationHelper();
}
protected EList<EObject> getContainmentList(Element element,
EClass definition, Stereotype stereotype) {
return getContainmentList(element, definition);
}
@Deprecated
protected EList<EObject> getContainmentList(Element element,
EClass definition) {
Resource eResource = element.eResource();
if (eResource != null) {
return eResource.getContents();
}
return null;
}
public boolean addToContainmentList(Element element,
EObject stereotypeApplication, Stereotype stereotype) {
boolean result = false;
EList<EObject> containmentList = getContainmentList(element,
stereotypeApplication.eClass(), stereotype);
if (containmentList != null) {
result = containmentList.add(stereotypeApplication);
}
return result;
}
@Deprecated
public boolean addToContainmentList(Element element,
EObject stereotypeApplication) {
return addToContainmentList(element, stereotypeApplication, null);
}
public boolean removeFromContainmentList(Element element,
EObject stereotypeApplication, Stereotype stereotype) {
boolean result = false;
EList<EObject> containmentList = getContainmentList(element,
stereotypeApplication.eClass(), stereotype);
if (containmentList != null) {
result = containmentList.remove(stereotypeApplication);
}
return result;
}
@Deprecated
public boolean removeFromContainmentList(Element element,
EObject stereotypeApplication) {
return removeFromContainmentList(element, stereotypeApplication,
null);
}
public EObject applyStereotype(Element element, EClass definition,
Stereotype stereotype) {
EObject stereotypeApplication = EcoreUtil.create(definition);
CacheAdapter.getInstance().adapt(stereotypeApplication);
addToContainmentList(element, stereotypeApplication, stereotype);
basicSetBaseElement(stereotypeApplication, definition, element);
return stereotypeApplication;
}
@Deprecated
public EObject applyStereotype(Element element, EClass definition) {
return applyStereotype(element, definition, null);
}
}
private static final class HelperLink<T>
extends AdapterImpl {
private final Class<T> helperType;
private final T helper;
private HelperLink(Class<T> helperType, T helper) {
this.helper = helper;
this.helperType = helperType;
}
static <T> HelperLink<T> getInstance(Notifier notifier,
Class<T> helperType) {
return getInstance(notifier.eAdapters(), helperType);
}
@SuppressWarnings("unchecked")
static <T> HelperLink<T> getInstance(List<Adapter> adapters,
Class<T> helperType) {
// Our isAdapterForType() implementation ensures that the generic
// signature matches
return (HelperLink<T>) EcoreUtil.getAdapter(adapters, helperType);
}
static <T> T link(Notifier notifier, Class<T> helperType, T helper) {
T result = helper;
EList<Adapter> adapters = notifier.eAdapters();
HelperLink<T> link = getInstance(adapters, helperType);
if (link == null) {
adapters.add(new HelperLink<T>(helperType, helper));
} else {
result = link.getHelper();
if (helper == null) {
adapters.remove(link);
} else if (result != helper) {
int index = adapters.indexOf(link);
adapters.set(index, new HelperLink<T>(helperType, helper));
}
}
return result;
}
@Override
public boolean isAdapterForType(Object type) {
return (type == helperType) || (type == helper);
}
T getHelper() {
return helper;
}
}
/**
* A qualified text provider that uses names of named elements as qualified
* text segments and :: as a separator.
*/
public static class QualifiedTextProvider
extends UML2Util.QualifiedTextProvider {
/**
* The default instance.
*/
public static final QualifiedTextProvider DEFAULT = new QualifiedTextProvider();
@Override
public String getText(EObject eObject) {
return eObject instanceof NamedElement
? ((NamedElement) eObject).getName()
: super.getText(eObject);
}
@Override
public String getSeparator() {
return NamedElement.SEPARATOR;
}
}
/**
* A copier that merges the contents of packages involved in package merge
* relationships based on the rules defined in the UML specification.
*/
public static class PackageMerger
extends EcoreUtil.Copier {
private static final long serialVersionUID = 1L;
protected class BodyMatcher
extends EStructuralFeatureMatcher {
protected BodyMatcher(Comment comment) {
super(comment, UMLPackage.Literals.COMMENT__BODY);
}
}
protected class NameMatcher
extends EStructuralFeatureMatcher {
protected NameMatcher(NamedElement namedElement) {
super(namedElement, UMLPackage.Literals.NAMED_ELEMENT__NAME);
}
}
protected class ImplicitAssociationNameMatcher
extends EClassMatcher {
private final String name;
ImplicitAssociationNameMatcher(Association association) {
super(association);
name = getName(association);
}
@Override
public boolean matches(EObject otherEObject) {
boolean result = super.matches(otherEObject);
if (result) {
Association other = (Association) otherEObject;
result = safeEquals(name, getName(other));
}
return result;
}
protected String getName(Association association) {
String result = association.getName();
if (result == null) {
StringBuilder buf = new StringBuilder();
buf.append('A');
for (Property end : association.getMemberEnds()) {
buf.append('_');
buf.append(getName(end));
}
result = buf.toString();
}
return result;
}
protected String getName(Property end) {
String result = end.getName();
if ((result == null) && (end.getType() != null)) {
result = end.getType().getName();
if (!UML2Util.isEmpty(result)) {
result = String.format(
"%s%s", //$NON-NLS-1$
Character.toLowerCase(result.charAt(0)),
result.substring(1));
}
}
return result;
}
}
protected class KeyMatcher
extends EClassMatcher {
protected KeyMatcher(BasicEMap.Entry<String, String> entry) {
super((EObject) entry);
}
@SuppressWarnings("unchecked")
@Override
public boolean matches(EObject otherEObject) {
return super.matches(otherEObject)
&& safeEquals(
((BasicEMap.Entry<String, String>) eObject).getKey(),
((BasicEMap.Entry<String, String>) otherEObject)
.getKey());
}
}
protected class ResultingQNameMatcher
extends EClassMatcher {
protected ResultingQNameMatcher(EObject eObject) {
super(eObject);
}
@Override
public boolean matches(EObject otherEObject) {
return super.matches(otherEObject)
&& safeEquals(getResultingQName(eObject),
getResultingQName(otherEObject));
}
}
protected class StringValueMatcher
extends EClassMatcher {
protected StringValueMatcher(ValueSpecification valueSpecification) {
super(valueSpecification);
}
@Override
public boolean matches(EObject otherEObject) {
return super.matches(otherEObject)
&& safeEquals(((ValueSpecification) eObject).stringValue(),
((ValueSpecification) otherEObject).stringValue());
}
}
protected class SourceMatcher
extends EStructuralFeatureMatcher {
protected SourceMatcher(EAnnotation eAnnotation) {
super(eAnnotation, EcorePackage.Literals.EANNOTATION__SOURCE);
}
}
protected class TypeMatcher
extends NameMatcher {
protected TypeMatcher(TypedElement typedElement) {
super(typedElement);
}
@Override
public boolean matches(EObject otherEObject) {
if (super.matches(otherEObject)) {
Type type = ((TypedElement) eObject).getType();
Type otherType = ((TypedElement) otherEObject).getType();
return (type == null && otherType == null)
|| new ResultingQNameMatcher(type).matches(otherType)
|| (type instanceof Classifier
&& otherType instanceof Classifier && (findEObject(
((Classifier) type).allParents(),
new ResultingQNameMatcher(otherType)) != null || findEObject(
((Classifier) otherType).allParents(),
new ResultingQNameMatcher(type)) != null));
}
return false;
}
}
private static final boolean DEBUG = false;
/**
* The option for handling cases where the staticity of merged
* properties does not match. Supported choices are
* <code>OPTION__IGNORE</code> and <code>OPTION__REPORT</code>.
*/
public static final String OPTION__DIFFERENT_PROPERTY_STATICITY = "DIFFERENT_PROPERTY_STATICITY"; //$NON-NLS-1$
/**
* The option for handling cases where the uniqueness of merged
* properties does not match. Supported choices are
* <code>OPTION__IGNORE</code> and <code>OPTION__REPORT</code>.
*/
public static final String OPTION__DIFFERENT_PROPERTY_UNIQUENESS = "DIFFERENT_PROPERTY_UNIQUENESS"; //$NON-NLS-1$
/**
* The option for handling cases where a redundant generalization is
* detected. Supported choices are <code>OPTION__IGNORE</code>,
* <code>OPTION__REPORT</code>, and <code>OPTION__DISCARD</code>.
*/
public static final String OPTION__REDUNDANT_GENERALIZATIONS = "REDUNDANT_GENERALIZATIONS"; //$NON-NLS-1$
/**
* The option for handling cases where an implicit redefinition is
* detected. Supported choices are <code>OPTION__IGNORE</code>,
* <code>OPTION__REPORT</code>, and <code>OPTION__PROCESS</code>.
*/
public static final String OPTION__IMPLICIT_REDEFINITIONS = "IMPLICIT_REDEFINITIONS"; //$NON-NLS-1$
/**
* The option for handling cases where an invalid redefinition is
* detected. Supported choices are <code>OPTION__IGNORE</code>,
* <code>OPTION__REPORT</code>, <code>OPTION__DISCARD</code>, and
* <code>OPTION__PROCESS</code>.
*/
public static final String OPTION__INVALID_REDEFINITIONS = "INVALID_REDEFINITIONS"; //$NON-NLS-1$
/**
* The option for handling cases where an invalid subset is detected.
* Supported choices are <code>OPTION__IGNORE</code>,
* <code>OPTION__REPORT</code>, <code>OPTION__DISCARD</code>, and
* <code>OPTION__PROCESS</code>.
*/
public static final String OPTION__INVALID_SUBSETS = "INVALID_SUBSETS"; //$NON-NLS-1$
/**
* The option for handling cases where an empty union is detected.
* Supported choices are <code>OPTION__IGNORE</code>,
* <code>OPTION__REPORT</code>, and <code>OPTION__PROCESS</code>.
*/
public static final String OPTION__EMPTY_UNIONS = "EMPTY_UNIONS"; //$NON-NLS-1$
/**
* The option for handling cases where an association specialization is
* missing. Supported choices are <code>OPTION__IGNORE</code>,
* <code>OPTION__REPORT</code>, and <code>OPTION__PROCESS</code>.
*/
public static final String OPTION__ASSOCIATION_SPECIALIZATIONS = "ASSOCIATION_SPECIALIZATIONS"; //$NON-NLS-1$
/**
* The option for handling cases where capability information is
* missing. Supported choices are <code>OPTION__IGNORE</code>,
* <code>OPTION__REPORT</code>, and <code>OPTION__PROCESS</code>.
*/
public static final String OPTION__CAPABILITIES = "CAPABILITIES"; //$NON-NLS-1$
/**
* The option for handling cases where a classifier with an empty
* qualified name is detected. Supported choices are
* <code>OPTION__IGNORE</code> and <code>OPTION__REPORT</code>.
*/
public static final String OPTION__EMPTY_QUALIFIED_NAMES = "EMPTY_QUALIFIED_NAMES"; //$NON-NLS-1$
/**
* The option for handling cases where an indistinguishable classifier
* is detected. Supported choices are <code>OPTION__IGNORE</code> and
* <code>OPTION__REPORT</code>.
*/
public static final String OPTION__INDISTINGUISHABLE_CLASSIFIERS = "INDISTINGUISHABLE_CLASSIFIERS"; //$NON-NLS-1$
/**
* The option for handling cases where an attribute transformation
* should be performed. Supported choices are
* <code>OPTION__IGNORE</code>, <code>OPTION__REPORT</code>, and
* <code>OPTION__PROCESS</code>.
*/
public static final String OPTION__ATTRIBUTE_TRANSFORMATIONS = "ATTRIBUTE_TRANSFORMATIONS"; //$NON-NLS-1$
private static final int DIAGNOSTIC_CODE_OFFSET = 1000;
/**
* The diagnostic code for cases where the staticity of merged
* properties does not match.
*/
public static final int DIFFERENT_PROPERTY_STATICITY = DIAGNOSTIC_CODE_OFFSET + 1;
/**
* The diagnostic code for cases where the uniqueness of merged
* properties does not match.
*/
public static final int DIFFERENT_PROPERTY_UNIQUENESS = DIAGNOSTIC_CODE_OFFSET + 2;
/**
* The diagnostic code for cases where a redundant generalization is
* detected.
*/
public static final int REDUNDANT_GENERALIZATION = DIAGNOSTIC_CODE_OFFSET + 3;
/**
* The diagnostic code for cases where an implicit redefinition is
* detected.
*/
public static final int IMPLICIT_REDEFINITION = DIAGNOSTIC_CODE_OFFSET + 4;
/**
* The diagnostic code for cases where an invalid redefinition is
* detected.
*/
public static final int INVALID_REDEFINITION = DIAGNOSTIC_CODE_OFFSET + 5;
/**
* The diagnostic code for cases where an invalid subset is detected.
*/
public static final int INVALID_SUBSET = DIAGNOSTIC_CODE_OFFSET + 6;
/**
* The diagnostic code for cases where an empty union is detected.
*/
public static final int EMPTY_UNION = DIAGNOSTIC_CODE_OFFSET + 7;
/**
* The diagnostic code for cases where an association specialization is
* missing.
*/
public static final int ASSOCIATION_SPECIALIZATION = DIAGNOSTIC_CODE_OFFSET + 8;
/**
* The diagnostic code for cases where capability information is
* missing.
*/
public static final int CAPABILITY = DIAGNOSTIC_CODE_OFFSET + 9;
/**
* The diagnostic code for cases where the qualified named of a
* classifier in a receiving or merged package is empty.
*/
public static final int EMPTY_QUALIFIED_NAME = DIAGNOSTIC_CODE_OFFSET + 10;
/**
* The diagnostic code for cases where a classifier in a receiving or
* merged package is indistinguishable.
*/
public static final int INDISTINGUISHABLE_CLASSIFIER = DIAGNOSTIC_CODE_OFFSET + 11;
/**
* The diagnostic code for cases where an attribute transformation
* should be performed.
*/
public static final int ATTRIBUTE_TRANSFORMATION = DIAGNOSTIC_CODE_OFFSET + 12;
protected TemplateableElement receivingElement = null;
protected Collection<? extends TemplateableElement> mergedElements = null;
/**
* @deprecated Use the {@link #receivingElement}, instead.
*/
@Deprecated
protected org.eclipse.uml2.uml.Package receivingPackage = null;
/**
* @deprecated Use the {@link #mergedElements}, instead
*/
@Deprecated
protected Collection<org.eclipse.uml2.uml.Package> mergedPackages = null;
protected final Map<EObject, List<EObject>> resultingToMergedEObjectMap = new LinkedHashMap<EObject, List<EObject>>();
protected Map<String, String> options = null;
protected DiagnosticChain diagnostics = null;
protected Map<Object, Object> context = null;
protected <EO extends EObject> List<EO> getMatchCandidates(EO eObject) {
Element baseElement = getBaseElement(eObject);
if (baseElement == null) {
EStructuralFeature eContainingFeature = eObject
.eContainingFeature();
if (eContainingFeature.isMany()) {
@SuppressWarnings("unchecked")
List<EO> values = (List<EO>) get(eObject.eContainer())
.eGet(eContainingFeature);
return values;
} else {
@SuppressWarnings("unchecked")
EO value = (EO) get(eObject.eContainer()).eGet(
eContainingFeature);
return Collections.<EO> singletonList(value);
}
} else {
@SuppressWarnings("unchecked")
List<EO> stereotypeApplications = (List<EO>) ((Element) get(baseElement))
.getStereotypeApplications();
return stereotypeApplications;
}
}
protected <EO extends EObject> List<EO> getMergedEObjects(
EO resultingEObject) {
@SuppressWarnings("unchecked")
List<EO> mergedEObjects = (List<EO>) resultingToMergedEObjectMap
.get(resultingEObject);
return mergedEObjects == null
? Collections.<EO> singletonList(resultingEObject)
: (List<EO>) mergedEObjects;
}
protected <EO extends EObject> EO getPreviouslyMergedEObject(
EO resultingEObject) {
List<EO> mergedEObjects = getMergedEObjects(resultingEObject);
return mergedEObjects.get(mergedEObjects.size() - 1);
}
protected String getResultingQName(EObject eObject) {
StringBuffer resultingQName = appendResultingQName(
new StringBuffer(), eObject, QualifiedTextProvider.DEFAULT);
return resultingQName.toString();
}
private StringBuffer appendResultingQName(StringBuffer resultingQName,
EObject eObject, QualifiedTextProvider qualifiedTextProvider) {
eObject = mergedElements.contains(eObject)
? receivingElement
: eObject;
EObject eContainer = eObject.eContainer();
if (eContainer != null) {
appendResultingQName(resultingQName, eContainer,
qualifiedTextProvider);
if (resultingQName.length() > 0) {
resultingQName.append(qualifiedTextProvider.getSeparator());
}
}
return getQualifiedTextSegment(eObject, qualifiedTextProvider,
resultingQName);
}
protected void mergeAssociation_IsDerived(
Association receivingAssociation, Association mergedAssociation) {
boolean receivingIsDerived = receivingAssociation.isDerived();
boolean resultingIsDerived = receivingIsDerived
|| mergedAssociation.isDerived();
if (receivingIsDerived != resultingIsDerived) {
if (options != null
&& OPTION__REPORT.equals(options
.get(OPTION__ATTRIBUTE_TRANSFORMATIONS))) {
if (diagnostics != null) {
diagnostics
.add(new BasicDiagnostic(
Diagnostic.WARNING,
UMLValidator.DIAGNOSTIC_SOURCE,
ATTRIBUTE_TRANSFORMATION,
UMLPlugin.INSTANCE
.getString(
"_UI_PackageMerger_ReportAttributeTransformation_diagnostic", //$NON-NLS-1$
getMessageSubstitutions(
context,
UMLPackage.Literals.ASSOCIATION__IS_DERIVED,
receivingAssociation,
mergedAssociation)),
new Object[]{receivingAssociation}));
}
} else {
if (diagnostics != null) {
diagnostics
.add(new BasicDiagnostic(
Diagnostic.INFO,
UMLValidator.DIAGNOSTIC_SOURCE,
ATTRIBUTE_TRANSFORMATION,
UMLPlugin.INSTANCE
.getString(
"_UI_PackageMerger_ProcessAttributeTransformation_diagnostic", //$NON-NLS-1$
getMessageSubstitutions(
context,
UMLPackage.Literals.ASSOCIATION__IS_DERIVED,
receivingAssociation,
mergedAssociation)),