-
Notifications
You must be signed in to change notification settings - Fork 140
/
UMLModelASTReader.java
1430 lines (1325 loc) · 72.3 KB
/
UMLModelASTReader.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
package gr.uom.java.xmi;
import static gr.uom.java.xmi.decomposition.Visitor.stringify;
import static org.eclipse.jdt.core.JavaCore.VERSION_21;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import com.github.gumtreediff.gen.jdt.AbstractJdtVisitor;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.compiler.IScanner;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.IDocElement;
import org.eclipse.jdt.core.dom.IExtendedModifier;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.Initializer;
import org.eclipse.jdt.core.dom.JavaDocRegion;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.MemberRef;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.MethodRef;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.RecordDeclaration;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.jdt.core.dom.TagElement;
import org.eclipse.jdt.core.dom.TagProperty;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclarationStatement;
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import com.github.gumtreediff.tree.TreeContext;
import gr.uom.java.xmi.LocationInfo.CodeElementType;
import gr.uom.java.xmi.decomposition.AbstractExpression;
import gr.uom.java.xmi.decomposition.OperationBody;
import gr.uom.java.xmi.decomposition.VariableDeclaration;
import org.refactoringminer.astDiff.visitors.JdtWithCommentsVisitor;
public class UMLModelASTReader {
private boolean VISIT_JDT_COMMENTS = Boolean.parseBoolean(System.getProperty("rm.jdt.comments", "true"));
private static final String FREE_MARKER_GENERATED = "generated using freemarker";
private static final String FREE_MARKER_GENERATED_2 = "generated using FreeMarker";
private static final String ANTLR_GENERATED = "// $ANTLR";
private static final String XTEXT_GENERATED = "generated by Xtext";
private static final String LWJGL_GENERATED = "MACHINE GENERATED FILE, DO NOT EDIT";
private static final String TEST_GENERATOR_GENERATED = "generated by TestGenerator";
private static final String THRIFT_GENERATED = "Autogenerated by Thrift Compiler";
private static final String AUTOREST_GENERATED = "AutoRest Code Generator";
private static final String FHIR_GENERATED = "for FHIR";
private static final String CAMEL_GENERATED = "Generated by camel-package-maven-plugin";
private static final String DEFAULT_JAVA_CORE_VERSION = JavaCore.VERSION_14;
private static final Pattern LEAD_WHITE_SPACE_JAVADOC = Pattern.compile("^\s+\\*", Pattern.MULTILINE);
private UMLModel umlModel;
public UMLModelASTReader(Map<String, String> javaFileContents, Set<String> repositoryDirectories, boolean astDiff) {
this.umlModel = new UMLModel(repositoryDirectories);
processJavaFileContents(javaFileContents, astDiff);
}
public static ASTNode processBlock(String methodBody) {
ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
Map<String, String> options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8);
parser.setCompilerOptions(options);
parser.setResolveBindings(false);
parser.setKind(ASTParser.K_STATEMENTS);
parser.setStatementsRecovery(true);
char[] charArray = methodBody.toCharArray();
parser.setSource(charArray);
ASTNode node = parser.createAST(null);
ASTNode methodBodyBlock = null;
if(node instanceof Block) {
Block extraBlockAddedByParser = (Block)node;
if(extraBlockAddedByParser.statements().size() > 0) {
methodBodyBlock = (ASTNode)extraBlockAddedByParser.statements().get(0);
}
}
return methodBodyBlock;
}
private void processJavaFileContents(Map<String, String> javaFileContents, boolean astDiff) {
ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
for(String filePath : javaFileContents.keySet()) {
String javaFileContent = javaFileContents.get(filePath);
if((javaFileContent.contains(FREE_MARKER_GENERATED) || javaFileContent.contains(FREE_MARKER_GENERATED_2) || javaFileContent.contains(ANTLR_GENERATED) ||
javaFileContent.contains(XTEXT_GENERATED) || javaFileContent.contains(LWJGL_GENERATED) || javaFileContent.contains(TEST_GENERATOR_GENERATED) ||
javaFileContent.contains(THRIFT_GENERATED) || javaFileContent.contains(AUTOREST_GENERATED) || javaFileContent.contains(FHIR_GENERATED) ||
javaFileContent.contains(CAMEL_GENERATED)) &&
!javaFileContent.contains("\"" + CAMEL_GENERATED + "\"") &&
!javaFileContent.contains("private static final String FREE_MARKER_GENERATED = \"generated using freemarker\";")) {
continue;
}
char[] charArray = javaFileContent.toCharArray();
try {
CompilationUnit compilationUnit = getCompilationUnit(DEFAULT_JAVA_CORE_VERSION, parser, charArray);
String maxRecommendedVersionFromProblems = getMaxRecommendedVersionFromProblems(compilationUnit);
if (maxRecommendedVersionFromProblems != null)
compilationUnit = getCompilationUnit(maxRecommendedVersionFromProblems, parser, charArray);
processCompilationUnit(filePath, compilationUnit, javaFileContent);
if(astDiff) {
IScanner scanner = ToolFactory.createScanner(true, false, false, false);
scanner.setSource(charArray);
AbstractJdtVisitor visitor = (VISIT_JDT_COMMENTS) ?
new JdtWithCommentsVisitor(scanner) :
new com.github.gumtreediff.gen.jdt.JdtVisitor(scanner);
compilationUnit.accept(visitor);
TreeContext treeContext = visitor.getTreeContext();
this.umlModel.getTreeContextMap().put(filePath, treeContext);
}
}
catch(Exception e) {
//e.printStackTrace();
}
}
}
public static CompilationUnit getCompilationUnit(String javaCoreVersion, ASTParser parser, char[] charArray) {
Map<String, String> options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, javaCoreVersion);
options.put(JavaCore.COMPILER_SOURCE, javaCoreVersion);
options.put(JavaCore.COMPILER_COMPLIANCE, javaCoreVersion);
parser.setCompilerOptions(options);
parser.setResolveBindings(false);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setStatementsRecovery(true);
parser.setSource(charArray);
return (CompilationUnit) parser.createAST(null);
}
public static String getMaxRecommendedVersionFromProblems(CompilationUnit compilationUnit) {
IProblem[] problems = compilationUnit.getProblems();
String result = null;
if (problems.length == 0) return null;
for (IProblem problem : problems) {
String[] arguments = problem.getArguments();
if (arguments != null && arguments.length > 1) {
try {
double value = Double.parseDouble(arguments[1]);
if (result == null || value > Double.parseDouble(result)) {
result = arguments[1];
}
} catch (NumberFormatException e) {
// System.out.println("Invalid number format in arguments: " + arguments[1]);
}
}
}
if (result == null) return VERSION_21;
return result;
}
public UMLModel getUmlModel() {
return this.umlModel;
}
protected void processCompilationUnit(String sourceFilePath, CompilationUnit compilationUnit, String javaFileContent) {
PackageDeclaration packageDeclaration = compilationUnit.getPackage();
String packageName = null;
UMLJavadoc packageDoc = null;
UMLPackage umlPackage = null;
String sourceFolder = "";
if(packageDeclaration != null) {
packageName = packageDeclaration.getName().getFullyQualifiedName();
int index = sourceFilePath.indexOf(packageName.replace('.', '/'));
if(index != -1) {
sourceFolder = sourceFilePath.substring(0, index);
}
packageDoc = generateJavadoc(compilationUnit, sourceFolder, sourceFilePath, packageDeclaration.getJavadoc(), javaFileContent);
LocationInfo packageLocationInfo = generateLocationInfo(compilationUnit, sourceFolder, sourceFilePath, packageDeclaration, CodeElementType.PACKAGE_DECLARATION);
umlPackage = new UMLPackage(packageName, packageLocationInfo);
}
else {
packageName = "";
if(compilationUnit.types().size() > 0) {
AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration) compilationUnit.types().get(0);
int index = sourceFilePath.indexOf(typeDeclaration.getName().getFullyQualifiedName());
if(index != -1) {
sourceFolder = sourceFilePath.substring(0, index);
}
}
}
List<UMLComment> comments = extractInternalComments(compilationUnit, sourceFolder, sourceFilePath, javaFileContent);
this.umlModel.getCommentMap().put(sourceFilePath, comments);
List<ImportDeclaration> imports = compilationUnit.imports();
List<UMLImport> importedTypes = new ArrayList<UMLImport>();
for(ImportDeclaration importDeclaration : imports) {
String elementName = importDeclaration.getName().getFullyQualifiedName();
LocationInfo locationInfo = generateLocationInfo(compilationUnit, sourceFolder, sourceFilePath, importDeclaration, CodeElementType.IMPORT_DECLARATION);
UMLImport imported = new UMLImport(elementName, importDeclaration.isOnDemand(), importDeclaration.isStatic(), locationInfo);
importedTypes.add(imported);
}
List<AbstractTypeDeclaration> topLevelTypeDeclarations = compilationUnit.types();
for(AbstractTypeDeclaration abstractTypeDeclaration : topLevelTypeDeclarations) {
if(abstractTypeDeclaration instanceof TypeDeclaration) {
TypeDeclaration topLevelTypeDeclaration = (TypeDeclaration)abstractTypeDeclaration;
processTypeDeclaration(compilationUnit, topLevelTypeDeclaration, umlPackage, packageName, sourceFolder, sourceFilePath, importedTypes, packageDoc, comments, javaFileContent);
}
else if(abstractTypeDeclaration instanceof EnumDeclaration) {
EnumDeclaration enumDeclaration = (EnumDeclaration)abstractTypeDeclaration;
processEnumDeclaration(compilationUnit, enumDeclaration, umlPackage, packageName, sourceFolder, sourceFilePath, importedTypes, packageDoc, comments, javaFileContent);
}
else if(abstractTypeDeclaration instanceof AnnotationTypeDeclaration) {
AnnotationTypeDeclaration annotationDeclaration = (AnnotationTypeDeclaration)abstractTypeDeclaration;
processAnnotationTypeDeclaration(compilationUnit, annotationDeclaration, umlPackage, packageName, sourceFolder, sourceFilePath, importedTypes, packageDoc, comments, javaFileContent);
}
else if(abstractTypeDeclaration instanceof RecordDeclaration) {
RecordDeclaration recordDeclaration = (RecordDeclaration)abstractTypeDeclaration;
processRecordDeclaration(compilationUnit, recordDeclaration, umlPackage, packageName, sourceFolder, sourceFilePath, importedTypes, packageDoc, comments, javaFileContent);
}
}
}
private List<UMLComment> extractInternalComments(CompilationUnit cu, String sourceFolder, String sourceFile, String javaFileContent) {
List<Comment> astComments = cu.getCommentList();
List<UMLComment> comments = new ArrayList<UMLComment>();
for(Comment comment : astComments) {
LocationInfo locationInfo = null;
if(comment.isLineComment()) {
locationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, comment, CodeElementType.LINE_COMMENT);
}
else if(comment.isBlockComment()) {
locationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, comment, CodeElementType.BLOCK_COMMENT);
}
else if(comment.isDocComment() && comment.getParent() == null) {
locationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, comment, CodeElementType.BLOCK_COMMENT);
}
if(locationInfo != null) {
int start = comment.getStartPosition();
int end = start + comment.getLength();
String text = javaFileContent.substring(start, end);
UMLComment umlComment = new UMLComment(text, locationInfo);
if(comment.isDocComment() && comment.getParent() == null) {
Javadoc doc = (Javadoc)comment;
umlComment.setJavaDoc(generateJavadoc(cu, sourceFolder, sourceFile, doc, javaFileContent));
}
comments.add(umlComment);
}
}
return comments;
}
private void distributeComments(List<UMLComment> compilationUnitComments, LocationInfo codeElementLocationInfo, List<UMLComment> codeElementComments) {
ListIterator<UMLComment> listIterator = compilationUnitComments.listIterator(compilationUnitComments.size());
while(listIterator.hasPrevious()) {
UMLComment comment = listIterator.previous();
LocationInfo commentLocationInfo = comment.getLocationInfo();
if(codeElementLocationInfo.subsumes(commentLocationInfo) ||
codeElementLocationInfo.sameLine(commentLocationInfo) ||
(codeElementLocationInfo.nextLine(commentLocationInfo) && !codeElementLocationInfo.getCodeElementType().equals(CodeElementType.ANONYMOUS_CLASS_DECLARATION)) ||
(codeElementComments.size() > 0 && codeElementComments.get(0).getLocationInfo().nextLine(commentLocationInfo))) {
codeElementComments.add(0, comment);
}
}
compilationUnitComments.removeAll(codeElementComments);
}
private UMLJavadoc generateJavadoc(CompilationUnit cu, BodyDeclaration bodyDeclaration, String sourceFolder, String sourceFile, String javaFileContent) {
Javadoc javaDoc = bodyDeclaration.getJavadoc();
return generateJavadoc(cu, sourceFolder, sourceFile, javaDoc, javaFileContent);
}
private UMLJavadoc generateJavadoc(CompilationUnit cu, String sourceFolder, String sourceFile, Javadoc javaDoc, String javaFileContent) {
UMLJavadoc doc = null;
if(javaDoc != null) {
LocationInfo locationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, javaDoc, CodeElementType.JAVADOC);
int start = javaDoc.getStartPosition();
int end = start + javaDoc.getLength();
String text = javaFileContent.substring(start, end);
Matcher matcher = LEAD_WHITE_SPACE_JAVADOC.matcher(text);
StringBuilder sb = new StringBuilder();
while (matcher.find()) {
matcher.appendReplacement(sb, " \\*");
}
matcher.appendTail(sb);
String trimLeadWhiteSpace = sb.toString();
doc = new UMLJavadoc(trimLeadWhiteSpace, locationInfo);
List<TagElement> tags = javaDoc.tags();
for(TagElement parentTag : tags) {
LocationInfo tagLocationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, parentTag, CodeElementType.TAG_ELEMENT);
UMLTagElement parentTagElement = new UMLTagElement(parentTag.getTagName(), tagLocationInfo);
processTagFragments(cu, sourceFolder, sourceFile, parentTag, parentTagElement);
doc.addTag(parentTagElement);
}
}
return doc;
}
private void processTagFragments(CompilationUnit cu, String sourceFolder, String sourceFile, TagElement parentTag, UMLTagElement parentTagElement) {
List<IDocElement> fragments = parentTag.fragments();
for(IDocElement docElement : fragments) {
LocationInfo docElementLocationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, (ASTNode)docElement, CodeElementType.DOC_ELEMENT);
if(docElement instanceof TagElement) {
TagElement nestedTag = (TagElement)docElement;
UMLTagElement nestedTagElement = new UMLTagElement(nestedTag.getTagName(), docElementLocationInfo);
processTagFragments(cu, sourceFolder, sourceFile, nestedTag, nestedTagElement);
parentTagElement.addNestedTag(nestedTagElement);
}
else if(docElement instanceof JavaDocRegion) {
//TODO support JavaDocRegion
}
else {
UMLDocElement umlDocElement = new UMLDocElement(docElement.toString(), docElementLocationInfo);
if(docElement instanceof Name) {
umlDocElement.setName(true);
}
else if(docElement instanceof MemberRef) {
umlDocElement.setMemberRef(true);
}
else if(docElement instanceof MethodRef) {
umlDocElement.setMethodRef(true);
}
else if(docElement instanceof TagProperty) {
umlDocElement.setTagProperty(true);
}
parentTagElement.addFragment(umlDocElement);
}
}
}
private boolean generatedCode(UMLJavadoc javadoc) {
if(javadoc != null)
return javadoc.contains(FREE_MARKER_GENERATED) || javadoc.contains(FREE_MARKER_GENERATED_2) ||
javadoc.contains(ANTLR_GENERATED) || javadoc.contains(XTEXT_GENERATED);
return false;
}
private void processRecordDeclaration(CompilationUnit cu, RecordDeclaration recordDeclaration, UMLPackage umlPackage, String packageName, String sourceFolder, String sourceFile,
List<UMLImport> importedTypes, UMLJavadoc packageDoc, List<UMLComment> comments, String javaFileContent) {
UMLJavadoc javadoc = generateJavadoc(cu, recordDeclaration, sourceFolder, sourceFile, javaFileContent);
if(generatedCode(javadoc)) {
return;
}
String recordName = recordDeclaration.getName().getFullyQualifiedName();
LocationInfo locationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, recordDeclaration, CodeElementType.RECORD_DECLARATION);
UMLClass umlClass = new UMLClass(packageName, recordName, locationInfo, recordDeclaration.isPackageMemberTypeDeclaration(), importedTypes);
umlClass.setJavadoc(javadoc);
if(recordDeclaration.isPackageMemberTypeDeclaration()) {
umlClass.setPackageDeclaration(umlPackage);
umlClass.setPackageDeclarationJavadoc(packageDoc);
for(UMLComment comment : comments) {
if(comment.getLocationInfo().before(locationInfo) && !locationInfo.nextLine(comment.getLocationInfo())) {
umlClass.getPackageDeclarationComments().add(comment);
}
}
comments.removeAll(umlClass.getPackageDeclarationComments());
}
umlClass.setRecord(true);
int startSignatureOffset = processModifiers(cu, sourceFolder, sourceFile, recordDeclaration, umlClass, javaFileContent);
if(startSignatureOffset == -1) {
startSignatureOffset = recordDeclaration.getName().getStartPosition();
}
List<TypeParameter> typeParameters = recordDeclaration.typeParameters();
for(TypeParameter typeParameter : typeParameters) {
UMLTypeParameter umlTypeParameter = new UMLTypeParameter(typeParameter.getName().getFullyQualifiedName(),
generateLocationInfo(cu, sourceFolder, sourceFile, typeParameter, CodeElementType.TYPE_PARAMETER));
List<Type> typeBounds = typeParameter.typeBounds();
for(Type type : typeBounds) {
umlTypeParameter.addTypeBound(UMLType.extractTypeObject(cu, sourceFolder, sourceFile, type, 0, javaFileContent));
}
List<IExtendedModifier> typeParameterExtendedModifiers = typeParameter.modifiers();
for(IExtendedModifier extendedModifier : typeParameterExtendedModifiers) {
if(extendedModifier.isAnnotation()) {
Annotation annotation = (Annotation)extendedModifier;
umlTypeParameter.addAnnotation(new UMLAnnotation(cu, sourceFolder, sourceFile, annotation, javaFileContent));
}
}
umlClass.addTypeParameter(umlTypeParameter);
}
List<Type> superInterfaceTypes = recordDeclaration.superInterfaceTypes();
for(Type interfaceType : superInterfaceTypes) {
UMLType umlType = UMLType.extractTypeObject(cu, sourceFolder, sourceFile, interfaceType, 0, javaFileContent);
UMLRealization umlRealization = new UMLRealization(umlClass, umlType.getClassType());
umlClass.addImplementedInterface(umlType);
getUmlModel().addRealization(umlRealization);
}
List<SingleVariableDeclaration> recordComponents = recordDeclaration.recordComponents();
for(SingleVariableDeclaration recordComponent : recordComponents) {
Type parameterType = recordComponent.getType();
String parameterName = recordComponent.getName().getFullyQualifiedName();
UMLType type = UMLType.extractTypeObject(cu, sourceFolder, sourceFile, parameterType, recordComponent.getExtraDimensions(), javaFileContent);
if(recordComponent.isVarargs()) {
type.setVarargs();
}
LocationInfo recordComponentLocationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, recordComponent, CodeElementType.RECORD_COMPONENT);
UMLRecordComponent umlRecordComponent = new UMLRecordComponent(parameterName, type, recordComponentLocationInfo);
VariableDeclaration variableDeclaration = new VariableDeclaration(cu, sourceFolder, sourceFile, recordComponent, umlRecordComponent, recordComponent.isVarargs(), new LinkedHashMap<>(), javaFileContent);
variableDeclaration.setAttribute(true);
umlRecordComponent.setVariableDeclaration(variableDeclaration);
umlRecordComponent.setClassName(umlClass.getName());
umlClass.addAttribute(umlRecordComponent);
}
Map<BodyDeclaration, VariableDeclarationContainer> map = processBodyDeclarations(cu, recordDeclaration, umlPackage, packageName, sourceFolder, sourceFile, importedTypes, umlClass, packageDoc, comments, javaFileContent);
processAnonymousClassDeclarations(cu, recordDeclaration, umlPackage, packageName, sourceFolder, sourceFile, recordName, importedTypes, packageDoc, comments, umlClass, javaFileContent);
for(BodyDeclaration declaration : map.keySet()) {
if(declaration instanceof MethodDeclaration) {
UMLOperation operation = (UMLOperation) map.get(declaration);
processMethodBody(cu, sourceFolder, sourceFile, (MethodDeclaration) declaration, operation, umlClass.getAttributes(), javaFileContent);
}
else if(declaration instanceof Initializer) {
UMLInitializer initializer = (UMLInitializer) map.get(declaration);
processInitializerBody(cu, sourceFolder, sourceFile, (Initializer) declaration, initializer, umlClass.getAttributes(), javaFileContent);
}
}
int endSignatureOffset = recordDeclaration.bodyDeclarations().size() > 0 ?
((BodyDeclaration)recordDeclaration.bodyDeclarations().get(0)).getStartPosition() :
recordDeclaration.getStartPosition() + recordDeclaration.getLength();
String text = javaFileContent.substring(startSignatureOffset, endSignatureOffset);
if(text.contains("{"))
text = text.substring(0, text.indexOf("{") + 1);
if(!text.contains("record ")) {
umlClass.setActualSignature("record " + text);
}
else {
umlClass.setActualSignature(text);
}
this.getUmlModel().addClass(umlClass);
distributeComments(comments, locationInfo, umlClass.getComments());
}
private void processAnnotationTypeDeclaration(CompilationUnit cu, AnnotationTypeDeclaration annotationDeclaration, UMLPackage umlPackage, String packageName, String sourceFolder, String sourceFile,
List<UMLImport> importedTypes, UMLJavadoc packageDoc, List<UMLComment> comments, String javaFileContent) {
UMLJavadoc javadoc = generateJavadoc(cu, annotationDeclaration, sourceFolder, sourceFile, javaFileContent);
if(generatedCode(javadoc)) {
return;
}
String className = annotationDeclaration.getName().getFullyQualifiedName();
LocationInfo locationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, annotationDeclaration, CodeElementType.ANNOTATION_TYPE_DECLARATION);
UMLClass umlClass = new UMLClass(packageName, className, locationInfo, annotationDeclaration.isPackageMemberTypeDeclaration(), importedTypes);
umlClass.setJavadoc(javadoc);
if(annotationDeclaration.isPackageMemberTypeDeclaration()) {
umlClass.setPackageDeclaration(umlPackage);
umlClass.setPackageDeclarationJavadoc(packageDoc);
for(UMLComment comment : comments) {
if(comment.getLocationInfo().before(locationInfo) && !locationInfo.nextLine(comment.getLocationInfo())) {
umlClass.getPackageDeclarationComments().add(comment);
}
}
comments.removeAll(umlClass.getPackageDeclarationComments());
}
umlClass.setAnnotation(true);
int startSignatureOffset = processModifiers(cu, sourceFolder, sourceFile, annotationDeclaration, umlClass, javaFileContent);
if(startSignatureOffset == -1) {
startSignatureOffset = annotationDeclaration.getName().getStartPosition();
}
Map<BodyDeclaration, VariableDeclarationContainer> map = processBodyDeclarations(cu, annotationDeclaration, umlPackage, packageName, sourceFolder, sourceFile, importedTypes, umlClass, packageDoc, comments, javaFileContent);
processAnonymousClassDeclarations(cu, annotationDeclaration, umlPackage, packageName, sourceFolder, sourceFile, className, importedTypes, packageDoc, comments, umlClass, javaFileContent);
for(BodyDeclaration declaration : map.keySet()) {
if(declaration instanceof MethodDeclaration) {
UMLOperation operation = (UMLOperation) map.get(declaration);
processMethodBody(cu, sourceFolder, sourceFile, (MethodDeclaration) declaration, operation, umlClass.getAttributes(), javaFileContent);
}
else if(declaration instanceof Initializer) {
UMLInitializer initializer = (UMLInitializer) map.get(declaration);
processInitializerBody(cu, sourceFolder, sourceFile, (Initializer) declaration, initializer, umlClass.getAttributes(), javaFileContent);
}
}
int endSignatureOffset = annotationDeclaration.bodyDeclarations().size() > 0 ?
((BodyDeclaration)annotationDeclaration.bodyDeclarations().get(0)).getStartPosition() :
annotationDeclaration.getStartPosition() + annotationDeclaration.getLength();
String text = javaFileContent.substring(startSignatureOffset, endSignatureOffset);
if(text.contains("{"))
text = text.substring(0, text.indexOf("{") + 1);
if(!text.contains("@interface ")) {
umlClass.setActualSignature("@interface " + text);
}
else {
umlClass.setActualSignature(text);
}
this.getUmlModel().addClass(umlClass);
distributeComments(comments, locationInfo, umlClass.getComments());
}
private void processEnumDeclaration(CompilationUnit cu, EnumDeclaration enumDeclaration, UMLPackage umlPackage, String packageName, String sourceFolder, String sourceFile,
List<UMLImport> importedTypes, UMLJavadoc packageDoc, List<UMLComment> comments, String javaFileContent) {
UMLJavadoc javadoc = generateJavadoc(cu, enumDeclaration, sourceFolder, sourceFile, javaFileContent);
if(generatedCode(javadoc)) {
return;
}
String className = enumDeclaration.getName().getFullyQualifiedName();
LocationInfo locationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, enumDeclaration, CodeElementType.ENUM_DECLARATION);
UMLClass umlClass = new UMLClass(packageName, className, locationInfo, enumDeclaration.isPackageMemberTypeDeclaration(), importedTypes);
umlClass.setJavadoc(javadoc);
if(enumDeclaration.isPackageMemberTypeDeclaration()) {
umlClass.setPackageDeclaration(umlPackage);
umlClass.setPackageDeclarationJavadoc(packageDoc);
for(UMLComment comment : comments) {
if(comment.getLocationInfo().before(locationInfo) && !locationInfo.nextLine(comment.getLocationInfo())) {
umlClass.getPackageDeclarationComments().add(comment);
}
}
comments.removeAll(umlClass.getPackageDeclarationComments());
}
umlClass.setEnum(true);
List<Type> superInterfaceTypes = enumDeclaration.superInterfaceTypes();
for(Type interfaceType : superInterfaceTypes) {
UMLType umlType = UMLType.extractTypeObject(cu, sourceFolder, sourceFile, interfaceType, 0, javaFileContent);
UMLRealization umlRealization = new UMLRealization(umlClass, umlType.getClassType());
umlClass.addImplementedInterface(umlType);
getUmlModel().addRealization(umlRealization);
}
List<EnumConstantDeclaration> enumConstantDeclarations = enumDeclaration.enumConstants();
for(EnumConstantDeclaration enumConstantDeclaration : enumConstantDeclarations) {
processEnumConstantDeclaration(cu, enumConstantDeclaration, sourceFolder, sourceFile, umlClass, comments, javaFileContent);
}
int startSignatureOffset = processModifiers(cu, sourceFolder, sourceFile, enumDeclaration, umlClass, javaFileContent);
if(startSignatureOffset == -1) {
startSignatureOffset = enumDeclaration.getName().getStartPosition();
}
Map<BodyDeclaration, VariableDeclarationContainer> map = processBodyDeclarations(cu, enumDeclaration, umlPackage, packageName, sourceFolder, sourceFile, importedTypes, umlClass, packageDoc, comments, javaFileContent);
processAnonymousClassDeclarations(cu, enumDeclaration, umlPackage, packageName, sourceFolder, sourceFile, className, importedTypes, packageDoc, comments, umlClass, javaFileContent);
for(BodyDeclaration declaration : map.keySet()) {
if(declaration instanceof MethodDeclaration) {
UMLOperation operation = (UMLOperation) map.get(declaration);
processMethodBody(cu, sourceFolder, sourceFile, (MethodDeclaration) declaration, operation, umlClass.getAttributes(), javaFileContent);
}
else if(declaration instanceof Initializer) {
UMLInitializer initializer = (UMLInitializer) map.get(declaration);
processInitializerBody(cu, sourceFolder, sourceFile, (Initializer) declaration, initializer, umlClass.getAttributes(), javaFileContent);
}
}
int endSignatureOffset = enumDeclaration.bodyDeclarations().size() > 0 ?
((BodyDeclaration)enumDeclaration.bodyDeclarations().get(0)).getStartPosition() :
enumDeclaration.getStartPosition() + enumDeclaration.getLength();
String text = javaFileContent.substring(startSignatureOffset, endSignatureOffset);
if(text.contains("{"))
text = text.substring(0, text.indexOf("{") + 1);
if(!text.contains("enum ")) {
umlClass.setActualSignature("enum " + text);
}
else {
umlClass.setActualSignature(text);
}
this.getUmlModel().addClass(umlClass);
distributeComments(comments, locationInfo, umlClass.getComments());
}
private Map<BodyDeclaration, VariableDeclarationContainer> processBodyDeclarations(CompilationUnit cu, AbstractTypeDeclaration abstractTypeDeclaration, UMLPackage umlPackage, String packageName,
String sourceFolder, String sourceFile, List<UMLImport> importedTypes, UMLClass umlClass, UMLJavadoc packageDoc, List<UMLComment> comments, String javaFileContent) {
Map<BodyDeclaration, VariableDeclarationContainer> map = new LinkedHashMap<>();
List<BodyDeclaration> bodyDeclarations = abstractTypeDeclaration.bodyDeclarations();
boolean interfaceOrAnnotation = umlClass.isInterface() || umlClass.isAnnotation();
for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
if(bodyDeclaration instanceof FieldDeclaration) {
FieldDeclaration fieldDeclaration = (FieldDeclaration)bodyDeclaration;
List<UMLAttribute> attributes = processFieldDeclaration(cu, fieldDeclaration, interfaceOrAnnotation, sourceFolder, sourceFile, comments, javaFileContent);
for(UMLAttribute attribute : attributes) {
attribute.setClassName(umlClass.getName());
umlClass.addAttribute(attribute);
}
}
else if(bodyDeclaration instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration)bodyDeclaration;
UMLOperation operation = processMethodDeclaration(cu, methodDeclaration, packageName, interfaceOrAnnotation, sourceFolder, sourceFile, comments, javaFileContent);
operation.setClassName(umlClass.getName());
umlClass.addOperation(operation);
map.put(methodDeclaration, operation);
}
else if(bodyDeclaration instanceof AnnotationTypeMemberDeclaration) {
AnnotationTypeMemberDeclaration annotationTypeMemberDeclaration = (AnnotationTypeMemberDeclaration)bodyDeclaration;
UMLOperation operation = processAnnotationTypeMember(cu, annotationTypeMemberDeclaration, packageName, interfaceOrAnnotation, sourceFolder, sourceFile, comments, javaFileContent);
operation.setClassName(umlClass.getName());
umlClass.addOperation(operation);
map.put(annotationTypeMemberDeclaration, operation);
}
else if(bodyDeclaration instanceof Initializer) {
Initializer initializer = (Initializer)bodyDeclaration;
UMLInitializer umlInitializer = processInitializer(cu, initializer, packageName, false, sourceFolder, sourceFile, comments, javaFileContent);
umlInitializer.setClassName(umlClass.getName());
umlClass.addInitializer(umlInitializer);
map.put(initializer, umlInitializer);
}
else if(bodyDeclaration instanceof TypeDeclaration) {
TypeDeclaration typeDeclaration = (TypeDeclaration)bodyDeclaration;
processTypeDeclaration(cu, typeDeclaration, umlPackage, umlClass.getName(), sourceFolder, sourceFile, importedTypes, packageDoc, comments, javaFileContent);
}
else if(bodyDeclaration instanceof EnumDeclaration) {
EnumDeclaration enumDeclaration = (EnumDeclaration)bodyDeclaration;
processEnumDeclaration(cu, enumDeclaration, umlPackage, umlClass.getName(), sourceFolder, sourceFile, importedTypes, packageDoc, comments, javaFileContent);
}
else if(bodyDeclaration instanceof AnnotationTypeDeclaration) {
AnnotationTypeDeclaration annotationDeclaration = (AnnotationTypeDeclaration)bodyDeclaration;
processAnnotationTypeDeclaration(cu, annotationDeclaration, umlPackage, umlClass.getName(), sourceFolder, sourceFile, importedTypes, packageDoc, comments, javaFileContent);
}
else if(bodyDeclaration instanceof RecordDeclaration) {
RecordDeclaration recordDeclaration = (RecordDeclaration)bodyDeclaration;
processRecordDeclaration(cu, recordDeclaration, umlPackage, umlClass.getName(), sourceFolder, sourceFile, importedTypes, packageDoc, comments, javaFileContent);
}
}
return map;
}
private void processTypeDeclaration(CompilationUnit cu, TypeDeclaration typeDeclaration, UMLPackage umlPackage, String packageName, String sourceFolder, String sourceFile,
List<UMLImport> importedTypes, UMLJavadoc packageDoc, List<UMLComment> comments, String javaFileContent) {
UMLJavadoc javadoc = generateJavadoc(cu, typeDeclaration, sourceFolder, sourceFile, javaFileContent);
if(generatedCode(javadoc)) {
return;
}
String className = typeDeclaration.getName().getFullyQualifiedName();
if(className.equals("$")) {
return;
}
LocationInfo locationInfo = generateLocationInfo(cu, sourceFolder, sourceFile, typeDeclaration, CodeElementType.TYPE_DECLARATION);
UMLClass umlClass = new UMLClass(packageName, className, locationInfo, typeDeclaration.isPackageMemberTypeDeclaration(), importedTypes);
umlClass.setJavadoc(javadoc);
if(typeDeclaration.isPackageMemberTypeDeclaration()) {
umlClass.setPackageDeclaration(umlPackage);
umlClass.setPackageDeclarationJavadoc(packageDoc);
for(UMLComment comment : comments) {
if(comment.getLocationInfo().before(locationInfo) && !locationInfo.nextLine(comment.getLocationInfo())) {
umlClass.getPackageDeclarationComments().add(comment);
}
}
comments.removeAll(umlClass.getPackageDeclarationComments());
}
if(typeDeclaration.isInterface()) {
umlClass.setInterface(true);
}
int startSignatureOffset = processModifiers(cu, sourceFolder, sourceFile, typeDeclaration, umlClass, javaFileContent);
if(startSignatureOffset == -1) {
startSignatureOffset = typeDeclaration.getName().getStartPosition();
}
List<TypeParameter> typeParameters = typeDeclaration.typeParameters();
for(TypeParameter typeParameter : typeParameters) {
UMLTypeParameter umlTypeParameter = new UMLTypeParameter(typeParameter.getName().getFullyQualifiedName(),
generateLocationInfo(cu, sourceFolder, sourceFile, typeParameter, CodeElementType.TYPE_PARAMETER));
List<Type> typeBounds = typeParameter.typeBounds();
for(Type type : typeBounds) {
umlTypeParameter.addTypeBound(UMLType.extractTypeObject(cu, sourceFolder, sourceFile, type, 0, javaFileContent));
}
List<IExtendedModifier> typeParameterExtendedModifiers = typeParameter.modifiers();
for(IExtendedModifier extendedModifier : typeParameterExtendedModifiers) {
if(extendedModifier.isAnnotation()) {
Annotation annotation = (Annotation)extendedModifier;
umlTypeParameter.addAnnotation(new UMLAnnotation(cu, sourceFolder, sourceFile, annotation, javaFileContent));
}
}
umlClass.addTypeParameter(umlTypeParameter);
}
Type superclassType = typeDeclaration.getSuperclassType();
if(superclassType != null) {
UMLType umlType = UMLType.extractTypeObject(cu, sourceFolder, sourceFile, superclassType, 0, javaFileContent);
UMLGeneralization umlGeneralization = new UMLGeneralization(umlClass, umlType.getClassType());
umlClass.setSuperclass(umlType);
getUmlModel().addGeneralization(umlGeneralization);
}
List<Type> superInterfaceTypes = typeDeclaration.superInterfaceTypes();
for(Type interfaceType : superInterfaceTypes) {
UMLType umlType = UMLType.extractTypeObject(cu, sourceFolder, sourceFile, interfaceType, 0, javaFileContent);
UMLRealization umlRealization = new UMLRealization(umlClass, umlType.getClassType());
umlClass.addImplementedInterface(umlType);
getUmlModel().addRealization(umlRealization);
}
List<Type> permittedTypes = typeDeclaration.permittedTypes();
for(Type type : permittedTypes) {
UMLType umlType = UMLType.extractTypeObject(cu, sourceFolder, sourceFile, type, 0, javaFileContent);
umlClass.addPermittedType(umlType);
}
Map<BodyDeclaration, VariableDeclarationContainer> map = processBodyDeclarations(cu, typeDeclaration, umlPackage, packageName, sourceFolder, sourceFile, importedTypes, umlClass, packageDoc, comments, javaFileContent);
processAnonymousClassDeclarations(cu, typeDeclaration, umlPackage, packageName, sourceFolder, sourceFile, className, importedTypes, packageDoc, comments, umlClass, javaFileContent);
for(BodyDeclaration declaration : map.keySet()) {
if(declaration instanceof MethodDeclaration) {
UMLOperation operation = (UMLOperation) map.get(declaration);
processMethodBody(cu, sourceFolder, sourceFile, (MethodDeclaration) declaration, operation, umlClass.getAttributes(), javaFileContent);
}
else if(declaration instanceof Initializer) {
UMLInitializer initializer = (UMLInitializer) map.get(declaration);
processInitializerBody(cu, sourceFolder, sourceFile, (Initializer) declaration, initializer, umlClass.getAttributes(), javaFileContent);
}
}
int endSignatureOffset = typeDeclaration.bodyDeclarations().size() > 0 ?
((BodyDeclaration)typeDeclaration.bodyDeclarations().get(0)).getStartPosition() :
typeDeclaration.getStartPosition() + typeDeclaration.getLength();
String text = javaFileContent.substring(startSignatureOffset, endSignatureOffset);
if(text.contains("{"))
text = text.substring(0, text.indexOf("{") + 1);
if(!text.contains("class ") && !text.contains("interface ")) {
if(typeDeclaration.isInterface()) {
umlClass.setActualSignature("interface " + text);
}
else {
umlClass.setActualSignature("class " + text);
}
}
else {
umlClass.setActualSignature(text);
}
this.getUmlModel().addClass(umlClass);
distributeComments(comments, locationInfo, umlClass.getComments());
}
private void processAnonymousClassDeclarations(CompilationUnit cu, AbstractTypeDeclaration typeDeclaration,
UMLPackage umlPackage, String packageName, String sourceFolder, String sourceFile, String className,
List<UMLImport> importedTypes, UMLJavadoc packageDoc, List<UMLComment> allComments, UMLClass umlClass, String javaFileContent) {
AnonymousClassDeclarationVisitor visitor = new AnonymousClassDeclarationVisitor();
typeDeclaration.accept(visitor);
Set<AnonymousClassDeclaration> anonymousClassDeclarations = visitor.getAnonymousClassDeclarations();
Set<TypeDeclarationStatement> typeDeclarationStatements = visitor.getTypeDeclarationStatements();
for(TypeDeclarationStatement statement : typeDeclarationStatements) {
String methodNamePath = getMethodNamePath(statement);
String fullName = packageName + "." + className + "." + methodNamePath;
AbstractTypeDeclaration localTypeDeclaration = statement.getDeclaration();
LocationInfo location = generateLocationInfo(cu, sourceFolder, sourceFile, localTypeDeclaration, CodeElementType.TYPE_DECLARATION);
for(UMLOperation operation : umlClass.getOperations()) {
if(operation.getLocationInfo().subsumes(location)) {
for(UMLComment comment : operation.getComments()) {
if(location.subsumes(comment.getLocationInfo())) {
allComments.add(comment);
}
}
}
}
if(localTypeDeclaration instanceof TypeDeclaration) {
TypeDeclaration typeDeclaration2 = (TypeDeclaration)localTypeDeclaration;
processTypeDeclaration(cu, typeDeclaration2, umlPackage, fullName, sourceFolder, sourceFile, importedTypes, packageDoc, allComments, javaFileContent);
}
else if(localTypeDeclaration instanceof EnumDeclaration) {
EnumDeclaration enumDeclaration = (EnumDeclaration)localTypeDeclaration;
processEnumDeclaration(cu, enumDeclaration, umlPackage, fullName, sourceFolder, sourceFile, importedTypes, packageDoc, allComments, javaFileContent);
}
else if(localTypeDeclaration instanceof AnnotationTypeDeclaration) {
AnnotationTypeDeclaration annotationDeclaration = (AnnotationTypeDeclaration)localTypeDeclaration;
processAnnotationTypeDeclaration(cu, annotationDeclaration, umlPackage, fullName, sourceFolder, sourceFile, importedTypes, packageDoc, allComments, javaFileContent);
}
else if(localTypeDeclaration instanceof RecordDeclaration) {
RecordDeclaration recordDeclaration = (RecordDeclaration)localTypeDeclaration;
processRecordDeclaration(cu, recordDeclaration, umlPackage, fullName, sourceFolder, sourceFile, importedTypes, packageDoc, allComments, javaFileContent);
}
}
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
for(AnonymousClassDeclaration anonymous : anonymousClassDeclarations) {
insertNode(anonymous, root);
}
List<UMLAnonymousClass> createdAnonymousClasses = new ArrayList<UMLAnonymousClass>();
Enumeration enumeration = root.postorderEnumeration();
while(enumeration.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)enumeration.nextElement();
if(node.getUserObject() != null) {
AnonymousClassDeclaration anonymous = (AnonymousClassDeclaration)node.getUserObject();
boolean operationFound = false;
boolean attributeFound = false;
boolean initializerFound = false;
UMLOperation matchingOperation = null;
UMLAttribute matchingAttribute = null;
UMLInitializer matchingInitializer = null;
UMLEnumConstant matchingEnumConstant = null;
List<UMLComment> comments = null;
for(UMLOperation operation : umlClass.getOperations()) {
if(operation.getLocationInfo().getStartOffset() <= anonymous.getStartPosition() &&
operation.getLocationInfo().getEndOffset() >= anonymous.getStartPosition()+anonymous.getLength()) {
comments = operation.getComments();
operationFound = true;
matchingOperation = operation;
break;
}
}
if(!operationFound) {
for(UMLAttribute attribute : umlClass.getAttributes()) {
if(attribute.getLocationInfo().getStartOffset() <= anonymous.getStartPosition() &&
attribute.getLocationInfo().getEndOffset() >= anonymous.getStartPosition()+anonymous.getLength()) {
comments = attribute.getComments();
attributeFound = true;
matchingAttribute = attribute;
break;
}
}
}
if(!operationFound && !attributeFound) {
for(UMLInitializer initializer : umlClass.getInitializers()) {
if(initializer.getLocationInfo().getStartOffset() <= anonymous.getStartPosition() &&
initializer.getLocationInfo().getEndOffset() >= anonymous.getStartPosition()+anonymous.getLength()) {
comments = initializer.getComments();
initializerFound = true;
matchingInitializer = initializer;
break;
}
}
}
if(!operationFound && !attributeFound && !initializerFound) {
for(UMLEnumConstant enumConstant : umlClass.getEnumConstants()) {
if(enumConstant.getLocationInfo().getStartOffset() <= anonymous.getStartPosition() &&
enumConstant.getLocationInfo().getEndOffset() >= anonymous.getStartPosition()+anonymous.getLength()) {
comments = enumConstant.getComments();
matchingEnumConstant = enumConstant;
break;
}
}
}
if(matchingOperation != null || matchingAttribute != null || matchingInitializer != null || matchingEnumConstant != null) {
String anonymousBinaryName = getAnonymousBinaryName(node);
String anonymousCodePath = getAnonymousCodePath(node);
UMLAnonymousClass anonymousClass = processAnonymousClassDeclaration(cu, anonymous, umlPackage, packageName + "." + className, anonymousBinaryName, anonymousCodePath, sourceFolder, sourceFile, packageDoc, comments, umlClass.getImportedTypes(), javaFileContent);
umlClass.addAnonymousClass(anonymousClass);
if(matchingOperation != null) {
matchingOperation.addAnonymousClass(anonymousClass);
anonymousClass.addParentContainer(matchingOperation);
}
if(matchingAttribute != null) {
matchingAttribute.addAnonymousClass(anonymousClass);
anonymousClass.addParentContainer(matchingAttribute);
}
if(matchingInitializer != null) {
matchingInitializer.addAnonymousClass(anonymousClass);
anonymousClass.addParentContainer(matchingInitializer);
}
if(matchingEnumConstant != null) {
matchingEnumConstant.addAnonymousClass(anonymousClass);
anonymousClass.addParentContainer(matchingEnumConstant);
}
for(UMLOperation operation : anonymousClass.getOperations()) {
for(UMLAnonymousClass createdAnonymousClass : createdAnonymousClasses) {
if(operation.getLocationInfo().subsumes(createdAnonymousClass.getLocationInfo())) {
operation.addAnonymousClass(createdAnonymousClass);
createdAnonymousClass.addParentContainer(operation);
}
}
}
for(UMLAttribute attribute : anonymousClass.getAttributes()) {
for(UMLAnonymousClass createdAnonymousClass : createdAnonymousClasses) {
if(attribute.getLocationInfo().subsumes(createdAnonymousClass.getLocationInfo())) {
attribute.addAnonymousClass(createdAnonymousClass);
createdAnonymousClass.addParentContainer(attribute);
}
}
}
for(UMLInitializer initializer : anonymousClass.getInitializers()) {
for(UMLAnonymousClass createdAnonymousClass : createdAnonymousClasses) {
if(initializer.getLocationInfo().subsumes(createdAnonymousClass.getLocationInfo())) {
initializer.addAnonymousClass(createdAnonymousClass);
createdAnonymousClass.addParentContainer(initializer);
}
}
}
for(UMLEnumConstant enumConstant : anonymousClass.getEnumConstants()) {
for(UMLAnonymousClass createdAnonymousClass : createdAnonymousClasses) {
if(enumConstant.getLocationInfo().subsumes(createdAnonymousClass.getLocationInfo())) {
enumConstant.addAnonymousClass(createdAnonymousClass);
createdAnonymousClass.addParentContainer(enumConstant);
}
}
}
createdAnonymousClasses.add(anonymousClass);
List<BodyDeclaration> bodyDeclarations = anonymous.bodyDeclarations();
int i=0;
int j=0;
for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
if(bodyDeclaration instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration)bodyDeclaration;
UMLOperation operation = anonymousClass.getOperations().get(i);
processMethodBody(cu, sourceFolder, sourceFile, methodDeclaration, operation, umlClass.getAttributes(), javaFileContent);
i++;
}
else if(bodyDeclaration instanceof Initializer) {
Initializer initializer = (Initializer)bodyDeclaration;
UMLInitializer umlInitializer = anonymousClass.getInitializers().get(j);
processInitializerBody(cu, sourceFolder, sourceFile, initializer, umlInitializer, umlClass.getAttributes(), javaFileContent);
j++;
}
}
}
}
}
}
private void processMethodBody(CompilationUnit cu, String sourceFolder, String sourceFile, MethodDeclaration methodDeclaration, UMLOperation operation, List<UMLAttribute> attributes, String javaFileContent) {
Block block = methodDeclaration.getBody();
if(block != null) {
OperationBody body = new OperationBody(cu, sourceFolder, sourceFile, block, operation, attributes, javaFileContent);
operation.setBody(body);
}
else {
operation.setBody(null);
}
}
private void processInitializerBody(CompilationUnit cu, String sourceFolder, String sourceFile, Initializer initializer, UMLInitializer umlInitializer, List<UMLAttribute> attributes, String javaFileContent) {
Block block = initializer.getBody();
if(block != null) {
OperationBody body = new OperationBody(cu, sourceFolder, sourceFile, block, umlInitializer, attributes, javaFileContent);
umlInitializer.setBody(body);
}
else {
umlInitializer.setBody(null);
}
}
private int processModifiers(CompilationUnit cu, String sourceFolder, String sourceFile, AbstractTypeDeclaration typeDeclaration, UMLClass umlClass, String javaFileContent) {
int startSignatureOffset = -1;
int modifiers = typeDeclaration.getModifiers();
if((modifiers & Modifier.ABSTRACT) != 0)
umlClass.setAbstract(true);
if((modifiers & Modifier.STATIC) != 0)
umlClass.setStatic(true);
if((modifiers & Modifier.FINAL) != 0)
umlClass.setFinal(true);
if((modifiers & Modifier.SEALED) != 0)
umlClass.setSealed(true);
if((modifiers & Modifier.PUBLIC) != 0)
umlClass.setVisibility(Visibility.PUBLIC);
else if((modifiers & Modifier.PROTECTED) != 0)
umlClass.setVisibility(Visibility.PROTECTED);
else if((modifiers & Modifier.PRIVATE) != 0)
umlClass.setVisibility(Visibility.PRIVATE);
else
umlClass.setVisibility(Visibility.PACKAGE);
List<IExtendedModifier> extendedModifiers = typeDeclaration.modifiers();
for(IExtendedModifier extendedModifier : extendedModifiers) {
if(extendedModifier.isAnnotation()) {
Annotation annotation = (Annotation)extendedModifier;
umlClass.addAnnotation(new UMLAnnotation(cu, sourceFolder, sourceFile, annotation, javaFileContent));
}
else if(extendedModifier.isModifier()) {
Modifier modifier = (Modifier)extendedModifier;
umlClass.addModifier(new UMLModifier(cu, sourceFolder, sourceFile, modifier));
if(startSignatureOffset == -1) {
startSignatureOffset = modifier.getStartPosition();
}
}
}
return startSignatureOffset;
}
private UMLInitializer processInitializer(CompilationUnit cu, Initializer initializer, String packageName, boolean isInterfaceMethod, String sourceFolder, String sourceFile, List<UMLComment> comments, String javaFileContent) {
UMLJavadoc javadoc = generateJavadoc(cu, initializer, sourceFolder, sourceFile, javaFileContent);
String name = "";
if(initializer.getParent() instanceof AnonymousClassDeclaration && initializer.getParent().getParent() instanceof ClassInstanceCreation) {
ClassInstanceCreation creation = (ClassInstanceCreation)initializer.getParent().getParent();
name = stringify(creation.getType());
}
else if(initializer.getParent() instanceof AbstractTypeDeclaration) {
AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration)initializer.getParent();
name = typeDeclaration.getName().getIdentifier();
}