Code cleanup.
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / Main.java
1 package eu.svjatoslav.inspector.java;
2
3 import eu.svjatoslav.inspector.java.structure.ClassGraph;
4
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.URLClassLoader;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.jar.JarEntry;
14 import java.util.jar.JarInputStream;
15
16 import static java.io.File.separator;
17 import static java.lang.System.getProperty;
18
19 /**
20  * This class acts as a commandline interface for JavaInspect.
21  */
22 public class Main {
23     public static void main(String[] args) throws IOException, ClassNotFoundException {
24         CommandlineConfiguration configuration = new CommandlineConfiguration(args);
25
26         List<File> jarFiles = configuration.jarFiles.getValue();
27
28         URLClassLoader classLoader = new URLClassLoader(
29                 getFileUrls(jarFiles),
30                 configuration.getClass().getClassLoader());
31
32         ClassGraph classGraph = new ClassGraph();
33         classGraph.setTargetDirectoryPath(getProperty("user.dir") + separator);
34         classGraph.setKeepDotFile(true);
35
36         for (File jarFile : jarFiles)
37             addJarToGraph(jarFile, classLoader, classGraph, configuration);
38
39         classGraph.generateGraph(configuration.graphName.getValue());
40
41         if (configuration.isDebug())
42             System.out.println("Graph ready.");
43     }
44
45     private static URL[] getFileUrls(List<File> jarFiles) {
46         List<URL> urls = new ArrayList<>();
47         jarFiles.forEach((File file) -> {
48             try {
49                 urls.add(file.toURI().toURL());
50             } catch (MalformedURLException e) {
51                 throw new RuntimeException(e);
52             }
53         });
54
55         return urls.toArray(new URL[urls.size()]);
56     }
57
58     private static void addJarToGraph(
59             File jarFile, URLClassLoader classLoader, ClassGraph classGraph, CommandlineConfiguration configuration)
60             throws IOException, ClassNotFoundException {
61
62         for (String className : getClassNamesFromJar(jarFile)) {
63             if (configuration.isDebug())
64                 System.out.println("Adding class to graph: " + className);
65
66             classGraph.add(loadClassByName(classLoader, className));
67         }
68     }
69
70     private static Class loadClassByName(URLClassLoader classLoader, String className) throws ClassNotFoundException {
71         return Class.forName(className, true, classLoader);
72     }
73
74     public static List<String> getClassNamesFromJar(File jarFile) throws IOException {
75         List<String> result = new ArrayList<>();
76         try (
77                 JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile))
78         ) {
79             while (true) {
80                 JarEntry jarEntry = jarInputStream.getNextJarEntry();
81                 if (jarEntry == null)
82                     break;
83
84                 if (isClassFile(jarEntry))
85                     result.add(getClassNameFromFileName(jarEntry));
86             }
87
88             return result;
89         }
90     }
91
92     private static boolean isClassFile(JarEntry jarEntry) {
93         return jarEntry.getName().endsWith(".class");
94     }
95
96     private static String getClassNameFromFileName(JarEntry jarEntry) {
97         String result = jarEntry.getName().replaceAll("/", "\\.");
98         return result.substring(0, result.lastIndexOf('.'));
99     }
100 }