Skip to content

Commit c40dd18

Browse files
committed
Added Todo, improved rendering replacind associations do java.lang by class members, added a way to include more tha java.lang as hiden but not used yet
1 parent 24e22b6 commit c40dd18

File tree

4 files changed

+105
-6
lines changed

4 files changed

+105
-6
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,11 @@ classes of your Diagram.
8484
You might want to remove the plugin markup from your pom after you got the desired Diagrams.
8585

8686

87+
TODO
88+
====
89+
* Add Filters to CLI params
90+
* hide some relations, choose toTypes to hide in relation fromType --> toType.
91+
* show some composition relations as attrs inside the class, e.g. java.lang.*
92+
instead of arrows.
93+
* represent Type parameters in class definitions i.e. public class SelectFieldFactory<D extends SelectFieldDefinition>
94+

src/main/java/com/github/juanmf/java2plant/Parser.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@ protected static Set<String> getTypeParams(Field f) {
237237
}
238238

239239
protected static Set<String> getTypeParams(ParameterizedType f) {
240-
Set<String> typeVars = new HashSet<String>();
240+
Set<String> typeVars = new HashSet<>();
241241
Type[] actualTypeArguments = f.getActualTypeArguments();
242-
for (Type t: actualTypeArguments ) {
243-
typeVars.add(t.toString());
242+
for (Type t: actualTypeArguments) {
243+
typeVars.add(t.toString().replace("class ", ""));
244244
}
245245
return typeVars;
246246
}

src/main/java/com/github/juanmf/java2plant/goals/Parse.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
4343
URLClassLoader loader = getLoader();
4444
getLog().debug("loader URLs: " + Arrays.toString(loader.getURLs()));
4545

46-
getLog().info("Following is the PlantUML src: \n" + Parser.parse(thePackage, Filters.FILTER_ALLOW_ALL, loader));
46+
getLog().info("Following is the PlantUML src: \n" + Parser.parse(thePackage, Filters.FILTER_FORBID_USES, loader));
4747
} catch (DependencyResolutionRequiredException e) {
4848
throw new MojoExecutionException("Something went wrong", e);
4949
}

src/main/java/com/github/juanmf/java2plant/render/PlantRenderer.java

+93-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
package com.github.juanmf.java2plant.render;
22

33
import com.github.juanmf.java2plant.Parser;
4+
import com.github.juanmf.java2plant.structure.Aggregation;
5+
import com.github.juanmf.java2plant.structure.Extension;
46
import com.github.juanmf.java2plant.structure.Relation;
7+
import com.github.juanmf.java2plant.structure.Use;
58

9+
import java.util.HashMap;
10+
import java.util.HashSet;
11+
import java.util.Iterator;
12+
import java.util.Map;
613
import java.util.Set;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
716

817
/**
918
1019
*/
1120
public class PlantRenderer {
1221
private final Set<String> types;
1322
private final Set<Relation> relations;
23+
private final Set<Pattern> toTypesToShowAsMember;
1424
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+
}
1532

1633
public PlantRenderer(Set<String> types, Set<Relation> relations) {
1734
this(types, relations, Filters.FILTER_ALLOW_ALL);
@@ -21,7 +38,8 @@ public PlantRenderer(Set<String> types, Set<Relation> relations, Filter filter)
2138
this.types = types;
2239
this.relations = relations;
2340
this.filter = filter;
24-
41+
toTypesToShowAsMember = new HashSet<>();
42+
toTypesToShowAsMember.add(Pattern.compile("^java.lang.*"));
2543
}
2644

2745
/**
@@ -37,6 +55,8 @@ public String render() {
3755
StringBuilder sb = new StringBuilder();
3856
sb.append("@startuml\n");
3957
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");
4060
sb.append("' Participants \n\n");
4161
addClasses(sb);
4262
sb.append("\n' Relations \n\n");
@@ -56,6 +76,9 @@ protected void addRelations(StringBuilder sb) {
5676
if (filter.isForbidenRelation(r.getClass())) {
5777
continue;
5878
}
79+
if (r.getFromType().contains("$")) {
80+
continue;
81+
}
5982
sb.append(r.toString()).append("\n");
6083
}
6184
}
@@ -69,11 +92,79 @@ protected void addClasses(StringBuilder sb) {
6992
for (String c : types) {
7093
try {
7194
Class<?> aClass = Class.forName(c, true, Parser.CLASS_LOADER);
72-
sb.append(aClass.toString()).append("\n");
95+
addClass(sb, aClass);
7396
} catch (ClassNotFoundException e) {
7497
System.out.println("ClassNotFoundException: " + e.getMessage());
7598
continue;
7699
}
77100
}
78101
}
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+
}
79170
}

0 commit comments

Comments
 (0)