Skip to content

Commit 63b867c

Browse files
Merge remote-tracking branch 'upstream/master'
Conflicts: src/main/java/com/github/juanmf/java2plant/Parser.java src/main/java/com/github/juanmf/java2plant/render/PlantRenderer.java
2 parents a00bdfb + c40dd18 commit 63b867c

File tree

7 files changed

+349
-21
lines changed

7 files changed

+349
-21
lines changed

.settings/org.eclipse.jdt.core.prefs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
3+
org.eclipse.jdt.core.compiler.compliance=1.7
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.7

.settings/org.eclipse.m2e.core.prefs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

README.md

+93
Original file line numberDiff line numberDiff line change
@@ -1 +1,94 @@
11
# Java2PlantUML
2+
This maven plugin allows you to inspect compile time relations on classes
3+
within the class path of your projects, and its dependencies. And renders a
4+
Class Diagram src for PlantUML in cli output as
5+
6+
```
7+
[INFO] Following is the PlantUML src:
8+
@startuml
9+
' Created by [email protected]
10+
11+
' Participants
12+
13+
class com.something.AClass
14+
15+
' Relations
16+
17+
com.something.AClass .down.> info.magnolia.module.googlesitemap.service.SiteMapXMLUtil : StaticPublisher()
18+
com.something.AClass .down.> javax.jcr.Node : setRootNode()
19+
com.something.AClass .down.> boolean : setPreview()
20+
com.something.AClass .down.> java.lang.String : setWorkspace()
21+
com.something.AClass .down.> org.apache.http.client.HttpClient : StaticPublisher()
22+
com.something.AClass .down.> info.magnolia.cms.i18n.I18nContentSupport : StaticPublisher()
23+
com.something.AClass .down.> info.magnolia.dam.api.AssetProviderRegistry : StaticPublisher()
24+
com.something.AClass .down.> class java.lang.String : setPublishingLanguages()java.util.Collection
25+
@enduml
26+
27+
[INFO] ------------------------------------------------------------------------
28+
[INFO] BUILD SUCCESS
29+
[INFO] ------------------------------------------------------------------------
30+
[INFO] Total time: 3.295 s
31+
[INFO] Finished at: 2016-08-02T08:19:29-03:00
32+
[INFO] Final Memory: 17M/212M
33+
[INFO] ------------------------------------------------------------------------
34+
35+
mvn clean install
36+
```
37+
38+
Installation
39+
============
40+
41+
To use it as a plugin, 1st you need install it in local repo as follows. Step
42+
at this project's pom Directory and run
43+
44+
```
45+
mvn clean install
46+
```
47+
48+
Usesage
49+
=======
50+
Then add the plugin markup to your pom.xml.
51+
For the sake of example, this plugin markup is included in its own pom.xml so
52+
you can run the plugin in this project too (after installing, of course).
53+
54+
```xml
55+
<project>
56+
...
57+
<build>
58+
...
59+
<plugins>
60+
...
61+
<plugin>
62+
<artifactId>java2PlantUML-maven-plugin</artifactId>
63+
<version>1.0.1</version>
64+
<configuration>
65+
<goalPrefix>java2PlantUML</goalPrefix>
66+
</configuration>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>
71+
```
72+
73+
Then step at your project's pom directory an run
74+
75+
```
76+
mvn -Dparse.thePackage="com.something.AClass" java2PlantUML:parse
77+
// or
78+
mvn -Dparse.thePackage="com.something.apackage" java2PlantUML:parse
79+
80+
```
81+
parse.thePackage is the root from where class scanning will get the main
82+
classes of your Diagram.
83+
84+
You might want to remove the plugin markup from your pom after you got the desired Diagrams.
85+
86+
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+

pom.xml

+42-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,55 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>com.github.juanmf</groupId>
8-
<artifactId>Java2PlantUML</artifactId>
9-
<version>1.0-SNAPSHOT</version>
8+
<artifactId>java2PlantUML-maven-plugin</artifactId>
9+
<version>1.0.1</version>
10+
<packaging>maven-plugin</packaging>
11+
12+
1013

