Skip to content

Commit bf56310

Browse files
Spotless reformatting
1 parent b991f3b commit bf56310

File tree

5 files changed

+244
-253
lines changed

5 files changed

+244
-253
lines changed
Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,71 @@
11
package com.ideacrest.app;
22

3+
import com.ideacrest.cycledetector.CircularReferenceChecker;
4+
import com.ideacrest.parser.JavaProjectParser;
35
import java.io.IOException;
46
import java.util.Map;
5-
67
import org.jgrapht.Graph;
78
import org.jgrapht.graph.AsSubgraph;
89
import org.jgrapht.graph.DefaultEdge;
910

10-
import com.ideacrest.cycledetector.CircularReferenceChecker;
11-
import com.ideacrest.parser.JavaProjectParser;
12-
1311
/**
1412
* Command line application to detect circular references in a java project.
1513
* Takes two arguments : source folder of java project, directory to store images of the circular reference graphs.
16-
*
14+
*
1715
* @author nikhil_pereira
1816
*
1917
*/
2018
public class CircularReferenceDetectorApp {
2119

22-
public static void main(String[] args) {
23-
CircularReferenceDetectorApp circularReferenceDetectorApp = new CircularReferenceDetectorApp();
24-
circularReferenceDetectorApp.launchApp(args);
25-
}
26-
27-
/**
28-
* Parses source project files and creates a graph of class references of the java project.
29-
* Detects cycles in the class references graph and stores the cycle graphs in the given output directory
30-
*
31-
* @param args
32-
*/
33-
public void launchApp(String[] args) {
34-
if(!validateArgs(args)) {
35-
printCommandUsage();
36-
}
37-
else {
38-
String srcDirectoryPath = args[0];
39-
String outputDirectoryPath = args[1];
40-
JavaProjectParser javaProjectParser = new JavaProjectParser();
41-
try {
42-
Graph<String, DefaultEdge> classReferencesGraph = javaProjectParser.getClassReferences(srcDirectoryPath);
43-
detectAndStoreCyclesInDirectory(outputDirectoryPath, classReferencesGraph);
44-
}
45-
catch(Exception e) {
46-
e.printStackTrace();
47-
}
48-
}
49-
}
20+
public static void main(String[] args) {
21+
CircularReferenceDetectorApp circularReferenceDetectorApp = new CircularReferenceDetectorApp();
22+
circularReferenceDetectorApp.launchApp(args);
23+
}
24+
25+
/**
26+
* Parses source project files and creates a graph of class references of the java project.
27+
* Detects cycles in the class references graph and stores the cycle graphs in the given output directory
28+
*
29+
* @param args
30+
*/
31+
public void launchApp(String[] args) {
32+
if (!validateArgs(args)) {
33+
printCommandUsage();
34+
} else {
35+
String srcDirectoryPath = args[0];
36+
String outputDirectoryPath = args[1];
37+
JavaProjectParser javaProjectParser = new JavaProjectParser();
38+
try {
39+
Graph<String, DefaultEdge> classReferencesGraph =
40+
javaProjectParser.getClassReferences(srcDirectoryPath);
41+
detectAndStoreCyclesInDirectory(outputDirectoryPath, classReferencesGraph);
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
}
5047

51-
private void detectAndStoreCyclesInDirectory(String outputDirectoryPath, Graph<String, DefaultEdge> classReferencesGraph) {
52-
CircularReferenceChecker circularReferenceChecker = new CircularReferenceChecker();
53-
Map<String,AsSubgraph<String, DefaultEdge>> cyclesForEveryVertexMap = circularReferenceChecker.detectCyles(classReferencesGraph);
54-
cyclesForEveryVertexMap.forEach( (vertex, subGraph)->{
55-
try {
56-
circularReferenceChecker.createImage(outputDirectoryPath, subGraph, vertex);
57-
} catch (IOException e) {
58-
e.printStackTrace();
59-
}
60-
});
61-
}
48+
private void detectAndStoreCyclesInDirectory(
49+
String outputDirectoryPath, Graph<String, DefaultEdge> classReferencesGraph) {
50+
CircularReferenceChecker circularReferenceChecker = new CircularReferenceChecker();
51+
Map<String, AsSubgraph<String, DefaultEdge>> cyclesForEveryVertexMap =
52+
circularReferenceChecker.detectCyles(classReferencesGraph);
53+
cyclesForEveryVertexMap.forEach((vertex, subGraph) -> {
54+
try {
55+
circularReferenceChecker.createImage(outputDirectoryPath, subGraph, vertex);
56+
} catch (IOException e) {
57+
e.printStackTrace();
58+
}
59+
});
60+
}
6261

63-
private boolean validateArgs(String[] args) {
64-
return args.length == 2;
65-
}
62+
private boolean validateArgs(String[] args) {
63+
return args.length == 2;
64+
}
6665

67-
private void printCommandUsage() {
68-
System.out.println("Usage:\n"
69-
+ "argument 1 : file path of source directory of the java project."
70-
+ "argument 2 : output directory path to store images of the circular reference graphs.");
71-
}
66+
private void printCommandUsage() {
67+
System.out.println("Usage:\n"
68+
+ "argument 1 : file path of source directory of the java project."
69+
+ "argument 2 : output directory path to store images of the circular reference graphs.");
70+
}
7271
}
Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,63 @@
11
package com.ideacrest.cycledetector;
22

3+
import com.mxgraph.layout.mxCircleLayout;
4+
import com.mxgraph.layout.mxIGraphLayout;
5+
import com.mxgraph.util.mxCellRenderer;
36
import java.awt.Color;
47
import java.awt.image.BufferedImage;
58
import java.io.File;
69
import java.io.IOException;
710
import java.util.HashMap;
811
import java.util.Map;
9-
1012
import javax.imageio.ImageIO;
11-
1213
import org.jgrapht.Graph;
1314
import org.jgrapht.alg.cycle.CycleDetector;
1415
import org.jgrapht.ext.JGraphXAdapter;
1516
import org.jgrapht.graph.AsSubgraph;
1617
import org.jgrapht.graph.DefaultEdge;
1718

18-
import com.mxgraph.layout.mxCircleLayout;
19-
import com.mxgraph.layout.mxIGraphLayout;
20-
import com.mxgraph.util.mxCellRenderer;
21-
2219
public class CircularReferenceChecker {
2320

24-
/**
25-
* Detects cycles in the classReferencesGraph parameter
26-
* and stores the cycles of a class as a subgraph in a Map
27-
*
28-
* @param classReferencesGraph
29-
* @return a Map of Class and its Cycle Graph
30-
*/
31-
public Map<String,AsSubgraph<String, DefaultEdge>> detectCyles(Graph<String, DefaultEdge> classReferencesGraph) {
32-
Map<String, AsSubgraph<String, DefaultEdge>> cyclesForEveryVertexMap = new HashMap<>();
33-
CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<>(classReferencesGraph);
34-
cycleDetector.findCycles().forEach(v -> {
35-
AsSubgraph<String, DefaultEdge> subGraph = new AsSubgraph<>(classReferencesGraph,
36-
cycleDetector.findCyclesContainingVertex(v));
37-
cyclesForEveryVertexMap.put(v,subGraph);
38-
});
39-
return cyclesForEveryVertexMap;
40-
}
41-
42-
/**
43-
* Given graph and image name, use jgrapht to create .png file of graph in given outputDirectory.
44-
* Create outputDirectory if it does not exist.
45-
*
46-
* @param outputDirectoryPath
47-
* @param subGraph
48-
* @param imageName
49-
* @throws IOException
50-
*/
51-
public void createImage(String outputDirectoryPath, Graph<String, DefaultEdge> subGraph, String imageName) throws IOException {
52-
new File(outputDirectoryPath).mkdirs();
53-
File imgFile = new File(outputDirectoryPath+"/graph" + imageName + ".png");
54-
if(imgFile.createNewFile()) {
55-
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<>(subGraph);
56-
mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
57-
layout.execute(graphAdapter.getDefaultParent());
58-
59-
BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null);
60-
if (image != null) {
61-
ImageIO.write(image, "PNG", imgFile);
62-
}
63-
}
64-
}
21+
/**
22+
* Detects cycles in the classReferencesGraph parameter
23+
* and stores the cycles of a class as a subgraph in a Map
24+
*
25+
* @param classReferencesGraph
26+
* @return a Map of Class and its Cycle Graph
27+
*/
28+
public Map<String, AsSubgraph<String, DefaultEdge>> detectCyles(Graph<String, DefaultEdge> classReferencesGraph) {
29+
Map<String, AsSubgraph<String, DefaultEdge>> cyclesForEveryVertexMap = new HashMap<>();
30+
CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<>(classReferencesGraph);
31+
cycleDetector.findCycles().forEach(v -> {
32+
AsSubgraph<String, DefaultEdge> subGraph =
33+
new AsSubgraph<>(classReferencesGraph, cycleDetector.findCyclesContainingVertex(v));
34+
cyclesForEveryVertexMap.put(v, subGraph);
35+
});
36+
return cyclesForEveryVertexMap;
37+
}
38+
39+
/**
40+
* Given graph and image name, use jgrapht to create .png file of graph in given outputDirectory.
41+
* Create outputDirectory if it does not exist.
42+
*
43+
* @param outputDirectoryPath
44+
* @param subGraph
45+
* @param imageName
46+
* @throws IOException
47+
*/
48+
public void createImage(String outputDirectoryPath, Graph<String, DefaultEdge> subGraph, String imageName)
49+
throws IOException {
50+
new File(outputDirectoryPath).mkdirs();
51+
File imgFile = new File(outputDirectoryPath + "/graph" + imageName + ".png");
52+
if (imgFile.createNewFile()) {
53+
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<>(subGraph);
54+
mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
55+
layout.execute(graphAdapter.getDefaultParent());
56+
57+
BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null);
58+
if (image != null) {
59+
ImageIO.write(image, "PNG", imgFile);
60+
}
61+
}
62+
}
6563
}

0 commit comments

Comments
 (0)