-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathClassGraph.java
964 lines (873 loc) · 33.5 KB
/
ClassGraph.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
/*
* Create a graphviz graph based on the classes in the specified java
* source files.
*
* (C) Copyright 2002-2005 Diomidis Spinellis
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*
*/
package org.umlgraph.doclet;
import static org.umlgraph.doclet.StringUtil.buildRelativePathFromClassNames;
import static org.umlgraph.doclet.StringUtil.escape;
import static org.umlgraph.doclet.StringUtil.fmt;
import static org.umlgraph.doclet.StringUtil.guilWrap;
import static org.umlgraph.doclet.StringUtil.guillemize;
import static org.umlgraph.doclet.StringUtil.htmlNewline;
import static org.umlgraph.doclet.StringUtil.removeTemplate;
import static org.umlgraph.doclet.StringUtil.splitPackageClass;
import static org.umlgraph.doclet.StringUtil.tokenize;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.ConstructorDoc;
import com.sun.javadoc.Doc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.PackageDoc;
import com.sun.javadoc.Parameter;
import com.sun.javadoc.ParameterizedType;
import com.sun.javadoc.ProgramElementDoc;
import com.sun.javadoc.RootDoc;
import com.sun.javadoc.Tag;
import com.sun.javadoc.Type;
import com.sun.javadoc.TypeVariable;
import com.sun.javadoc.WildcardType;
/**
* Class graph generation engine
* @depend - - - StringUtil
* @depend - - - Options
* @composed - - * ClassInfo
* @has - - - OptionProvider
*
* @version $Revision$
* @author <a href="http://www.spinellis.gr">Diomidis Spinellis</a>
*/
class ClassGraph {
enum Align {
LEFT, CENTER, RIGHT;
public final String lower;
private Align() {
this.lower = toString().toLowerCase();
}
};
protected Map<String, ClassInfo> classnames = new HashMap<String, ClassInfo>();
protected Set<String> rootClasses;
protected Map<String, ClassDoc> rootClassdocs = new HashMap<String, ClassDoc>();
protected OptionProvider optionProvider;
protected PrintWriter w;
protected ClassDoc collectionClassDoc;
protected ClassDoc mapClassDoc;
protected String linePostfix;
protected String linePrefix;
// used only when generating context class diagrams in UMLDoc, to generate the proper
// relative links to other classes in the image map
protected final String contextPackageName;
/**
* Create a new ClassGraph. <p>The packages passed as an
* argument are the ones specified on the command line.</p>
* <p>Local URLs will be generated for these packages.</p>
* @param root The root of docs as provided by the javadoc API
* @param optionProvider The main option provider
* @param contextDoc The current context for generating relative links, may be a ClassDoc
* or a PackageDoc (used by UMLDoc)
*/
public ClassGraph(RootDoc root, OptionProvider optionProvider, Doc contextDoc) {
this.optionProvider = optionProvider;
this.collectionClassDoc = root.classNamed("java.util.Collection");
this.mapClassDoc = root.classNamed("java.util.Map");
// to gather the packages containing specified classes, loop thru them and gather
// package definitions. User root.specifiedPackages is not safe, since the user
// may specify just a list of classes (human users usually don't, but automated tools do)
rootClasses = new HashSet<String>();
for (ClassDoc classDoc : root.classes()) {
rootClasses.add(classDoc.qualifiedName());
rootClassdocs.put(classDoc.qualifiedName(), classDoc);
}
// determine the context path, relative to the root
if (contextDoc instanceof ClassDoc)
contextPackageName = ((ClassDoc) contextDoc).containingPackage().name();
else if (contextDoc instanceof PackageDoc)
contextPackageName = ((PackageDoc) contextDoc).name();
else
contextPackageName = null; // Not available
Options opt = optionProvider.getGlobalOptions();
linePrefix = opt.compact ? "" : "\t";
linePostfix = opt.compact ? "" : "\n";
}
/** Return the class's name, possibly by stripping the leading path */
private static String qualifiedName(Options opt, String r) {
if (opt.hideGenerics)
r = removeTemplate(r);
// Fast path - nothing to do:
if (opt.showQualified && (opt.showQualifiedGenerics || r.indexOf('<') < 0))
return r;
StringBuilder buf = new StringBuilder(r.length());
qualifiedNameInner(opt, r, buf, 0, !opt.showQualified);
return buf.toString();
}
private static int qualifiedNameInner(Options opt, String r, StringBuilder buf, int last, boolean strip) {
strip = strip && last < r.length() && Character.isLowerCase(r.charAt(last));
for (int i = last; i < r.length(); i++) {
char c = r.charAt(i);
if (c == '.' || c == '$') {
if (strip)
last = i + 1; // skip dot
strip = strip && last < r.length() && Character.isLowerCase(r.charAt(last));
continue;
}
if (Character.isJavaIdentifierPart(c))
continue;
buf.append(r, last, i);
last = i;
// Handle nesting of generics
if (c == '<') {
buf.append('<');
i = last = qualifiedNameInner(opt, r, buf, ++last, !opt.showQualifiedGenerics);
buf.append('>');
} else if (c == '>')
return i + 1;
}
buf.append(r, last, r.length());
return r.length();
}
/**
* Print the visibility adornment of element e prefixed by
* any stereotypes
*/
private String visibility(Options opt, ProgramElementDoc e) {
return opt.showVisibility ? Visibility.get(e).symbol : " ";
}
/** Print the method parameter p */
private String parameter(Options opt, Parameter p[]) {
StringBuilder par = new StringBuilder(1000);
for (int i = 0; i < p.length; i++) {
par.append(p[i].name() + typeAnnotation(opt, p[i].type()));
if (i + 1 < p.length)
par.append(", ");
}
return par.toString();
}
/** Print a a basic type t */
private String type(Options opt, Type t, boolean generics) {
return ((generics ? opt.showQualifiedGenerics : opt.showQualified) ? //
t.qualifiedTypeName() : t.typeName()) //
+ (opt.hideGenerics ? "" : typeParameters(opt, t.asParameterizedType()));
}
/** Print the parameters of the parameterized type t */
private String typeParameters(Options opt, ParameterizedType t) {
if (t == null)
return "";
StringBuilder tp = new StringBuilder(1000).append("<");
Type args[] = t.typeArguments();
for (int i = 0; i < args.length; i++) {
tp.append(type(opt, args[i], true));
if (i != args.length - 1)
tp.append(", ");
}
return tp.append(">").toString();
}
/** Annotate an field/argument with its type t */
private String typeAnnotation(Options opt, Type t) {
if (t.typeName().equals("void"))
return "";
return " : " + type(opt, t, false) + t.dimension();
}
/** Print the class's attributes fd */
private void attributes(Options opt, FieldDoc fd[]) {
for (FieldDoc f : fd) {
if (hidden(f))
continue;
stereotype(opt, f, Align.LEFT);
String att = visibility(opt, f) + f.name();
if (opt.showType)
att += typeAnnotation(opt, f.type());
tableLine(Align.LEFT, att);
tagvalue(opt, f);
}
}
/*
* The following two methods look similar, but can't
* be refactored into one, because their common interface,
* ExecutableMemberDoc, doesn't support returnType for ctors.
*/
/** Print the class's constructors m */
private boolean operations(Options opt, ConstructorDoc m[]) {
boolean printed = false;
for (ConstructorDoc cd : m) {
if (hidden(cd))
continue;
stereotype(opt, cd, Align.LEFT);
String cs = visibility(opt, cd) + cd.name() //
+ (opt.showType ? "(" + parameter(opt, cd.parameters()) + ")" : "()");
tableLine(Align.LEFT, cs);
tagvalue(opt, cd);
printed = true;
}
return printed;
}
/** Print the class's operations m */
private boolean operations(Options opt, MethodDoc m[]) {
boolean printed = false;
for (MethodDoc md : m) {
if (hidden(md))
continue;
// Filter-out static initializer method
if (md.name().equals("<clinit>") && md.isStatic() && md.isPackagePrivate())
continue;
stereotype(opt, md, Align.LEFT);
String op = visibility(opt, md) + md.name() + //
(opt.showType ? "(" + parameter(opt, md.parameters()) + ")" + typeAnnotation(opt, md.returnType())
: "()");
tableLine(Align.LEFT, (md.isAbstract() ? Font.ABSTRACT : Font.NORMAL).wrap(opt, op));
printed = true;
tagvalue(opt, md);
}
return printed;
}
/** Print the common class node's properties */
private void nodeProperties(Options opt) {
Options def = opt.getGlobalOptions();
if (opt.nodeFontName != def.nodeFontName)
w.print(",fontname=\"" + opt.nodeFontName + "\"");
if (opt.nodeFontColor != def.nodeFontColor)
w.print(",fontcolor=\"" + opt.nodeFontColor + "\"");
if (opt.nodeFontSize != def.nodeFontSize)
w.print(",fontsize=" + fmt(opt.nodeFontSize));
w.print(opt.shape.style);
w.println("];");
}
/**
* Return as a string the tagged values associated with c
* @param opt the Options used to guess font names
* @param c the Doc entry to look for @tagvalue
* @param prevterm the termination string for the previous element
* @param term the termination character for each tagged value
*/
private void tagvalue(Options opt, Doc c) {
Tag tags[] = c.tags("tagvalue");
if (tags.length == 0)
return;
for (Tag tag : tags) {
String t[] = tokenize(tag.text());
if (t.length != 2) {
System.err.println("@tagvalue expects two fields: " + tag.text());
continue;
}
tableLine(Align.RIGHT, Font.TAG.wrap(opt, "{" + t[0] + " = " + t[1] + "}"));
}
}
/**
* Return as a string the stereotypes associated with c
* terminated by the escape character term
*/
private void stereotype(Options opt, Doc c, Align align) {
for (Tag tag : c.tags("stereotype")) {
String t[] = tokenize(tag.text());
if (t.length != 1) {
System.err.println("@stereotype expects one field: " + tag.text());
continue;
}
tableLine(align, guilWrap(opt, t[0]));
}
}
/** Return true if c has a @hidden tag associated with it */
private boolean hidden(ProgramElementDoc c) {
if (c.tags("hidden").length > 0 || c.tags("view").length > 0)
return true;
Options opt = optionProvider.getOptionsFor(c instanceof ClassDoc ? (ClassDoc) c : c.containingClass());
return opt.matchesHideExpression(c.toString()) //
|| (opt.hidePrivateInner && c instanceof ClassDoc && c.isPrivate() && ((ClassDoc) c).containingClass() != null);
}
protected ClassInfo getClassInfo(ClassDoc cd, boolean create) {
return getClassInfo(cd, cd.toString(), create);
}
protected ClassInfo getClassInfo(String className, boolean create) {
return getClassInfo(null, className, create);
}
protected ClassInfo getClassInfo(ClassDoc cd, String className, boolean create) {
className = removeTemplate(className);
ClassInfo ci = classnames.get(className);
if (ci == null && create) {
boolean hidden = cd != null ? hidden(cd) : optionProvider.getOptionsFor(className).matchesHideExpression(className);
ci = new ClassInfo(hidden);
classnames.put(className, ci);
}
return ci;
}
/** Return true if the class name is associated to an hidden class or matches a hide expression */
private boolean hidden(String className) {
className = removeTemplate(className);
ClassInfo ci = classnames.get(className);
return ci != null ? ci.hidden : optionProvider.getOptionsFor(className).matchesHideExpression(className);
}
/**
* Prints the class if needed.
* <p>
* A class is a rootClass if it's included among the classes returned by
* RootDoc.classes(), this information is used to properly compute
* relative links in diagrams for UMLDoc
*/
public String printClass(ClassDoc c, boolean rootClass) {
ClassInfo ci = getClassInfo(c, true);
if(ci.nodePrinted || ci.hidden)
return ci.name;
Options opt = optionProvider.getOptionsFor(c);
if (c.isEnum() && !opt.showEnumerations)
return ci.name;
String className = c.toString();
// Associate classname's alias
w.println(linePrefix + "// " + className);
// Create label
w.print(linePrefix + ci.name + " [label=");
boolean showMembers =
(opt.showAttributes && c.fields().length > 0) ||
(c.isEnum() && opt.showEnumConstants && c.enumConstants().length > 0) ||
(opt.showOperations && c.methods().length > 0) ||
(opt.showConstructors && c.constructors().length > 0);
final String url = classToUrl(c, rootClass);
externalTableStart(opt, c.qualifiedName(), url);
firstInnerTableStart(opt);
if (c.isInterface())
tableLine(Align.CENTER, guilWrap(opt, "interface"));
if (c.isEnum())
tableLine(Align.CENTER, guilWrap(opt, "enumeration"));
stereotype(opt, c, Align.CENTER);
Font font = c.isAbstract() && !c.isInterface() ? Font.CLASS_ABSTRACT : Font.CLASS;
String qualifiedName = qualifiedName(opt, className);
int idx = splitPackageClass(qualifiedName);
if (opt.showComment)
tableLine(Align.LEFT, Font.CLASS.wrap(opt, htmlNewline(escape(c.commentText()))));
else if (opt.postfixPackage && idx > 0 && idx < (qualifiedName.length() - 1)) {
String packageName = qualifiedName.substring(0, idx);
String cn = qualifiedName.substring(idx + 1);
tableLine(Align.CENTER, font.wrap(opt, escape(cn)));
tableLine(Align.CENTER, Font.PACKAGE.wrap(opt, packageName));
} else {
tableLine(Align.CENTER, font.wrap(opt, escape(qualifiedName)));
}
tagvalue(opt, c);
firstInnerTableEnd(opt);
/*
* Warning: The boolean expressions guarding innerTableStart()
* in this block, should match those in the code block above
* marked: "Calculate the number of innerTable rows we will emmit"
*/
if (showMembers) {
if (opt.showAttributes) {
innerTableStart();
FieldDoc[] fields = c.fields();
// if there are no fields, print an empty line to generate proper HTML
if (fields.length == 0)
tableLine(Align.LEFT, "");
else
attributes(opt, c.fields());
innerTableEnd();
} else if(!c.isEnum() && (opt.showConstructors || opt.showOperations)) {
// show an emtpy box if we don't show attributes but
// we show operations
innerTableStart();
tableLine(Align.LEFT, "");
innerTableEnd();
}
if (c.isEnum() && opt.showEnumConstants) {
innerTableStart();
FieldDoc[] ecs = c.enumConstants();
// if there are no constants, print an empty line to generate proper HTML
if (ecs.length == 0) {
tableLine(Align.LEFT, "");
} else {
for (FieldDoc fd : ecs) {
tableLine(Align.LEFT, fd.name());
}
}
innerTableEnd();
}
if (!c.isEnum() && (opt.showConstructors || opt.showOperations)) {
innerTableStart();
boolean printedLines = false;
if (opt.showConstructors)
printedLines |= operations(opt, c.constructors());
if (opt.showOperations)
printedLines |= operations(opt, c.methods());
if (!printedLines)
// if there are no operations nor constructors,
// print an empty line to generate proper HTML
tableLine(Align.LEFT, "");
innerTableEnd();
}
}
externalTableEnd();
if (url != null)
w.print(", URL=\"" + url + "\"");
nodeProperties(opt);
// If needed, add a note for this node
int ni = 0;
for (Tag t : c.tags("note")) {
String noteName = "n" + ni + "c" + ci.name;
w.print(linePrefix + "// Note annotation\n");
w.print(linePrefix + noteName + " [label=");
externalTableStart(UmlGraph.getCommentOptions(), c.qualifiedName(), url);
innerTableStart();
tableLine(Align.LEFT, Font.CLASS.wrap(UmlGraph.getCommentOptions(), htmlNewline(escape(t.text()))));
innerTableEnd();
externalTableEnd();
nodeProperties(UmlGraph.getCommentOptions());
ClassInfo ci1 = getClassInfo(c, true);
w.print(linePrefix + noteName + " -> " + ci1.name + "[arrowhead=none];\n");
ni++;
}
ci.nodePrinted = true;
return ci.name;
}
/**
* Print all relations for a given's class's tag
* @param tagname the tag containing the given relation
* @param from the source class
* @param edgetype the dot edge specification
*/
private void allRelation(Options opt, RelationType rt, ClassDoc from) {
String tagname = rt.lower;
for (Tag tag : from.tags(tagname)) {
String t[] = tokenize(tag.text()); // l-src label l-dst target
t = t.length == 1 ? new String[] { "-", "-", "-", t[0] } : t; // Shorthand
if (t.length != 4) {
System.err.println("Error in " + from + "\n" + tagname + " expects four fields (l-src label l-dst target): " + tag.text());
return;
}
ClassDoc to = from.findClass(t[3]);
if (to != null) {
if(hidden(to))
continue;
relation(opt, rt, from, to, t[0], t[1], t[2]);
} else {
if(hidden(t[3]))
continue;
relation(opt, rt, from, from.toString(), to, t[3], t[0], t[1], t[2]);
}
}
}
/**
* Print the specified relation
* @param from the source class (may be null)
* @param fromName the source class's name
* @param to the destination class (may be null)
* @param toName the destination class's name
*/
private void relation(Options opt, RelationType rt, ClassDoc from, String fromName,
ClassDoc to, String toName, String tailLabel, String label, String headLabel) {
tailLabel = (tailLabel != null && !tailLabel.isEmpty()) ? ",taillabel=\"" + tailLabel + "\"" : "";
label = (label != null && !label.isEmpty()) ? ",label=\"" + guillemize(opt, label) + "\"" : "";
headLabel = (headLabel != null && !headLabel.isEmpty()) ? ",headlabel=\"" + headLabel + "\"" : "";
boolean unLabeled = tailLabel.isEmpty() && label.isEmpty() && headLabel.isEmpty();
ClassInfo ci1 = getClassInfo(from, fromName, true), ci2 = getClassInfo(to, toName, true);
String n1 = ci1.name, n2 = ci2.name;
// For ranking we need to output extends/implements backwards.
if (rt.backorder) { // Swap:
n1 = ci2.name;
n2 = ci1.name;
String tmp = tailLabel;
tailLabel = headLabel;
headLabel = tmp;
}
Options def = opt.getGlobalOptions();
// print relation
w.println(linePrefix + "// " + fromName + " " + rt.lower + " " + toName);
w.println(linePrefix + n1 + " -> " + n2 + " [" + rt.style +
(opt.edgeColor != def.edgeColor ? ",color=\"" + opt.edgeColor + "\"" : "") +
(unLabeled ? "" :
(opt.edgeFontName != def.edgeFontName ? ",fontname=\"" + opt.edgeFontName + "\"" : "") +
(opt.edgeFontColor != def.edgeFontColor ? ",fontcolor=\"" + opt.edgeFontColor + "\"" : "") +
(opt.edgeFontSize != def.edgeFontSize ? ",fontsize=" + fmt(opt.edgeFontSize) : "")) +
tailLabel + label + headLabel +
"];");
// update relation info
RelationDirection d = RelationDirection.BOTH;
if(rt == RelationType.NAVASSOC || rt == RelationType.DEPEND)
d = RelationDirection.OUT;
ci1.addRelation(toName, rt, d);
ci2.addRelation(fromName, rt, d.inverse());
}
/**
* Print the specified relation
* @param from the source class
* @param to the destination class
*/
private void relation(Options opt, RelationType rt, ClassDoc from,
ClassDoc to, String tailLabel, String label, String headLabel) {
relation(opt, rt, from, from.toString(), to, to.toString(), tailLabel, label, headLabel);
}
/** Print a class's relations */
public void printRelations(ClassDoc c) {
Options opt = optionProvider.getOptionsFor(c);
if (hidden(c) || c.name().equals("")) // avoid phantom classes, they may pop up when the source uses annotations
return;
// Print generalization (through the Java superclass)
Type s = c.superclassType();
ClassDoc sc = s != null && !s.qualifiedTypeName().equals(Object.class.getName()) ? s.asClassDoc() : null;
if (sc != null && !c.isEnum() && !hidden(sc))
relation(opt, RelationType.EXTENDS, c, sc, null, null, null);
// Print generalizations (through @extends tags)
for (Tag tag : c.tags("extends"))
if (!hidden(tag.text()))
relation(opt, RelationType.EXTENDS, c, c.findClass(tag.text()), null, null, null);
// Print realizations (Java interfaces)
for (Type iface : c.interfaceTypes()) {
ClassDoc ic = iface.asClassDoc();
if (!hidden(ic))
relation(opt, RelationType.IMPLEMENTS, c, ic, null, null, null);
}
// Print other associations
allRelation(opt, RelationType.COMPOSED, c);
allRelation(opt, RelationType.NAVCOMPOSED, c);
allRelation(opt, RelationType.HAS, c);
allRelation(opt, RelationType.NAVHAS, c);
allRelation(opt, RelationType.ASSOC, c);
allRelation(opt, RelationType.NAVASSOC, c);
allRelation(opt, RelationType.DEPEND, c);
}
/** Print classes that were parts of relationships, but not parsed by javadoc */
public void printExtraClasses(RootDoc root) {
Set<String> names = new HashSet<String>(classnames.keySet());
for(String className: names) {
ClassInfo info = getClassInfo(className, true);
if (info.nodePrinted)
continue;
ClassDoc c = root.classNamed(className);
if(c != null) {
printClass(c, false);
continue;
}
// Handle missing classes:
Options opt = optionProvider.getOptionsFor(className);
if(opt.matchesHideExpression(className))
continue;
w.println(linePrefix + "// " + className);
w.print(linePrefix + info.name + "[label=");
externalTableStart(opt, className, classToUrl(className));
innerTableStart();
String qualifiedName = qualifiedName(opt, className);
int startTemplate = qualifiedName.indexOf('<');
int idx = qualifiedName.lastIndexOf('.', startTemplate < 0 ? qualifiedName.length() - 1 : startTemplate);
if(opt.postfixPackage && idx > 0 && idx < (qualifiedName.length() - 1)) {
String packageName = qualifiedName.substring(0, idx);
String cn = qualifiedName.substring(idx + 1);
tableLine(Align.CENTER, Font.CLASS.wrap(opt, escape(cn)));
tableLine(Align.CENTER, Font.PACKAGE.wrap(opt, packageName));
} else {
tableLine(Align.CENTER, Font.CLASS.wrap(opt, escape(qualifiedName)));
}
innerTableEnd();
externalTableEnd();
if (className == null || className.length() == 0)
w.print(",URL=\"" + classToUrl(className) + "\"");
nodeProperties(opt);
}
}
/**
* Prints associations recovered from the fields of a class. An association is inferred only
* if another relation between the two classes is not already in the graph.
* @param classes
*/
public void printInferredRelations(ClassDoc c) {
// check if the source is excluded from inference
if (hidden(c))
return;
Options opt = optionProvider.getOptionsFor(c);
for (FieldDoc field : c.fields(false)) {
if(hidden(field))
continue;
// skip statics
if(field.isStatic())
continue;
// skip primitives
FieldRelationInfo fri = getFieldRelationInfo(field);
if (fri == null)
continue;
// check if the destination is excluded from inference
if (hidden(fri.cd))
continue;
// if source and dest are not already linked, add a dependency
RelationPattern rp = getClassInfo(c, true).getRelation(fri.cd.toString());
if (rp == null) {
String destAdornment = fri.multiple ? "*" : "";
relation(opt, opt.inferRelationshipType, c, fri.cd, "", "", destAdornment);
}
}
}
/**
* Returns an array representing the imported classes of c.
* Disables the deprecation warning, which is output, because the
* imported classed are an implementation detail.
*/
@SuppressWarnings( "deprecation" )
ClassDoc[] importedClasses(ClassDoc c) {
return c.importedClasses();
}
/**
* Prints dependencies recovered from the methods of a class. A
* dependency is inferred only if another relation between the two
* classes is not already in the graph.
* @param classes
*/
public void printInferredDependencies(ClassDoc c) {
if (hidden(c))
return;
Options opt = optionProvider.getOptionsFor(c);
Set<Type> types = new HashSet<Type>();
// harvest method return and parameter types
for (MethodDoc method : filterByVisibility(c.methods(false), opt.inferDependencyVisibility)) {
types.add(method.returnType());
for (Parameter parameter : method.parameters()) {
types.add(parameter.type());
}
}
// and the field types
if (!opt.inferRelationships) {
for (FieldDoc field : filterByVisibility(c.fields(false), opt.inferDependencyVisibility)) {
types.add(field.type());
}
}
// see if there are some type parameters
if (c.asParameterizedType() != null) {
ParameterizedType pt = c.asParameterizedType();
types.addAll(Arrays.asList(pt.typeArguments()));
}
// see if type parameters extend something
for(TypeVariable tv: c.typeParameters()) {
if(tv.bounds().length > 0 )
types.addAll(Arrays.asList(tv.bounds()));
}
// and finally check for explicitly imported classes (this
// assumes there are no unused imports...)
if (opt.useImports)
types.addAll(Arrays.asList(importedClasses(c)));
// compute dependencies
for (Type type : types) {
// skip primitives and type variables, as well as dependencies
// on the source class
if (type.isPrimitive() || type instanceof WildcardType || type instanceof TypeVariable
|| c.toString().equals(type.asClassDoc().toString()))
continue;
// check if the destination is excluded from inference
ClassDoc fc = type.asClassDoc();
if (hidden(fc))
continue;
// check if source and destination are in the same package and if we are allowed
// to infer dependencies between classes in the same package
if(!opt.inferDepInPackage && c.containingPackage().equals(fc.containingPackage()))
continue;
// if source and dest are not already linked, add a dependency
RelationPattern rp = getClassInfo(c, true).getRelation(fc.toString());
if (rp == null || rp.matchesOne(new RelationPattern(RelationDirection.OUT))) {
relation(opt, RelationType.DEPEND, c, fc, "", "", "");
}
}
}
/**
* Returns all program element docs that have a visibility greater or
* equal than the specified level
*/
private <T extends ProgramElementDoc> List<T> filterByVisibility(T[] docs, Visibility visibility) {
if (visibility == Visibility.PRIVATE)
return Arrays.asList(docs);
List<T> filtered = new ArrayList<T>();
for (T doc : docs) {
if (Visibility.get(doc).compareTo(visibility) > 0)
filtered.add(doc);
}
return filtered;
}
private FieldRelationInfo getFieldRelationInfo(FieldDoc field) {
Type type = field.type();
if(type.isPrimitive() || type instanceof WildcardType || type instanceof TypeVariable)
return null;
if (type.dimension().endsWith("[]")) {
return new FieldRelationInfo(type.asClassDoc(), true);
}
Options opt = optionProvider.getOptionsFor(type.asClassDoc());
if (opt.matchesCollPackageExpression(type.qualifiedTypeName())) {
Type[] argTypes = getInterfaceTypeArguments(collectionClassDoc, type);
if (argTypes != null && argTypes.length == 1 && !argTypes[0].isPrimitive())
return new FieldRelationInfo(argTypes[0].asClassDoc(), true);
argTypes = getInterfaceTypeArguments(mapClassDoc, type);
if (argTypes != null && argTypes.length == 2 && !argTypes[1].isPrimitive())
return new FieldRelationInfo(argTypes[1].asClassDoc(), true);
}
return new FieldRelationInfo(type.asClassDoc(), false);
}
private Type[] getInterfaceTypeArguments(ClassDoc iface, Type t) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
if (iface != null && iface.equals(t.asClassDoc())) {
return pt.typeArguments();
} else {
for (Type pti : pt.interfaceTypes()) {
Type[] result = getInterfaceTypeArguments(iface, pti);
if (result != null)
return result;
}
if (pt.superclassType() != null)
return getInterfaceTypeArguments(iface, pt.superclassType());
}
} else if (t instanceof ClassDoc) {
ClassDoc cd = (ClassDoc) t;
for (Type pti : cd.interfaceTypes()) {
Type[] result = getInterfaceTypeArguments(iface, pti);
if (result != null)
return result;
}
if (cd.superclassType() != null)
return getInterfaceTypeArguments(iface, cd.superclassType());
}
return null;
}
/** Convert the class name into a corresponding URL */
public String classToUrl(ClassDoc cd, boolean rootClass) {
// building relative path for context and package diagrams
if(contextPackageName != null && rootClass)
return buildRelativePathFromClassNames(contextPackageName, cd.containingPackage().name()) + cd.name() + ".html";
return classToUrl(cd.qualifiedName());
}
/** Convert the class name into a corresponding URL */
public String classToUrl(String className) {
ClassDoc classDoc = rootClassdocs.get(className);
if (classDoc != null) {
String docRoot = optionProvider.getGlobalOptions().apiDocRoot;
if (docRoot == null)
return null;
return new StringBuilder(docRoot.length() + className.length() + 10).append(docRoot) //
.append(classDoc.containingPackage().name().replace('.', '/')) //
.append('/').append(classDoc.name()).append(".html").toString();
}
String docRoot = optionProvider.getGlobalOptions().getApiDocRoot(className);
if (docRoot == null)
return null;
int split = splitPackageClass(className);
StringBuilder buf = new StringBuilder(docRoot.length() + className.length() + 10).append(docRoot);
if (split > 0) // Avoid -1, and the extra slash then.
buf.append(className.substring(0, split).replace('.', '/')).append('/');
return buf.append(className, Math.min(split + 1, className.length()), className.length()) //
.append(".html").toString();
}
/**
* Dot prologue
* @throws IOException
*/
public void prologue() throws IOException {
Options opt = optionProvider.getGlobalOptions();
OutputStream os;
if (opt.outputFileName.equals("-"))
os = System.out;
else {
// prepare output file. Use the output file name as a full path unless the output
// directory is specified
File file = new File(opt.outputDirectory, opt.outputFileName);
// make sure the output directory are there, otherwise create them
if (file.getParentFile() != null
&& !file.getParentFile().exists())
file.getParentFile().mkdirs();
os = new FileOutputStream(file);
}
// print prologue
w = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(os), opt.outputEncoding));
w.println(
"#!/usr/local/bin/dot\n" +
"#\n" +
"# Class diagram \n" +
"# Generated by UMLGraph version " +
Version.VERSION + " (http://www.spinellis.gr/umlgraph/)\n" +
"#\n\n" +
"digraph G {\n" +
linePrefix + "graph [fontnames=\"svg\"]\n" +
linePrefix + "edge [fontname=\"" + opt.edgeFontName +
"\",fontsize=" + fmt(opt.edgeFontSize) +
",labelfontname=\"" + opt.edgeFontName +
"\",labelfontsize=" + fmt(opt.edgeFontSize) +
",color=\"" + opt.edgeColor + "\"];\n" +
linePrefix + "node [fontname=\"" + opt.nodeFontName +
"\",fontcolor=\"" + opt.nodeFontColor +
"\",fontsize=" + fmt(opt.nodeFontSize) +
",shape=plaintext,margin=0,width=0,height=0];"
);
w.println(linePrefix + "nodesep=" + opt.nodeSep + ";");
w.println(linePrefix + "ranksep=" + opt.rankSep + ";");
if (opt.horizontal)
w.println(linePrefix + "rankdir=LR;");
if (opt.bgColor != null)
w.println(linePrefix + "bgcolor=\"" + opt.bgColor + "\";\n");
}
/** Dot epilogue */
public void epilogue() {
w.println("}\n");
w.flush();
w.close();
}
private void externalTableStart(Options opt, String name, String url) {
String bgcolor = opt.nodeFillColor == null ? "" : (" bgcolor=\"" + opt.nodeFillColor + "\"");
String href = url == null ? "" : (" href=\"" + url + "\" target=\"_parent\"");
w.print("<<table title=\"" + name + "\" border=\"0\" cellborder=\"" +
opt.shape.cellBorder() + "\" cellspacing=\"0\" " +
"cellpadding=\"2\"" + bgcolor + href + ">" + linePostfix);
}
private void externalTableEnd() {
w.print(linePrefix + linePrefix + "</table>>");
}
private void innerTableStart() {
w.print(linePrefix + linePrefix + "<tr><td><table border=\"0\" cellspacing=\"0\" "
+ "cellpadding=\"1\">" + linePostfix);
}
/**
* Start the first inner table of a class.
*/
private void firstInnerTableStart(Options opt) {
w.print(linePrefix + linePrefix + "<tr>" + opt.shape.extraColumn() +
"<td><table border=\"0\" cellspacing=\"0\" " +
"cellpadding=\"1\">" + linePostfix);
}
private void innerTableEnd() {
w.print(linePrefix + linePrefix + "</table></td></tr>" + linePostfix);
}
/**
* End the first inner table of a class.
*/
private void firstInnerTableEnd(Options opt) {
w.print(linePrefix + linePrefix + "</table></td>" +
opt.shape.extraColumn() + "</tr>" + linePostfix);
}
private void tableLine(Align align, String text) {
w.print(linePrefix + linePrefix //
+ "<tr><td align=\"" + align.lower + "\" balign=\"" + align.lower + "\"> " //
+ text // MAY contain markup!
+ " </td></tr>" + linePostfix);
}
private static class FieldRelationInfo {
ClassDoc cd;
boolean multiple;
public FieldRelationInfo(ClassDoc cd, boolean multiple) {
this.cd = cd;
this.multiple = multiple;
}
}
}