1114
<dependencies>
1215
<dependency>
1316
<groupId>org.reflections</groupId>
1417
<artifactId>reflections</artifactId>
1518
<version>0.9.10</version>
1619
</dependency>
20+
21+
<!-- dependencies to annotations and maven plugin stuff-->
22+
<dependency>
23+
<groupId>org.apache.maven</groupId>
24+
<artifactId>maven-plugin-api</artifactId>
25+
<version>2.2.1</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.apache.maven</groupId>
29+
<artifactId>maven-project</artifactId>
30+
<version>2.2.1</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.maven.plugin-tools</groupId>
34+
<artifactId>maven-plugin-annotations</artifactId>
35+
<version>3.4</version>
36+
<scope>provided</scope>
37+
</dependency>
1738
</dependencies>
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-compiler-plugin</artifactId>
44+
<configuration>
45+
<source>1.7</source>
46+
<target>1.7</target>
47+
</configuration>
48+
</plugin>
49+
<plugin>
50+
<artifactId>java2PlantUML-maven-plugin</artifactId>
51+
<version>1.0.0</version>
52+
<configuration>
53+
<goalPrefix>java2PlantUML</goalPrefix>
54+
</configuration>
1855

56+
</plugin>
57+
</plugins>
58+
</build>
1959
</project>

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

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.juanmf.java2plant;
22

