Skip to content

Commit 24e22b6

Browse files
committed
Fixed classLoader while using as plugin
1 parent ad22c94 commit 24e22b6

File tree

5 files changed

+175
-26
lines changed

5 files changed

+175
-26
lines changed

README.md

+71-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
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
25

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).
353

454
```xml
555
<project>
@@ -9,13 +59,28 @@
959
<plugins>
1060
...
1161
<plugin>
12-
<artifactId>java2PlantUML-maven-plugin</artifactId>
13-
<version>1.0-SNAPSHOT</version>
14-
<configuration>
15-
<goalPrefix>java2PlantUML</goalPrefix>
16-
</configuration>
17-
</plugin>
62+
<artifactId>java2PlantUML-maven-plugin</artifactId>
63+
<version>1.0.1</version>
64+
<configuration>
65+
<goalPrefix>java2PlantUML</goalPrefix>
66+
</configuration>
67+
</plugin>
1868
</plugins>
1969
</build>
2070
</project>
2171
```
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+

pom.xml

+27-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.juanmf</groupId>
88
<artifactId>java2PlantUML-maven-plugin</artifactId>
9-
<version>1.0-SNAPSHOT</version>
9+
<version>1.0.1</version>
1010
<packaging>maven-plugin</packaging>
1111

1212
<dependencies>
@@ -16,19 +16,42 @@
1616
<version>0.9.10</version>
1717
</dependency>
1818

19+
<!-- dependencies to annotations and maven plugin stuff-->
1920
<dependency>
2021
<groupId>org.apache.maven</groupId>
2122
<artifactId>maven-plugin-api</artifactId>
22-
<version>3.0</version>
23+
<version>2.2.1</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.apache.maven</groupId>
27+
<artifactId>maven-project</artifactId>
28+
<version>2.2.1</version>
2329
</dependency>
24-
25-
<!-- dependencies to annotations -->
2630
<dependency>
2731
<groupId>org.apache.maven.plugin-tools</groupId>
2832
<artifactId>maven-plugin-annotations</artifactId>
2933
<version>3.4</version>
3034
<scope>provided</scope>
3135
</dependency>
3236
</dependencies>
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-compiler-plugin</artifactId>
42+
<configuration>
43+
<source>1.7</source>
44+
<target>1.7</target>
45+
</configuration>
46+
</plugin>
47+
<plugin>
48+
<artifactId>java2PlantUML-maven-plugin</artifactId>
49+
<version>1.0.0</version>
50+
<configuration>
51+
<goalPrefix>java2PlantUML</goalPrefix>
52+
</configuration>
3353

54+
</plugin>
55+
</plugins>
56+
</build>
3457
</project>

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

+23-8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141

