1
1
package com .github .juanmf .java2plant .render ;
2
2
3
3
import com .github .juanmf .java2plant .Parser ;
4
+ import com .github .juanmf .java2plant .structure .Aggregation ;
5
+ import com .github .juanmf .java2plant .structure .Extension ;
4
6
import com .github .juanmf .java2plant .structure .Relation ;
7
+ import com .github .juanmf .java2plant .structure .Use ;
5
8
9
+ import java .util .HashMap ;
10
+ import java .util .HashSet ;
11
+ import java .util .Iterator ;
12
+ import java .util .Map ;
6
13
import java .util .Set ;
14
+ import java .util .regex .Matcher ;
15
+ import java .util .regex .Pattern ;
7
16
8
17
/**
9
18
10
19
*/
11
20
public class PlantRenderer {
12
21
private final Set <String > types ;
13
22
private final Set <Relation > relations ;
23
+ private final Set <Pattern > toTypesToShowAsMember ;
14
24
private final Filter filter ;
25
+ private static final Map <Class <? extends Relation >, MemberPrinter > memberPrinters = new HashMap <>();
26
+
27
+ static {
28
+ memberPrinters .put (Use .class , new MethodPrinter ());
29
+ memberPrinters .put (Aggregation .class , new FieldPrinter ());
30
+ memberPrinters .put (Extension .class , new NullPrinter ());
31
+ }
15
32
16
33
public PlantRenderer (Set <String > types , Set <Relation > relations ) {
17
34
this (types , relations , Filters .FILTER_ALLOW_ALL );
@@ -21,7 +38,8 @@ public PlantRenderer(Set<String> types, Set<Relation> relations, Filter filter)
21
38
this .types = types ;
22
39
this .relations = relations ;
23
40
this .filter = filter ;
24
-
41
+ toTypesToShowAsMember = new HashSet <>();
42
+ toTypesToShowAsMember .add (Pattern .compile ("^java.lang.*" ));
25
43
}
26
44
27
45
/**
@@ -37,6 +55,8 @@ public String render() {
37
55
StringBuilder sb = new StringBuilder ();
38
56
sb .append ("@startuml\n " );
39
57
sb .
append (
"' Created by [email protected] \n \n " );
58
+ sb .append ("' Using left to right direction to try a better layout feel free to edit\n " );
59
+ sb .append ("left to right direction\n " );
40
60
sb .append ("' Participants \n \n " );
41
61
addClasses (sb );
42
62
sb .append ("\n ' Relations \n \n " );
@@ -56,6 +76,9 @@ protected void addRelations(StringBuilder sb) {
56
76
if (filter .isForbidenRelation (r .getClass ())) {
57
77
continue ;
58
78
}
79
+ if (r .getFromType ().contains ("$" )) {
80
+ continue ;
81
+ }
59
82
sb .append (r .toString ()).append ("\n " );
60
83
}
61
84
}
@@ -69,11 +92,79 @@ protected void addClasses(StringBuilder sb) {
69
92
for (String c : types ) {
70
93
try {
71
94
Class <?> aClass = Class .forName (c , true , Parser .CLASS_LOADER );
72
- sb . append ( aClass . toString ()). append ( " \n " );
95
+ addClass ( sb , aClass );
73
96
} catch (ClassNotFoundException e ) {
74
97
System .out .println ("ClassNotFoundException: " + e .getMessage ());
75
98
continue ;
76
99
}
77
100
}
78
101
}
102
+
103
+ protected void addClass (StringBuilder sb , Class <?> aClass ) {
104
+ if (aClass .getName ().contains ("$" )) {
105
+ return ;
106
+ }
107
+ sb .append (aClass .toString ()).append (" {\n " );
108
+ renderClassMembers (sb , aClass );
109
+ sb .append ("\n } \n " );
110
+ }
111
+
112
+ private void renderClassMembers (StringBuilder sb , Class <?> aClass ) {
113
+ Iterator <Relation > ri = relations .iterator ();
114
+ Set <String > fields = new HashSet <>();
115
+ Set <String > methods = new HashSet <>();
116
+ while (ri .hasNext ()) {
117
+ Relation relation = ri .next ();
118
+ if (relation .getFromType ().equals (aClass .getName ())
119
+ && matches (relation .getToType (), toTypesToShowAsMember )) {
120
+ System .out .println (String .format ("%s has a relation to %s to be shown as member" , aClass .getName (), relation .getToType ()));
121
+ memberPrinters .get (relation .getClass ()).addMember (relation , fields , methods );
122
+ ri .remove ();
123
+ }
124
+ }
125
+ for (String field : fields ) {
126
+ sb .append (field + "\n " );
127
+ }
128
+ sb .append ("--\n " );
129
+ for (String method : methods ) {
130
+ sb .append (method + "\n " );
131
+ }
132
+ }
133
+
134
+ private boolean matches (String toType , Set <Pattern > toTypesToShowAsAttrs ) {
135
+ for (Pattern pattern : toTypesToShowAsAttrs ) {
136
+ Matcher m = pattern .matcher (toType );
137
+ if (m .matches ()) {
138
+ return true ;
139
+ }
140
+ }
141
+ return false ;
142
+ }
143
+
144
+ interface MemberPrinter {
145
+ void addMember (Relation r , Set <String > fields , Set <String > methods );
146
+ }
147
+
148
+ static class FieldPrinter implements MemberPrinter {
149
+ @ Override
150
+ public void addMember (Relation r , Set <String > fields , Set <String > methods ) {
151
+ String msg = r .getMessage ();
152
+ fields .add (msg );
153
+ }
154
+ }
155
+
156
+ static class NullPrinter implements MemberPrinter {
157
+ @ Override
158
+ public void addMember (Relation r , Set <String > fields , Set <String > methods ) {
159
+ System .out .println (String .format ("skipping %s to %s relation" , r .getFromType (), r .getToType ()));
160
+ }
161
+ }
162
+
163
+ static class MethodPrinter implements MemberPrinter {
164
+ @ Override
165
+ public void addMember (Relation r , Set <String > fields , Set <String > methods ) {
166
+ String msg = r .getMessage ();
167
+ methods .add (msg );
168
+ }
169
+ }
79
170
}
0 commit comments