3+
34
import java.lang.reflect.Constructor;
45
import java.lang.reflect.Field;
56
import java.lang.reflect.Method;
@@ -39,7 +40,9 @@
3940
*
4041
4142
*/
43+
4244
public class Parser {
45+
public static ClassLoader CLASS_LOADER = null;
4346

4447
/**
4548
* Parse the given package recursively, then iterates over found types to fetch their relations.
@@ -48,14 +51,24 @@ public class Parser {
4851
*
4952
* @return PlantUML src code of a Collaboration Diagram for the types found in package and all
5053
* related Types.
51-
*
52-
* @throws ClassNotFoundException
5354
*/
5455
public static String parse(String packageToPase, Filter<Class<? extends Relation>> relationsFilter, Filter<String> classesFilter) throws ClassNotFoundException {
55-
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
56+
List<ClassLoader> classLoadersList = new LinkedList<>();
57+
return parse(packageToPase, relationsFilter, classesFilter, classLoadersList);
58+
}
59+
60+
public static String parse(String packageToPase, Filter<Class<? extends Relation>> relationsFilter, Filter<String> classesFilter, ClassLoader classLoader)
61+
{
62+
List<ClassLoader> classLoadersList = new LinkedList<>();
63+
classLoadersList.add(classLoader);
64+
return parse(packageToPase, relationsFilter, classesFilter, classLoadersList);
65+
}
66+
67+
public static String parse(String packageToPase, Filter<Class<? extends Relation>> relationsFilter, Filter<String> classesFilter, List<ClassLoader> classLoadersList)
68+
{
5669
classLoadersList.add(ClasspathHelper.contextClassLoader());
5770
classLoadersList.add(ClasspathHelper.staticClassLoader());
58-
71+
CLASS_LOADER = classLoadersList.get(0);
5972
Reflections reflections = new Reflections(new ConfigurationBuilder()
6073
.setScanners(new SubTypesScanner(false /* exclude Object.class */), new ResourcesScanner())
6174
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
@@ -64,9 +77,13 @@ public static String parse(String packageToPase, Filter<Class<? extends Relation
6477
Set<String> types = reflections.getAllTypes();
6578
Set<Relation> relations = new HashSet<Relation>();
6679
for (String type: types) {
67-
addFromTypeRelations(relations, Class.forName(type));
80+
try {
81+
addFromTypeRelations(relations, Class.forName(type, true, CLASS_LOADER));
82+
} catch (ClassNotFoundException e) {
83+
System.out.println("ClassNotFoundException: " + e.getMessage());
84+
continue;
85+
}
6886
}
69-
7087
return new PlantRenderer(types, relations, relationsFilter, classesFilter).render();
7188
}
7289

@@ -180,7 +197,7 @@ protected static void addMethodUse(Set<Relation> relations, Class<?> fromType, C
180197

181198
protected static String getSimpleName(String fqcn) {
182199
int lastDotidx = fqcn.lastIndexOf(".");
183-
String simpleName = -1 == lastDotidx ? fqcn : fqcn.substring(lastDotidx);
200+
String simpleName = -1 == lastDotidx ? fqcn : fqcn.substring(lastDotidx + 1);
184201
return simpleName;
185202
}
186203

@@ -222,10 +239,10 @@ protected static Set<String> getTypeParams(Field f) {
222239
}
223240

224241
protected static Set<String> getTypeParams(ParameterizedType f) {
225-
Set<String> typeVars = new HashSet<String>();
242+
Set<String> typeVars = new HashSet<>();
226243
Type[] actualTypeArguments = f.getActualTypeArguments();
227-
for (Type t: actualTypeArguments ) {
228-
typeVars.add(t.toString());
244+
for (Type t: actualTypeArguments) {
245+
typeVars.add(t.toString().replace("class ", ""));
229246
}
230247
return typeVars;
231248
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.github.juanmf.java2plant.goals;
2+
3+
import java.io.File;
4+
import java.net.MalformedURLException;
5+
import java.net.URL;
6+
import java.net.URLClassLoader;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import org.apache.maven.artifact.DependencyResolutionRequiredException;
12+
import org.apache.maven.plugin.AbstractMojo;
13+
import org.apache.maven.plugin.MojoExecutionException;
14+
import org.apache.maven.plugin.MojoFailureException;
15+
import org.apache.maven.plugins.annotations.Component;
16+
import org.apache.maven.plugins.annotations.Mojo;
17+
import org.apache.maven.plugins.annotations.Parameter;
18+
import org.apache.maven.plugins.annotations.ResolutionScope;
19+
import org.apache.maven.project.MavenProject;
20+
21+
import com.github.juanmf.java2plant.Parser;
22+
import com.github.juanmf.java2plant.render.filters.Filters;
23+
24+
/**
25+
*
26+
27+
*/
28+
@Mojo(name = "parse",
29+
requiresDependencyResolution = ResolutionScope.COMPILE)
30+
public class Parse extends AbstractMojo {
31+
32+
/**
33+
* The package to parse for Types and Associations
34+
*/
35+
@Parameter(property = "parse.thePackage", defaultValue = "com.github.juanmf.java2plant.structure")
36+
private String thePackage;
37+
38+
@Component
39+
private MavenProject project;
40+
41+
@SuppressWarnings("unchecked")
42+
public void execute() throws MojoExecutionException, MojoFailureException {
43+
try {
44+
URLClassLoader loader = getLoader();
45+
getLog().debug("loader URLs: " + Arrays.toString(loader.getURLs()));
46+
47+
getLog().info("Following is the PlantUML src: \n" + Parser.parse(thePackage, Filters.FILTER_FORBID_USES, Filters.FILTER_ALLOW_ALL_CLASSES, loader));
48+
} catch (DependencyResolutionRequiredException e) {
49+
throw new MojoExecutionException("Something went wrong", e);
50+
}
51+
}
52+
53+
/**
54+
* Fetches all project's class path elements and creates a URLClassLoader to be used by
55+
* Reflections.
56+
*
57+
* @return All class path's URLs in a ClassLoader
58+
* @throws DependencyResolutionRequiredException
59+
* @throws MojoExecutionException
60+
*/
61+
private URLClassLoader getLoader() throws DependencyResolutionRequiredException, MojoExecutionException {
62+
List<String> classpathElements = null;
63+
classpathElements = project.getCompileClasspathElements();
64+
List<URL> projectClasspathList = new ArrayList<>();
65+
for (String element : classpathElements) {
66+
try {
67+
projectClasspathList.add(new File(element).toURI().toURL());
68+
} catch (MalformedURLException e) {
69+
throw new MojoExecutionException(element + " is an invalid classpath element", e);
70+
}
71+
}
72+
URLClassLoader loader = new URLClassLoader(projectClasspathList.toArray(new URL[0]));
73+
return loader;
74+
}
75+
}

0 commit comments

Comments
 (0)