From: Svjatoslav Agejenko Date: Tue, 19 Oct 2021 16:57:54 +0000 (+0300) Subject: Added ability to specify root classes. X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=250ce45d956b8c1c9b9247b1e248aab580190451;p=javainspect.git Added ability to specify root classes. System will try to discover class reference tree starting from roots. --- diff --git a/src/main/java/eu/svjatoslav/inspector/java/commandline/CommandlineConfiguration.java b/src/main/java/eu/svjatoslav/inspector/java/commandline/CommandlineConfiguration.java index 5d4bf6f..2f8feca 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/commandline/CommandlineConfiguration.java +++ b/src/main/java/eu/svjatoslav/inspector/java/commandline/CommandlineConfiguration.java @@ -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"); diff --git a/src/main/java/eu/svjatoslav/inspector/java/commandline/Main.java b/src/main/java/eu/svjatoslav/inspector/java/commandline/Main.java index 6a63588..d4d144a 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/commandline/Main.java +++ b/src/main/java/eu/svjatoslav/inspector/java/commandline/Main.java @@ -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. } }