Added ability to specify root classes.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 19 Oct 2021 16:57:54 +0000 (19:57 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 19 Oct 2021 16:57:54 +0000 (19:57 +0300)
System will try to discover class reference tree starting from roots.

src/main/java/eu/svjatoslav/inspector/java/commandline/CommandlineConfiguration.java
src/main/java/eu/svjatoslav/inspector/java/commandline/Main.java

index 5d4bf6f..2f8feca 100644 (file)
@@ -10,6 +10,7 @@ public class CommandlineConfiguration {
     private NullParameter showDebug;
     public StringParameters whitelistGlob;
     public StringParameters blacklistGlob;
+    public StringParameters rootClasses;
     public TargetImageTypeParameter targetImageType;
     public NullParameter keepDotFile;
     public DirectoryParameter targetDirectory;
@@ -61,6 +62,11 @@ public class CommandlineConfiguration {
                 new StringParameters("Blacklist glob(s)."))
                 .addAliases("-b");
 
+        rootClasses = parser.add(
+                        new StringParameters("root class(es)."))
+                .addAliases("-r");
+
+
         targetDirectory = parser.add(new DirectoryParameter("Target directory. " +
                 "Default is current directory.").mustExist())
                 .addAliases("-d");
index 6a63588..d4d144a 100644 (file)
@@ -45,9 +45,16 @@ public class Main {
 
         classGraph.setKeepDotFile(configuration.keepDotFile.getValue());
 
-        for (File jarFile : jarFiles)
-            addJarToGraph(jarFile, classLoader, classGraph, configuration);
-
+        if (configuration.rootClasses.isSpecified()){
+            // add only selected root classes
+            for (String rootClass : configuration.rootClasses.getValue())
+                attemptClassAdditionByName(classLoader, classGraph, configuration, rootClass);
+
+        } else {
+            // add all classes in the jar files to graph
+            for (File jarFile : jarFiles)
+                addJarToGraph(jarFile, classLoader, classGraph, configuration);
+        }
         configuration.blacklistGlob.getValue().forEach(classGraph::blacklistClassGlob);
         configuration.whitelistGlob.getValue().forEach(classGraph::whitelistClassGlob);
 
@@ -74,17 +81,20 @@ public class Main {
             File jarFile, URLClassLoader classLoader, ClassGraph classGraph, CommandlineConfiguration configuration)
             throws IOException, ClassNotFoundException {
 
-        for (String className : getClassNamesFromJar(jarFile)) {
+        for (String className : getClassNamesFromJar(jarFile))
+            attemptClassAdditionByName(classLoader, classGraph, configuration, className);
+    }
+
+    private static void attemptClassAdditionByName(URLClassLoader classLoader, ClassGraph classGraph, CommandlineConfiguration configuration, String className) throws ClassNotFoundException {
+        if (configuration.isDebug())
+            System.out.println("Adding class to graph: " + className);
+        try {
+            classGraph.add(loadClassByName(classLoader, className));
+        } catch (NoClassDefFoundError e){
             if (configuration.isDebug())
-                System.out.println("Adding class to graph: " + className);
-            try {
-                classGraph.add(loadClassByName(classLoader, className));
-            } catch (NoClassDefFoundError e){
-                if (configuration.isDebug())
-                    System.out.println("Class definition was not found.");
-                // Sometimes referenced classes are not found in the same Jar.
-                // Let's ignore this and proceed with the classes that we have.
-            }
+                System.out.println("Class definition was not found.");
+            // Sometimes referenced classes are not found in the same Jar.
+            // Let's ignore this and proceed with the classes that we have.
         }
     }