4242
public class Parser {
43+
public static ClassLoader CLASS_LOADER = null;
4344

4445
/**
4546
* Parse the given package recursively, then iterates over found types to fetch their relations.
@@ -48,14 +49,24 @@ public class Parser {
4849
*
4950
* @return PlantUML src code of a Collaboration Diagram for the types found in package and all
5051
* related Types.
51-
*
52-
* @throws ClassNotFoundException
5352
*/
54-
public static String parse(String packageToPase, Filter filter) throws ClassNotFoundException {
55-
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
53+
public static String parse(String packageToPase, Filter filter) {
54+
List<ClassLoader> classLoadersList = new LinkedList<>();
55+
return parse(packageToPase, filter, classLoadersList);
56+
}
57+
58+
public static String parse(String packageToPase, Filter filter, ClassLoader classLoader)
59+
{
60+
List<ClassLoader> classLoadersList = new LinkedList<>();
61+
classLoadersList.add(classLoader);
62+
return parse(packageToPase, filter, classLoadersList);
63+
}
64+
65+
public static String parse(String packageToPase, Filter filter, List<ClassLoader> classLoadersList)
66+
{
5667
classLoadersList.add(ClasspathHelper.contextClassLoader());
5768
classLoadersList.add(ClasspathHelper.staticClassLoader());
58-
69+
CLASS_LOADER = classLoadersList.get(0);
5970
Reflections reflections = new Reflections(new ConfigurationBuilder()
6071
.setScanners(new SubTypesScanner(false /* exclude Object.class */), new ResourcesScanner())
6172
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
@@ -64,9 +75,13 @@ public static String parse(String packageToPase, Filter filter) throws ClassNotF
6475
Set<String> types = reflections.getAllTypes();
6576
Set<Relation> relations = new HashSet<Relation>();
6677
for (String type: types) {
67-
addFromTypeRelations(relations, Class.forName(type));
78+
try {
79+
addFromTypeRelations(relations, Class.forName(type, true, CLASS_LOADER));
80+
} catch (ClassNotFoundException e) {
81+
System.out.println("ClassNotFoundException: " + e.getMessage());
82+
continue;
83+
}
6884
}
69-
7085
return new PlantRenderer(types, relations, filter).render();
7186
}
7287

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

181196
protected static String getSimpleName(String fqcn) {
182197
int lastDotidx = fqcn.lastIndexOf(".");
183-
String simpleName = -1 == lastDotidx ? fqcn : fqcn.substring(lastDotidx);
198+
String simpleName = -1 == lastDotidx ? fqcn : fqcn.substring(lastDotidx + 1);
184199
return simpleName;
185200
}
186201

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

+47-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@
22

33
import com.github.juanmf.java2plant.Parser;
44
import com.github.juanmf.java2plant.render.Filters;
5+
import org.apache.maven.artifact.DependencyResolutionRequiredException;
56
import org.apache.maven.plugin.AbstractMojo;
67
import org.apache.maven.plugin.MojoExecutionException;
78
import org.apache.maven.plugin.MojoFailureException;
9+
import org.apache.maven.plugins.annotations.Component;
810
import org.apache.maven.plugins.annotations.Mojo;
911
import org.apache.maven.plugins.annotations.Parameter;
12+
import org.apache.maven.plugins.annotations.ResolutionScope;
13+
import org.apache.maven.project.MavenProject;
14+
15+
import java.io.File;
16+
import java.net.MalformedURLException;
17+
import java.net.URL;
18+
import java.net.URLClassLoader;
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.List;
1022

1123
/**
24+
*
1225
1326
*/
14-
@Mojo(name = "parse")
27+
@Mojo(name = "parse",
28+
requiresDependencyResolution = ResolutionScope.COMPILE)
1529
public class Parse extends AbstractMojo {
1630

1731
/**
@@ -20,11 +34,41 @@ public class Parse extends AbstractMojo {
2034
@Parameter(property = "parse.thePackage", defaultValue = "com.github.juanmf.java2plant.structure")
2135
private String thePackage;
2236

37+
@Component
38+
private MavenProject project;
39+
40+
@SuppressWarnings("unchecked")
2341
public void execute() throws MojoExecutionException, MojoFailureException {
2442
try {
25-
System.out.println(Parser.parse(thePackage, Filters.FILTER_ALLOW_ALL));
26-
} catch (ClassNotFoundException e) {
43+
URLClassLoader loader = getLoader();
44+
getLog().debug("loader URLs: " + Arrays.toString(loader.getURLs()));
45+
46+
getLog().info("Following is the PlantUML src: \n" + Parser.parse(thePackage, Filters.FILTER_ALLOW_ALL, loader));
47+
} catch (DependencyResolutionRequiredException e) {
2748
throw new MojoExecutionException("Something went wrong", e);
2849
}
2950
}
51+
52+
/**
53+
* Fetches all project's class path elements and creates a URLClassLoader to be used by
54+
* Reflections.
55+
*
56+
* @return All class path's URLs in a ClassLoader
57+
* @throws DependencyResolutionRequiredException
58+
* @throws MojoExecutionException
59+
*/
60+
private URLClassLoader getLoader() throws DependencyResolutionRequiredException, MojoExecutionException {
61+
List<String> classpathElements = null;
62+
classpathElements = project.getCompileClasspathElements();
63+
List<URL> projectClasspathList = new ArrayList<>();
64+
for (String element : classpathElements) {
65+
try {
66+
projectClasspathList.add(new File(element).toURI().toURL());
67+
} catch (MalformedURLException e) {
68+
throw new MojoExecutionException(element + " is an invalid classpath element", e);
69+
}
70+
}
71+
URLClassLoader loader = new URLClassLoader(projectClasspathList.toArray(new URL[0]));
72+
return loader;
73+
}
3074
}

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

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

3+
import com.github.juanmf.java2plant.Parser;
34
import com.github.juanmf.java2plant.structure.Relation;
45

56
import java.util.Set;
@@ -65,13 +66,14 @@ protected void addRelations(StringBuilder sb) {
6566
* @param sb
6667
*/
6768
protected void addClasses(StringBuilder sb) {
68-
try {
69-
for (String c : types) {
70-
Class<?> aClass = Class.forName(c);
69+
for (String c : types) {
70+
try {
71+
Class<?> aClass = Class.forName(c, true, Parser.CLASS_LOADER);
7172
sb.append(aClass.toString()).append("\n");
73+
} catch (ClassNotFoundException e) {
74+
System.out.println("ClassNotFoundException: " + e.getMessage());
75+
continue;
7276
}
73-
} catch (ClassNotFoundException e) {
74-
e.printStackTrace();
7577
}
7678
}
7779
}

0 commit comments

Comments
 (0)