forked from umple/umple
/
UmpleInternalParser_CodeClass.ump
3771 lines (3351 loc) · 130 KB
/
UmpleInternalParser_CodeClass.ump
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: All contributers to the Umple Project
This file is made available subject to the open source license found at:
http://umple.org/license
This file analyze class related umple meta model tokens such class declarations,
associations and attributes and populates the umple meta model.
Please refer to UmpleInternalParser.ump for more details.
*/
namespace cruise.umple.compiler;
class UmpleInternalParser
{
depend java.util.regex.*;
internal AssociationVariable[] unlinkedAssociationVariables;
internal Association[] unlinkedAssociations;
internal Map<Position,String> positionToClassNameReference = new HashMap<Position, String>();
internal Map<UmpleClassifier,List<Token>> unanalyzedInvariants = new HashMap<UmpleClassifier,List<Token>>();
internal Map<UmpleClassifier,List<String>> unlinkedExtends = new HashMap<UmpleClassifier,List<String>>();
internal Map<UmpleClassifier,List<Token>> unlinkedExtendsTokens = new HashMap<UmpleClassifier,List<Token>>();
internal Map<UmpleClassifier,List<Token>> unlinkedExtendsTokensInterface = new HashMap <UmpleClassifier, List<Token>>();
internal Map<UmpleClassifier,List<String>> unlinkedInterfaceExtends = new HashMap<UmpleClassifier,List<String>>();
// List of comments populated by the parser to be added to the next class, attribute, association, method, etc.
internal Comment[] lastComments;
// last attribute made.
internal Attribute lastattr = null;
// last association made.
internal AssociationVariable lastassoc = null;
Position lastassocPosition = null;
internal Map<Token, UmpleClass> attributeAutouniqueImmutable = null;
/*
* Analyze class definitions and related tokens.
*
* @param t The current token that will be analyzed to determine how to further make use of it (is it a comment, class, etc?)
* @param analysisStep Used to determine whether or not things should be analyzed more than once (multi-pass).
*/
private void analyzeClassToken(Token t, int analysisStep)
{
if (analysisStep != 2)
{
shouldProcessAgain = shouldProcessAgain || (analysisStep == 1);
return;
}
// Only need to clear comments if there actually was comments.
boolean shouldConsumeComment = lastComments.size() > 0;
// Determine what the current token is primarily, and based on that the analysis procedure is determined.
if (t.isStatic("//") || t.isStatic("/*") || t.isStatic("*/"))
{
shouldConsumeComment = false;
}
else if (t.is("useStatement") )
{
analyzeAllTokens(t);
}
else if (t.is("strictness") )
{
// unimplemented feature. Issue a warning that it is currently not fully implemented
setFailedPosition(t.getPosition(), 9999, t.getName(), t.toString());
}
else if (t.is("strictnessMessage"))
{
if(t.getValue("message").equals("allow"))
{
allowMessage(Integer.parseInt(t.getValue("messageNumber")));
}
else if(t.getValue("message").equals("disallow"))
{
disallowMessage(Integer.parseInt(t.getValue("messageNumber")));
}
else if(t.getValue("message").equals("ignore"))
{
ignoreMessage(Integer.parseInt(t.getValue("messageNumber")));
}
else if(t.getValue("message").equals("expect"))
{
expectMessage(Integer.parseInt(t.getValue("messageNumber")));
}
}
else if (t.is("namespace"))
{
if(!packageNameUsed && !t.getValue().equals(currentPackageName))
setFailedPosition(t.getPosition(),31,currentPackageName,t.getValue());
currentPackageName = t.getValue();
if (model.getDefaultNamespace() == null)
{
model.setDefaultNamespace(currentPackageName);
}
packageNameUsed = false;
}
else if (t.is("inlineComment"))
{
analyzeComment(t);
shouldConsumeComment = addToLastAttributeOrAssociation(t);
}
else if (t.is("multilineComment"))
{
analyzeMultilineComment(t);
shouldConsumeComment = false;
}
else if (t.is("classDefinition"))
{
analyzeClass(t);
}
else if (t.is("externalDefinition"))
{
if (t.getValue("interface")!=null)
analyzeExternalInterface(t);
else
analyzeExternal(t);
}
else if (t.is("interfaceDefinition"))
{
analyzeInterface(t);
}
else if (t.is("associationClassDefinition"))
{
analyzeAssociationClass(t);
}
else if (t.is("associationDefinition"))
{
analyzeAllAssociations(t);
}
else if (t.is("toplevelException"))
{
analyzeToplevelException(t);
}
// This essentially "clears" the comments in the list so that new comments, when parsed, will be the ones appearing above
// classes, methods, attributes, etc (whichever comes next) rather than old comments propogating everywhere.
if (shouldConsumeComment)
{
lastComments.clear();
}
}
/*
* If the last attribute/association made matches the line number of the comment, add the comment to the attribute/association.
*
* @param t the comment token that is used to see the position of the comment
*/
private boolean addToLastAttributeOrAssociation(Token t)
{
if (t.getPosition() == null) {return false;}
if (lastattr != null)
{
if (lastattr.getPosition().getLineNum() == t.getPosition().getLineNum())
{
// this will only add the current comment, as there can only be 1 inline comment per line
for (Comment c : lastComments)
{
lastattr.addComment(c);
}
return true;
}
else
{
lastattr = null;
}
}
else if (lastassoc != null && lastassocPosition != null)
{
if (lastassocPosition.getLineNum() == t.getPosition().getLineNum())
{
// this will only add the current comment, as there can only be 1 inline comment per line
for (Comment c : lastComments)
{
lastassoc.addComment(c);
}
return true;
}
else
{
lastassoc = null;
}
}
return false;
}
/*
* Analyze class content tokens.
*
* @param token The current token that will be analyzed to determine how to further make use of it (is it a method, comment,
* attribute, etc?)
* @param aClass The Umple class used such that parsed content such as methods, attributes, comments, etc may be added to
* it.
* @param analysisStep Used to determine whether or not things should be analyzed more than once (multi-pass).
*/
private void analyzeClassToken(Token token, UmpleClass aClass, int analysisStep)
{
if (analysisStep != 1)
{
return;
}
// Only need to clear comments if there actually was comments.
boolean shouldConsumeComment = lastComments.size() > 0;
// Determine what the current token is primarily, and based on that the analysis procedure is determined.
if (token.isStatic("//") || token.isStatic("/*") || token.isStatic("*/"))
{
shouldConsumeComment = false;
}
else if (token.is("inlineComment"))
{
analyzeComment(token);
shouldConsumeComment = addToLastAttributeOrAssociation(token);
}
else if (token.is("multilineComment"))
{
analyzeMultilineComment(token);
shouldConsumeComment = false;
}
// TODO Under development
else if (token.is("classDefinition"))
{
UmpleClass childClass = analyzeClass(token);
boolean wasSet = childClass.setExtendsClass(aClass);
if (!wasSet)
{
setFailedPosition(token.getPosition(), 16, childClass.getName(), aClass.getName());
}
}
else if (token.is("constantDeclaration"))
{
analyzeConstant(token,aClass);
}
else if(token.is("constantDeclarationDeprecated"))
{
setFailedPosition(token.getPosition(), 901);
analyzeConstant(token,aClass);
}
else if (token.is("attribute"))
{
analyzeAttribute(token,aClass);
}
else if (token.is("extraCode"))
{
analyzeExtraCode(token,aClass);
}
else if (token.is("concreteMethodDeclaration"))
{
analyzeMethod(token,aClass);
}
else if (token.is("depend"))
{
Depend d = new Depend(token.getValue());
aClass.addDepend(d);
}
else if (token.is("inlineAssociation"))
{
analyzeinlineAssociation(token,aClass);
}
else if (token.is("symmetricReflexiveAssociation"))
{
analyzeSymmetricReflexiveAssociation(token,aClass);
}
else if (token.is("invariant"))
{
if(!unanalyzedInvariants.containsKey(aClass))
{
unanalyzedInvariants.put(aClass, new ArrayList<Token>());
}
unanalyzedInvariants.get(aClass).add(token);
}
else if (token.is("exception"))
{
analyzeException(token,aClass);
}
// This essentially "clears" the comments in the list so that new comments, when parsed, will be the ones appearing above
// classes, methods, attributes, etc (whichever comes next) rather than old comments propogating everywhere.
if (shouldConsumeComment)
{
lastComments.clear();
}
}
/**
* Analyzes a comment to determine if it should be added into the list of currently parsed comments waiting to be added to
* a class, attribute, association, method or otherwise.
*
* Note that this is for an inline comment rather than a multiline comment.
*
* @param token The current token which has been flagged to be a comment to analyze, containing its value.
*/
private void analyzeComment(Token token)
{
String theValue = "";
// Special comment directive to force umpleoutput directives to be added
// In every class
if (token.getValue().startsWith("@outputumplesource")) {
outputUmpleSource = true;
return;
}
if (!token.getValue().equals("$?[End_of_model]$?"))
{
theValue = token.getValue();
lastComments.add(new Comment(theValue));
}
}
/**
* Analyzes a comment to determine if it should be added into the list of currently parsed comments waiting to be added to
* a class, attribute, association, method or otherwise.
*
* Note that this is for a multiline comment, which essentially means the possibility of multiple inline comments (1 per line)
* that will be concatenated together.
*
* @param token The current token which has been flagged to be a comment to analyze, containing its value.
*/
private void analyzeMultilineComment(Token token)
{
String inlineComments[] = token.getValue().split("\n");
// Go through the inline comments and add them to the list of comments waiting to be applied
String theComment = "";
for (int i = 0; i < inlineComments.length; i++)
{
theComment = inlineComments[i];
if(theComment.startsWith("@outputumplesource")) {
outputUmpleSource = true;
}
else {
Comment comment = new Comment(theComment);
comment.setIsInline(false);
lastComments.add(comment);
}
}
}
// Link associations, association variables and extends that were "defined" after their use
private void postTokenClassAnalysis()
{
if (verifyClassesInUse())
{
checkSingletonAssociations();
addUnlinkedAssociationVariables();
addUnlinkedAssociations();
addUnlinkedExtends();
//required for parsing specializedAssociations
createSpecializedLinks();
checkDuplicateAssociationNames();
checkExtendsForCycles();
checkSortedAssociations();
checkClassInterfaceAssocations();
//Solution to issue 543
//checkExtendsClass();
checkAbstractClass();
analyzeInvariants();
//Solution to issue 502
checkAttributeConflictImmutableAutounique();
//Issue 492
checkNonReachableStates();
//Issue 610
checkAssociationClassKeys();
}
}
public void analyzeInvariants()
{
for(UmpleClassifier uClass:unanalyzedInvariants.keySet())
{
for(Token invariant:unanalyzedInvariants.get(uClass))
{
analyzeInvariant(invariant,uClass);
}
}
}
//Issue 610
private void checkAssociationClassKeys()
{
for (UmpleClass uClass: model.getUmpleClasses())
{
if(uClass instanceof AssociationClass) {
AssociationClass assocClass = (AssociationClass)uClass;
assocClass.addMissingKeyIfNeeded();
}
}
}
//Issue 492
private void checkNonReachableStates()
{
List<State> lStates = new ArrayList<State>();
List<State> lParentStates = new ArrayList<State>();
for (UmpleClass uClass: model.getUmpleClasses())
{
for (StateMachine sm: uClass.getStateMachines())
{
//Enum check
if(((sm.getAllEvents().size() > 0) && (sm.getAllTransitions().size() > 0)) || (sm.getNestedStateMachines().size() >0))
{
lStates = new ArrayList<State>();
State state = sm.getStartState();
checkReachableStates(state, lStates, lParentStates);
for(State sp : lParentStates)
{
if(!lStates.contains(sp))
{
lStates.add(sp);
}
}
raiseWarningUnreachable(sm, lStates);
}
}
}
}
//Issue 492
private void checkReachableStates(State s, List<State> ls, List<State> lp)
{
if(s == null)
return;
if(!ls.contains(s))
ls.add(s);
for ( Transition transition: s.getTransitions())
{
if(!ls.contains(transition.getNextState()))
checkReachableStates(transition.getNextState(), ls, lp);
}
if(s.hasNestedStateMachines())
{
for(StateMachine nestedSm: s.getNestedStateMachines())
{
checkReachableStates(nestedSm.getStartState(), ls, lp);
}
}
StateMachine aux = s.getStateMachine();
State fatherState = null;
while(aux.getParentState() != null)
{
fatherState = aux.getParentState();
if(!lp.contains(fatherState))
{
lp.add(fatherState);
for ( Transition transition: fatherState.getTransitions())
{
if(!ls.contains(transition.getNextState()))
checkReachableStates(transition.getNextState(), ls, lp);
}
}
if (fatherState.getStateMachine() == null)
return;
aux = fatherState.getStateMachine();
}
}
private void raiseWarningUnreachable(StateMachine sm, List<State> lStates)
{
boolean error = false;
if(!sm.hasStates())
return;
for(State s : sm.getStates())
{
if(!lStates.contains(s))
{
if(s.getPosition() != null)
{
getParseResult().addErrorMessage(new ErrorMessage(67,s.getPosition(),s.getName(), sm.getName()));
error = true;
}
}
if (s.hasNestedStateMachines() && !error)
{
for(StateMachine sm2 : s.getNestedStateMachines())
raiseWarningUnreachable(sm2, lStates);
}
}
}
private void checkAttributeConflictImmutableAutounique(){
if(attributeAutouniqueImmutable != null && attributeAutouniqueImmutable.size() > 0)
{
for(Map.Entry<Token, UmpleClass> entry : attributeAutouniqueImmutable.entrySet())
{
String attrNameCapital = Character.toUpperCase(entry.getKey().getValue("name").charAt(0)) + entry.getKey().getValue("name").substring(1);
if(entry.getKey().getValue("autounique") != null)
{
for(Attribute attr : entry.getValue().getAttributes())
{
if(attr.getName().equals("next"+attrNameCapital))
{
getParseResult().addErrorMessage(new ErrorMessage(38,attr.getPosition(),attr.getName(),"autounique",entry.getKey().getValue("name")));
}
}
}
if(entry.getKey().getValue("modifier") !=null)
{
for(Attribute attr : entry.getValue().getAttributes())
{
if(attr.getName().equals("canSet"+attrNameCapital)||attr.getName().equals("can_set_"+entry.getKey().getValue("name")))
{
getParseResult().addErrorMessage(new ErrorMessage(38,attr.getPosition(),attr.getName(),"immutable",entry.getKey().getValue("name")));
}
}
}
}
}
}
private void postTokenInterfaceAnalysis()
{
addUnlinkedInterfaceExtends();
checkExtendsForCyclesInterface();
}
private List <UmpleInterface> recursiveCycleCheckInterface(List <UmpleInterface> extend,UmpleInterface parent, HashMap<UmpleInterface, Boolean> map)
{
List <UmpleInterface> temp = new ArrayList <UmpleInterface>();
if(extend == null|| extend.isEmpty())
return temp;
for (UmpleInterface I: extend){
if(map.containsKey(I)){
temp.add(I);
return temp;
}
map.put(I, true);
for (UmpleInterface EI: I.getExtendsInterface()){
if(parent.equals(EI)){
temp.add(EI);
return temp;
}
}
List <UmpleInterface> temptemp = recursiveCycleCheckInterface(I.getExtendsInterface(), parent, map);
temp.addAll(temptemp);
}
return temp;
}
private void checkExtendsForCyclesInterface()
{
for(UmpleInterface I : model.getUmpleInterfaces())
{
HashMap<UmpleInterface, Boolean> vistedMap = new HashMap<UmpleInterface, Boolean>();
if(I.getExtendsInterface() != null)
{
if(recursiveCycleCheckInterface(I.getExtendsInterface(), I, vistedMap).contains(I))
{
Token t = I.getExtendsToken();
if(t.getValue().equals(I.getName()))
getParseResult().addErrorMessage(new ErrorMessage(11,t.getPosition(),"Interface",I.getName()));
else
getParseResult().addErrorMessage(new ErrorMessage(12,t.getPosition(),"Interface",t.getValue(),I.getName()));
}
}
}
}
private UmpleClass recursiveCycleCheck(UmpleClass extend, UmpleClass parent, HashMap<UmpleClass, Boolean> map)
{
UmpleClass temp = null;
if(extend == null)
return null;
if(map.containsKey(extend))
return extend;
map.put(extend, true);
if(parent.equals(extend.getExtendsClass()))
return extend.getExtendsClass();
temp = recursiveCycleCheck(extend.getExtendsClass(), parent, map);
return temp;
}
private void checkExtendsForCycles()
{
for(UmpleClass C : model.getUmpleClasses())
{
HashMap<UmpleClass, Boolean> vistedMap = new HashMap<UmpleClass, Boolean>();
if(C.getExtendsClass() != null)
{
if(C.equals(recursiveCycleCheck(C.getExtendsClass(), C, vistedMap)))
{
Token t = C.getExtendsToken();
if(t.getValue().equals(C.getName()))
getParseResult().addErrorMessage(new ErrorMessage(11,t.getPosition(),"Class",C.getName()));
else
getParseResult().addErrorMessage(new ErrorMessage(12,t.getPosition(),"Class",t.getValue(),C.getName()));
}
}
}
}
// Check for classes that should be abstract
private void checkAbstractClass(){
for(UmpleClass uClass : model.getUmpleClasses())
{
if(uClass.getIsAbstract())
{
// no work needs to be done on this class.
continue;
}
if(uClass.getUnimplementedMethods().length > 0)
{
uClass.setIsAbstract(true);
continue;
}
// Check if class should be abstract based on properties of ancestors
HashMap<String, Integer> uChildren = new HashMap<String, Integer>();
checkAbstractClassRecursive(uClass, uChildren);
}
}
/*
* Checks for unimplemented methods in ancestor classes
* against the implemented methods in aClass. if neither
* aClass nor its ancestors implemented a method, then
* aClass must be abstract.
*/
private Method[] checkAbstractClassRecursive(UmpleClass aClass, HashMap<String, Integer> aChildren){
if(aClass.getExtendsToken() == null || aClass.getExtendsClass() == null || aChildren.get(aClass.getName()) != null)
{
return aClass.getUnimplementedMethods();
}
aChildren.put(aClass.getName(), 1);
Method[] ancestorUnimplementedMethods = checkAbstractClassRecursive(aClass.getExtendsClass(), aChildren);
for(Method unimplementedMethod : ancestorUnimplementedMethods)
{
if(!aClass.hasMethod(unimplementedMethod))
{
aClass.addUnimplementedMethod(unimplementedMethod);
}
}
if(aClass.getUnimplementedMethods().length > 0)
{
aClass.setIsAbstract(true);
}
return aClass.getUnimplementedMethods();
}
// Check for the existence of a a parent class
private void checkExtendsClass(){
for(UmpleClass child : model.getUmpleClasses())
{
if(child.getExtendsToken() != null)
{
if (child.getExtendsClass() != null )
{
// check to make sure attribute doesn't share a name with inherited attribute - F.K.
//Issue 578
List<Attribute> attRepetition = new ArrayList<Attribute>();
for(Attribute aAttribute : child.getAttributes())
{
UmpleClass extend = child.getExtendsClass();
while(extend!=null && child != extend)
{
for(Attribute extendAttr : extend.getAttributes())
{
String currentName = aAttribute.getName();
if (extendAttr.getName().equals(currentName))
{
if(extendAttr.getType().equals(aAttribute.getType()))
{
if(aAttribute.getValue() == null)
attRepetition.add(aAttribute);
else
aAttribute.setIsRefinement(true);
}
else
{
attRepetition.add(aAttribute);
setFailedPosition(aAttribute.getPosition(), 44, child.getName(), currentName, extend.getName());
}
}
}
extend = extend.getExtendsClass();
}
}
for(Attribute att : attRepetition)
child.deleteAttribute(att);
continue;
}
else{
Token t = child.getExtendsToken();
getParseResult().addErrorMessage(new ErrorMessage(33,t.getPosition(),t.getValue(),child.getName()));
}
}
}
}
/*
* Analyzes all associations that are part of the given token indicated to be related to an association.
*
* @param associationToken The token indicated to be an association or association Class where sub tokens will be analyzed from to further
* analyze the individual associations.
*/
private void analyzeAllAssociations(Token associationToken)
{
String name = associationToken.getValue("name");
// Go through every token that is a child of the current token (all associations part of this association).
for(Token token : associationToken.getSubTokens()){
boolean isAssociationToken = token.is("association");
//Issue 213/131: [association] elements inside associationClasses generate 2 associations instead of one
if (isAssociationToken && associationToken.is("associationClassDefinition")) {
for (Token t : token.getSubTokens()) {
if (t.is("associationEnd")) {
analyzeAssociation(t, "");
}
}
} else if (isAssociationToken || token.is("singleAssociationEnd")){
Association association = analyzeAssociation(token, "");
if(isAssociationToken && association != null){
association.setName(name);
}
}
if (!getParseResult().getWasSuccess()) { return; }
}
}
/*
* Analyzes a class token to populate an Umple class.
*
* This is also where the list of currently parsed comments will be added to the Umple class.
*
* @param classToken The token which contains the data to be analyzed to populate an Umple class.
*
* @return An Umple class populated with data based on the analysis of the class token.
*/
private UmpleClass analyzeClass(Token classToken)
{
//Reset number of activeObjects with each class definition
this.numberOfActiveObjects = 1;
String className = classToken.getValue("name").split(" ")[classToken.getValue("name").split(" ").length-1];
//Check to ensure the name is valid (starts with a letter, and only contains letters, numbers, or underscores
if (Token.isValidIdentifier(className, "[A-Za-z]") != true) {
setFailedPosition(classToken.getPosition(), 100, className);
}
else if ( className.matches("[a-z].*") ){ // Warn when class name does not start with a capital letter.
setFailedPosition(classToken.getPosition(), 101, className);
}
UmpleClass aClass;
//Issue 213: UmpleClass can be an AssociationClass
if(classToken.is("associationClassDefinition")){
aClass = model.addAssociationClass(classToken.getValue("name"));
}else{
aClass = model.addUmpleClass(classToken.getValue("name"));
}
if ( classToken.is("classDefinition") && "external".equals(aClass.getModifier()) )
aClass.setModifier(""); // Remove the external modifier if a non-external specification of this class is found.
Position thePosition = classToken.getPosition();
Position endPosition = classToken.getEndPosition();
// Set the original .ump file and line number
aClass.addPosition(thePosition);
aClass.addEndPosition(endPosition);
// Add all the comments in the comment list to the Umple class.
// But add them before any umplesource special comments
int regularCommentCountEnd = 0;
for (Comment c : aClass.getComments()) {
if(c.getText().startsWith("@umplesource")) break;
regularCommentCountEnd++;
}
for (Comment c : lastComments)
{
aClass.addCommentAt(c,regularCommentCountEnd);
regularCommentCountEnd++;
}
// Add special position comment at the end if @outputumplesource had been
// detected earlier in a comment
if(outputUmpleSource == true) {
aClass.addComment(new Comment("@umplesource " + thePosition.getRelativePath((UmpleClass)null,"Java")+" "+thePosition.getLineNumber()));
}
// If the "abstract" keyword is parsed, make the Umple class an abstract class.
if (classToken.getValue("abstract") != null)
{
boolean wasSet = aClass.setIsAbstract(true);
// Ensure the value was set.
if (wasSet == false)
{
setFailedPosition(classToken.getPosition(), 0, "Unable to make class abstract!");
}
}
addExtendsTo(classToken, aClass, unlinkedExtends, unlinkedExtendsTokens);
// If the "singleton" keyword is parsed, make the Umple class a singleton.
if (classToken.getValue("singleton") != null)
{
aClass.setIsSingleton(true);
}
if(!"".equals(aClass.getPackageName()) && !currentPackageName.equals(aClass.getPackageName()) && !packageNameUsed){
setFailedPosition(classToken.getPosition(), 30, aClass.getName(), currentPackageName);
aClass.setPackageName(currentPackageName);
}
if("".equals(aClass.getPackageName())){
aClass.setPackageName(currentPackageName);
}
packageNameUsed = true;
if (aClass.getIsSingleton())
{
classToken.setName(classToken.getName());
}
if (classToken.getValue("immutable") != null)
{
boolean wasSet = aClass.setImmutable();
if (!wasSet)
{
// Future-proofing: currently all paths cause wasSet to be true
setFailedPosition(classToken.getPosition(), 14, classToken.getName());
}
}
if(classToken.is("associationClassDefinition")) {
analyzeAllAssociations(classToken);
}
analyzeAllTokens(classToken,aClass);
return aClass;
}
/*
* Takes an Umple classifier and analyzes a classifier token to add classifiers which extend it.
*
* @param classifierToken The token to be analyzed to add subclasses to the specified Umple classifier.
* @param aClassifier The Umple classifier for which subclasses will be added.
*/
private void addExtendsTo(Token classifierToken, UmpleClassifier aClassifier, Map <UmpleClassifier,List <String>> unlinkedExtends, Map <UmpleClassifier, List<Token>> unlinkedExtendsTokens)
//private void addExtendsTo(Token classToken, UmpleClassifier aClassifier)
{
List<String> extendsList = new ArrayList<String>();
List<Token> extendsTokenList = new ArrayList<Token>();
String extendName="";
// Go through all sub-tokens of the class token to add subclasses related to the Umple class.
for (Token extendsToken : classifierToken.getSubTokens())
{
if (extendsToken.getValue("extendsName") != null||extendsToken.getValue("extendsNames")!=null)
{
String name = extendsToken.getValue("extendsName") != null?extendsToken.getValue("extendsName"):extendsToken.getValue("extendsNames");
extendsList.add(name);
extendsTokenList.add(extendsToken); // With this line we really don't need the above, todo: refactor
unlinkedExtends.put(aClassifier, extendsList);
unlinkedExtendsTokens.put(aClassifier, extendsTokenList);
extendName = extendsToken.getValue("extendsName");
} else if (extendsToken.getValue("gTemplateParameter") !=null ){
processGTemplateParameterAssignment(extendsToken, aClassifier, extendName);
}
}
//Checks list of extends tokens to prevent multiple class inheritance
if(numberOfExtendsClass(extendsTokenList) > 1)
{
Token t = extendsTokenList.get(0);
getParseResult().addErrorMessage(new ErrorMessage(34,t.getPosition(),aClassifier.getName(),t.getValue()));
}
}
//Returns the number of umple class in extends list (extList)
private int numberOfExtendsClass(List<Token> extList)
{
int counter = 0;
for(Token t : extList)
{
if(isAnUmpleClass(t.getValue("extendsName")))
counter++;
}
return counter;
}
//This method checks if an umple element with name "name" is an umple class
private boolean isAnUmpleClass(String name)
{
for(UmpleClass aClass : model.getUmpleClasses())
{
if(aClass != null)
{
String nam = aClass.getName();
if(nam.equals(name))
return true;
}
}
return false;
}
private UmpleClass analyzeExternal(Token externalToken)
{
// Check to see if there is an existing class
UmpleClass existingClass = model.getUmpleClass(externalToken.getValue("name"));
UmpleClass aClass = analyzeClass(externalToken);
// Only set the modifier to external if there is not a class defined with the same name
if ( existingClass == null )
aClass.setModifier("external");
return aClass;
}
private UmpleInterface analyzeExternalInterface(Token externalToken)
{
UmpleInterface anInterface = analyzeInterface(externalToken);
anInterface.setModifier("external");
return anInterface;
}
private UmpleInterface analyzeInterface(Token t)
{
String interfaceName = t.getValue("name");
//Check to ensure the name is valid (starts with a letter, and only contains letters, numbers, or underscores
if (Token.isValidIdentifier(interfaceName, "[A-Za-z|@]") != true) {
if(!interfaceName.matches("[A-Za-z|@]+<.+>"))
{
setFailedPosition(t.getPosition(), 110, interfaceName);
}
}
else if ( interfaceName.matches("[a-z].*") ){ // Warn when interface name doesn't start with a capital letter.
setFailedPosition(t.getPosition(), 111, interfaceName);
}
UmpleInterface newInterface = new UmpleInterface(t.getValue("name"));
model.addUmpleInterface(newInterface);
if(!"".equals(newInterface.getPackageName()) && !currentPackageName.equals(newInterface.getPackageName()) && !packageNameUsed){
setFailedPosition(t.getPosition(), 30, newInterface.getName(), currentPackageName);
newInterface.setPackageName(currentPackageName);
}
if("".equals(newInterface.getPackageName())){
newInterface.setPackageName(currentPackageName);
}
packageNameUsed = true;
analyzeInterface(t,newInterface);
return newInterface;
}
private void analyzeInterface(Token interfaceToken, UmpleInterface aInterface)
{
for(Token token : interfaceToken.getSubTokens())
{
if (token.is("depend"))
{
Depend d = new Depend(token.getValue());
aInterface.addDepend(d);
}
if (token.is("interfaceMemberDeclaration"))
{
analyzeInterfaceMembers(token, aInterface);
}