/
ElementOperations.java
1801 lines (1487 loc) · 54.8 KB
/
ElementOperations.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, 2018 IBM Corporation, Embarcadero Technologies, CEA, 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 Technologie) - 247980
* Kenn Hussey - 323181, 418466, 528925, 535301
* Christian W. Damus (CEA) - 300957
* Kenn Hussey (CEA) - 485756, 517384
* Camille Letavernier - 528925
*/
package org.eclipse.uml2.uml.internal.operations;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.eclipse.emf.common.util.BasicDiagnostic;
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.TreeIterator;
import org.eclipse.emf.common.util.UniqueEList;
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.EFactory;
import org.eclipse.emf.ecore.EModelElement;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.Association;
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.Model;
import org.eclipse.uml2.uml.Relationship;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.ProfileApplication;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.UMLPlugin;
import org.eclipse.uml2.uml.util.UMLUtil;
import org.eclipse.uml2.uml.util.UMLValidator;
/**
* <!-- begin-user-doc -->
* A static utility class that provides operations related to '<em><b>Element</b></em>' model objects.
* <!-- end-user-doc -->
*
* <p>
* The following operations are supported:
* <ul>
* <li>{@link org.eclipse.uml2.uml.Element#validateNotOwnSelf(org.eclipse.emf.common.util.DiagnosticChain, java.util.Map) <em>Validate Not Own Self</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#validateHasOwner(org.eclipse.emf.common.util.DiagnosticChain, java.util.Map) <em>Validate Has Owner</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#destroy() <em>Destroy</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#hasKeyword(java.lang.String) <em>Has Keyword</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getKeywords() <em>Get Keywords</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#addKeyword(java.lang.String) <em>Add Keyword</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#removeKeyword(java.lang.String) <em>Remove Keyword</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getNearestPackage() <em>Get Nearest Package</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getModel() <em>Get Model</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#isStereotypeApplicable(org.eclipse.uml2.uml.Stereotype) <em>Is Stereotype Applicable</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#isStereotypeRequired(org.eclipse.uml2.uml.Stereotype) <em>Is Stereotype Required</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#isStereotypeApplied(org.eclipse.uml2.uml.Stereotype) <em>Is Stereotype Applied</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#applyStereotype(org.eclipse.uml2.uml.Stereotype) <em>Apply Stereotype</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#unapplyStereotype(org.eclipse.uml2.uml.Stereotype) <em>Unapply Stereotype</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getApplicableStereotypes() <em>Get Applicable Stereotypes</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getApplicableStereotype(java.lang.String) <em>Get Applicable Stereotype</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getStereotypeApplications() <em>Get Stereotype Applications</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getStereotypeApplication(org.eclipse.uml2.uml.Stereotype) <em>Get Stereotype Application</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getRequiredStereotypes() <em>Get Required Stereotypes</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getRequiredStereotype(java.lang.String) <em>Get Required Stereotype</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getAppliedStereotypes() <em>Get Applied Stereotypes</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getAppliedStereotype(java.lang.String) <em>Get Applied Stereotype</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getAppliedSubstereotypes(org.eclipse.uml2.uml.Stereotype) <em>Get Applied Substereotypes</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getAppliedSubstereotype(org.eclipse.uml2.uml.Stereotype, java.lang.String) <em>Get Applied Substereotype</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#hasValue(org.eclipse.uml2.uml.Stereotype, java.lang.String) <em>Has Value</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getValue(org.eclipse.uml2.uml.Stereotype, java.lang.String) <em>Get Value</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#setValue(org.eclipse.uml2.uml.Stereotype, java.lang.String, java.lang.Object) <em>Set Value</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#createEAnnotation(java.lang.String) <em>Create EAnnotation</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getRelationships() <em>Get Relationships</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getRelationships(org.eclipse.emf.ecore.EClass) <em>Get Relationships</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getSourceDirectedRelationships() <em>Get Source Directed Relationships</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getSourceDirectedRelationships(org.eclipse.emf.ecore.EClass) <em>Get Source Directed Relationships</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getTargetDirectedRelationships() <em>Get Target Directed Relationships</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#getTargetDirectedRelationships(org.eclipse.emf.ecore.EClass) <em>Get Target Directed Relationships</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#allOwnedElements() <em>All Owned Elements</em>}</li>
* <li>{@link org.eclipse.uml2.uml.Element#mustBeOwned() <em>Must Be Owned</em>}</li>
* </ul>
* </p>
*
* @generated not
*/
public class ElementOperations
extends UMLUtil {
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
protected ElementOperations() {
super();
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* An element may not directly or indirectly own itself.
* not allOwnedElements()->includes(self)
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param diagnostics The chain of diagnostics to which problems are to be appended.
* @param context The cache of context-specific information.
* <!-- end-model-doc -->
* @generated NOT
*/
public static boolean validateNotOwnSelf(Element element,
DiagnosticChain diagnostics, Map<Object, Object> context) {
boolean result = true;
if (EcoreUtil.isAncestor(element, element.getOwner())) {
result = false;
if (diagnostics != null) {
diagnostics.add(new BasicDiagnostic(Diagnostic.WARNING,
UMLValidator.DIAGNOSTIC_SOURCE,
UMLValidator.ELEMENT__NOT_OWN_SELF,
UMLPlugin.INSTANCE.getString(
"_UI_Element_NotOwnSelf_diagnostic", //$NON-NLS-1$
getMessageSubstitutions(context, element)),
new Object[]{element}));
}
}
return result;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Elements that must be owned must have an owner.
* mustBeOwned() implies owner->notEmpty()
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param diagnostics The chain of diagnostics to which problems are to be appended.
* @param context The cache of context-specific information.
* <!-- end-model-doc -->
* @generated NOT
*/
public static boolean validateHasOwner(Element element,
DiagnosticChain diagnostics, Map<Object, Object> context) {
boolean result = true;
if (element.mustBeOwned() && element.getOwner() == null) {
result = false;
if (diagnostics != null) {
diagnostics.add(new BasicDiagnostic(Diagnostic.WARNING,
UMLValidator.DIAGNOSTIC_SOURCE,
UMLValidator.ELEMENT__HAS_OWNER,
UMLPlugin.INSTANCE.getString(
"_UI_Element_HasOwner_diagnostic", //$NON-NLS-1$
getMessageSubstitutions(context, element)),
new Object[]{element}));
}
}
return result;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the stereotype applications for this element.
* @param element The receiving '<em><b>Element</b></em>' model object.
* <!-- end-model-doc -->
* @generated NOT
*/
public static EList<EObject> getStereotypeApplications(Element element) {
EList<EObject> stereotypeApplications = new UniqueEList.FastCompare<EObject>();
for (EStructuralFeature.Setting setting : getNonNavigableInverseReferences(
element)) {
if (setting.getEStructuralFeature().getName()
.startsWith(Extension.METACLASS_ROLE_PREFIX)) {
EObject eObject = setting.getEObject();
if (getStereotype(eObject) != null) {
stereotypeApplications.add(eObject);
}
}
}
return ECollections.unmodifiableEList(stereotypeApplications);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the application of the specified stereotype for this element, or null if no such stereotype application exists.
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param stereotype The stereotype for which to retrieve an application.
* <!-- end-model-doc -->
* @generated NOT
*/
public static EObject getStereotypeApplication(Element element,
Stereotype stereotype) {
for (EObject stereotypeApplication : element
.getStereotypeApplications()) {
if (getStereotype(stereotypeApplication) == stereotype) {
return stereotypeApplication;
}
}
return null;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the stereotypes that are required for this element.
* @param element The receiving '<em><b>Element</b></em>' model object.
* <!-- end-model-doc -->
* @generated NOT
*/
public static EList<Stereotype> getRequiredStereotypes(Element element) {
org.eclipse.uml2.uml.Package package_ = element.getNearestPackage();
if (package_ != null) {
EList<Stereotype> requiredStereotypes = new UniqueEList.FastCompare<Stereotype>();
for (ProfileApplication profileApplication : package_
.getAllProfileApplications()) {
Profile appliedProfile = profileApplication.getAppliedProfile();
if (appliedProfile != null) {
for (Stereotype stereotype : appliedProfile
.allApplicableStereotypes()) {
ENamedElement appliedDefinition = profileApplication
.getAppliedDefinition(stereotype);
if (appliedDefinition instanceof EClass
&& !((EClass) appliedDefinition).isAbstract()) {
Extension extension = getExtension(element,
stereotype);
if (extension != null && extension.isRequired()) {
requiredStereotypes.add(stereotype);
}
}
}
}
}
return ECollections.unmodifiableEList(requiredStereotypes);
}
return ECollections.emptyEList();
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the stereotype with the specified qualified name that is required for this element, or null if no such stereotype is required.
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param qualifiedName The qualified name of the required stereotype to retrieve.
* <!-- end-model-doc -->
* @generated NOT
*/
public static Stereotype getRequiredStereotype(Element element,
String qualifiedName) {
for (Stereotype requiredStereotype : element.getRequiredStereotypes()) {
if (safeEquals(requiredStereotype.getQualifiedName(),
qualifiedName)) {
return requiredStereotype;
}
}
return null;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the stereotypes that are applied to this element.
* @param element The receiving '<em><b>Element</b></em>' model object.
* <!-- end-model-doc -->
* @generated NOT
*/
public static EList<Stereotype> getAppliedStereotypes(Element element) {
EList<Stereotype> appliedStereotypes = new UniqueEList.FastCompare<Stereotype>();
for (EStructuralFeature.Setting setting : getNonNavigableInverseReferences(
element)) {
if (setting.getEStructuralFeature().getName()
.startsWith(Extension.METACLASS_ROLE_PREFIX)) {
Stereotype stereotype = getStereotype(setting.getEObject());
if (stereotype != null) {
appliedStereotypes.add(stereotype);
}
}
}
return ECollections.unmodifiableEList(appliedStereotypes);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the stereotype with the specified qualified name that is applied to this element, or null if no such stereotype is applied.
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param qualifiedName The qualified name of the applied stereotype to retrieve.
* <!-- end-model-doc -->
* @generated NOT
*/
public static Stereotype getAppliedStereotype(Element element,
String qualifiedName) {
for (Stereotype appliedStereotype : element.getAppliedStereotypes()) {
if (safeEquals(appliedStereotype.getQualifiedName(),
qualifiedName)) {
return appliedStereotype;
}
}
return null;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the substereotypes of the specified stereotype that are applied to this element.
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param stereotype The superstereotype of the applied substereotypes to retrieve.
* <!-- end-model-doc -->
* @generated NOT
*/
public static EList<Stereotype> getAppliedSubstereotypes(Element element,
Stereotype stereotype) {
EList<Stereotype> appliedSubstereotypes = new UniqueEList.FastCompare<Stereotype>();
for (Stereotype appliedStereotype : element.getAppliedStereotypes()) {
if (appliedStereotype.allParents().contains(stereotype)) {
appliedSubstereotypes.add(appliedStereotype);
}
}
return ECollections.unmodifiableEList(appliedSubstereotypes);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the substereotype of the specified stereotype with the specified qualified name that is applied to this element, or null if no such stereotype is applied.
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param stereotype The superstereotype of the applied substereotype to retrieve.
* @param qualifiedName The qualified name of the applied substereotype to retrieve.
* <!-- end-model-doc -->
* @generated NOT
*/
public static Stereotype getAppliedSubstereotype(Element element,
Stereotype stereotype, String qualifiedName) {
for (Stereotype appliedSubstereotype : element
.getAppliedSubstereotypes(stereotype)) {
if (safeEquals(appliedSubstereotype.getQualifiedName(),
qualifiedName)) {
return appliedSubstereotype;
}
}
return null;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Determines whether this element has a (non-default) value for the property with the specified name in the specified stereotype.
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param stereotype The stereotype for which to test the property.
* @param propertyName The name of the property in question.
* <!-- end-model-doc -->
* @generated NOT
*/
public static boolean hasValue(Element element, Stereotype stereotype,
String propertyName) {
EObject eObject = element.getStereotypeApplication(stereotype);
if (eObject != null && !isEmpty(propertyName)) {
EClass eClass = eObject.eClass();
String[] segments = propertyName.split(NamedElement.SEPARATOR);
for (int i = 0, length = segments.length; i < length; i++) {
String segment = segments[i];
EStructuralFeature eStructuralFeature = null;
int index = -1;
if (segment.indexOf('[') == -1) {
eStructuralFeature = eClass
.getEStructuralFeature(getValidJavaIdentifier(segment));
} else {
eStructuralFeature = eClass
.getEStructuralFeature(getValidJavaIdentifier(
segment.substring(0, segment.indexOf('['))));
try {
index = Integer.parseInt(segment.substring(
segment.indexOf('[') + 1, segment.indexOf(']')));
} catch (Exception e) {
return false;
}
}
if (eStructuralFeature != null) {
if (length > i + 1) {
if (eObject != null) {
EClassifier eType = eStructuralFeature.getEType();
if (eType instanceof EClass) {
eClass = (EClass) eType;
if (eStructuralFeature.isMany()) {
@SuppressWarnings("unchecked")
List<EObject> list = (List<EObject>) eObject
.eGet(eStructuralFeature);
eObject = list.get(index);
} else {
eObject = (EObject) eObject
.eGet(eStructuralFeature);
}
}
} else {
return false;
}
} else {
if (eStructuralFeature.isMany()) {
@SuppressWarnings("unchecked")
List<Object> list = eObject == null
? Collections.emptyList()
: (List<Object>) eObject
.eGet(eStructuralFeature);
return index == -1
? !list.isEmpty()
: !safeEquals(
eStructuralFeature.getDefaultValue(),
list.get(index));
} else {
return eObject == null
? false
: !safeEquals(
eStructuralFeature.getDefaultValue(),
eObject.eGet(eStructuralFeature));
}
}
}
}
}
return false;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the value of the property with the specified name in the specified stereotype for this element.
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param stereotype The stereotype for which to retrieve the value.
* @param propertyName The name of the property whose value to retrieve.
* <!-- end-model-doc -->
* @generated NOT
*/
public static Object getValue(Element element, Stereotype stereotype,
String propertyName) {
if (stereotype == null || !element.isStereotypeApplied(stereotype)) {
throw new IllegalArgumentException(String.valueOf(stereotype));
}
if (isEmpty(propertyName)) {
throw new IllegalArgumentException(String.valueOf(propertyName));
}
EObject eObject = element.getStereotypeApplication(stereotype);
if (eObject == null) {
throw new IllegalArgumentException(String.valueOf(stereotype));
}
EClass eClass = eObject.eClass();
String[] segments = propertyName.split(NamedElement.SEPARATOR);
for (int i = 0, length = segments.length; i < length; i++) {
String segment = segments[i];
EStructuralFeature eStructuralFeature = null;
int index = -1;
if (segment.indexOf('[') == -1) {
eStructuralFeature = eClass
.getEStructuralFeature(getValidJavaIdentifier(segment));
} else {
eStructuralFeature = eClass
.getEStructuralFeature(getValidJavaIdentifier(
segment.substring(0, segment.indexOf('['))));
try {
index = Integer.parseInt(segment.substring(
segment.indexOf('[') + 1, segment.indexOf(']')));
} catch (Exception e) {
throw new IllegalArgumentException(
String.valueOf(propertyName));
}
}
if (eStructuralFeature == null) {
throw new IllegalArgumentException(
String.valueOf(propertyName));
}
EClassifier eType = eStructuralFeature.getEType();
if (i + 1 < length) {
if (eObject == null || !(eType instanceof EClass)) {
throw new IllegalArgumentException(
String.valueOf(propertyName));
}
eClass = (EClass) eType;
if (eStructuralFeature.isMany()) {
@SuppressWarnings("unchecked")
List<EObject> list = (List<EObject>) eObject
.eGet(eStructuralFeature);
eObject = list.get(index);
} else {
eObject = (EObject) eObject.eGet(eStructuralFeature);
}
} else {
Object value = null;
if (eStructuralFeature.isMany()) {
@SuppressWarnings("unchecked")
List<Object> list = eObject == null
? Collections.emptyList()
: (List<Object>) eObject.eGet(eStructuralFeature);
value = index == -1
? list
: list.get(index);
} else {
value = eObject == null
? eStructuralFeature.getDefaultValue()
: eObject.eGet(eStructuralFeature);
}
if (eType instanceof EEnum && value instanceof EEnumLiteral) {
EAnnotation eAnnotation = eType
.getEAnnotation(UML2_UML_PACKAGE_2_0_NS_URI);
if (eAnnotation != null) {
EList<EObject> references = eAnnotation.getReferences();
if (references.size() > 0) {
Object reference = references.get(0);
if (reference instanceof Enumeration) {
value = ((Enumeration) reference)
.getOwnedLiteral(UMLUtil.UML2EcoreConverter
.getOriginalName((EEnumLiteral) value));
}
}
}
}
return value;
}
}
return null;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Sets the value of the property with the specified name in the specified stereotype for this element.
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param stereotype The stereotype for which to set the value.
* @param propertyName The name of the property whose value to set.
* @param newValue The new value for the property.
* <!-- end-model-doc -->
* @generated NOT
*/
public static void setValue(Element element, Stereotype stereotype,
String propertyName, Object newValue) {
if (stereotype == null) {
throw new IllegalArgumentException(String.valueOf(stereotype));
}
if (isEmpty(propertyName)) {
throw new IllegalArgumentException(String.valueOf(propertyName));
}
EObject eObject = element.getStereotypeApplication(stereotype);
if (eObject == null) {
throw new IllegalArgumentException(String.valueOf(stereotype));
}
EClass eClass = eObject.eClass();
String[] segments = propertyName.split(NamedElement.SEPARATOR);
for (int i = 0, length = segments.length; i < length; i++) {
String segment = segments[i];
EStructuralFeature eStructuralFeature = null;
int index = -1;
if (segment.indexOf('[') == -1) {
eStructuralFeature = eClass
.getEStructuralFeature(getValidJavaIdentifier(segment));
} else {
eStructuralFeature = eClass
.getEStructuralFeature(getValidJavaIdentifier(
segment.substring(0, segment.indexOf('['))));
try {
index = Integer.parseInt(segment.substring(
segment.indexOf('[') + 1, segment.indexOf(']')));
} catch (Exception e) {
throw new IllegalArgumentException(
String.valueOf(propertyName));
}
}
if (eStructuralFeature == null) {
throw new IllegalArgumentException(
String.valueOf(propertyName));
}
EClassifier eType = eStructuralFeature.getEType();
if (i + 1 < length) {
if (!(eType instanceof EClass)) {
throw new IllegalArgumentException(
String.valueOf(propertyName));
}
eClass = (EClass) eType;
if (eStructuralFeature.isMany()) {
@SuppressWarnings("unchecked")
List<Object> list = (List<Object>) eObject
.eGet(eStructuralFeature);
int size = list.size();
if (size <= index) {
if (!((EReference) eStructuralFeature)
.isContainment()) {
throw new IllegalArgumentException(
String.valueOf(propertyName));
}
for (int j = size; j <= index; j++) {
list.add(j, EcoreUtil.create(eClass));
}
}
eObject = (EObject) list.get(index);
} else {
Object value = eObject.eGet(eStructuralFeature);
if (value == null) {
if (!((EReference) eStructuralFeature)
.isContainment()) {
throw new IllegalArgumentException(
String.valueOf(propertyName));
}
eObject.eSet(eStructuralFeature,
value = EcoreUtil.create(eClass));
}
eObject = (EObject) value;
}
} else {
if (newValue != null) {
if (eType instanceof EClass) {
EClass eClassType = (EClass) eType;
if (newValue instanceof List<?>) {
@SuppressWarnings("unchecked")
Iterator<Object> j = ((List<Object>) newValue)
.iterator();
while (j.hasNext()) {
if (!eClassType.isInstance(j.next())) {
throw new IllegalArgumentException(
String.valueOf(newValue));
}
}
} else if (!eClassType.isInstance(newValue)) {
throw new IllegalArgumentException(
String.valueOf(newValue));
}
} else if (eType instanceof EDataType) {
EDataType eDataType = (EDataType) eType;
EFactory eFactoryInstance = eDataType.getEPackage()
.getEFactoryInstance();
if (newValue instanceof List<?>) {
@SuppressWarnings("unchecked")
List<Object> newList = new ArrayList<Object>(
(List<Object>) newValue);
if (eDataType instanceof EEnum) {
EEnum eEnum = (EEnum) eDataType;
for (ListIterator<Object> li = newList
.listIterator(); li.hasNext();) {
Object item = li.next();
if (item instanceof EnumerationLiteral) {
li.set(
eEnum
.getEEnumLiteral(
((EnumerationLiteral) item)
.getName())
.getInstance());
}
}
}
for (ListIterator<Object> li = newList
.listIterator(); li.hasNext();) {
Object item = li.next();
if (item instanceof String) {
try {
li.set(
eFactoryInstance.createFromString(
eDataType, (String) item));
} catch (Exception e) {
// ignore
}
}
}
newValue = newList;
} else {
if (eDataType instanceof EEnum) {
EEnum eEnum = (EEnum) eDataType;
if (newValue instanceof EnumerationLiteral) {
newValue = eEnum.getEEnumLiteral(
((EnumerationLiteral) newValue)
.getName())
.getInstance();
}
}
if (newValue instanceof String) {
try {
newValue = eFactoryInstance
.createFromString(eDataType,
(String) newValue);
} catch (Exception e) {
// ignore
}
}
}
}
}
if (newValue == null) {
newValue = eStructuralFeature.getDefaultValue();
}
if (eStructuralFeature.isMany()) {
if (index == -1) {
if (newValue instanceof List<?>) {
eObject.eSet(eStructuralFeature, newValue);
} else {
throw new IllegalArgumentException(
String.valueOf(newValue));
}
} else {
@SuppressWarnings("unchecked")
List<Object> list = (List<Object>) eObject
.eGet(eStructuralFeature);
for (int j = list.size(); j < index; j++) {
list.add(j, eStructuralFeature.getDefaultValue());
}
if (index == list.size()) {
list.add(index, newValue);
} else {
list.set(index, newValue);
}
}
} else {
eObject.eSet(eStructuralFeature, newValue);
}
}
}
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Creates an annotation with the specified source and this element as its model element.
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param source The source for the new annotation.
* <!-- end-model-doc -->
* @generated NOT
*/
public static EAnnotation createEAnnotation(Element element,
String source) {
return createEAnnotation((EModelElement) element, source);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the relationships in which this element is involved.
* @param element The receiving '<em><b>Element</b></em>' model object.
* <!-- end-model-doc -->
* @generated NOT
*/
public static EList<Relationship> getRelationships(Element element) {
return getRelationships(element, UMLPackage.Literals.RELATIONSHIP);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the relationships of the specified type in which this element is involved.
* @param element The receiving '<em><b>Element</b></em>' model object.
* @param eClass The (meta)type of the relationships to retrieve.
* <!-- end-model-doc -->
* @generated NOT
*/
public static EList<Relationship> getRelationships(Element element,
EClass eClass) {
EList<Relationship> relationships = new UniqueEList.FastCompare<Relationship>();
for (EStructuralFeature.Setting setting : getNonNavigableInverseReferences(
element)) {
EObject eObject = setting.getEObject();
if (eClass.isInstance(eObject)) {
relationships.add((Relationship) eObject);
} else if (eObject instanceof Property) {
Association association = ((Property) eObject).getAssociation();
if (eClass.isInstance(association)) {
relationships.add(association);
}
}
}
for (EReference eReference : element.eClass().getEAllReferences()) {
if (!eReference.isDerived() && element.eIsSet(eReference)) {
EClass eReferenceType = eReference.getEReferenceType();
if (eClass.isSuperTypeOf(eReferenceType)) {
if (eReference.isMany()) {
@SuppressWarnings("unchecked")
List<Relationship> values = (List<Relationship>) element
.eGet(eReference);
relationships.addAll(values);
} else {
relationships
.add((Relationship) element.eGet(eReference));
}
} else if (eReferenceType.isSuperTypeOf(eClass)) {
Object value = element.eGet(eReference);
if (eReference.isMany()) {
@SuppressWarnings("unchecked")
Iterator<Object> i = ((List<Object>) value).iterator();
while (i.hasNext()) {
value = i.next();
if (eClass.isInstance(value)) {
relationships.add((Relationship) value);
}
}
} else if (eClass.isInstance(value)) {
relationships.add((Relationship) value);
}
}
}
}
return ECollections.unmodifiableEList(relationships);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the directed relationships for which this element is a source.
* @param element The receiving '<em><b>Element</b></em>' model object.
* <!-- end-model-doc -->
* @generated NOT
*/
public static EList<DirectedRelationship> getSourceDirectedRelationships(
Element element) {
return getSourceDirectedRelationships(element,
UMLPackage.Literals.DIRECTED_RELATIONSHIP);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-model-doc -->
* Retrieves the directed relationships of the specified type for which this element is a source.
* @param element The receiving '<em><b>Element</b></em>' model object.