ReflectionUtils { @Nonnull @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", justification = "False positive on try-with-resources as of JDK11") public static Resources resourcesOf(String... packages) { String[] paths = stream(packages).map(ReflectionUtils::toPath).toArray(String[]::new); ClassGraph classGraph = new ClassGraph() .whitelistPackages(packages) .whitelistPaths(paths) .ignoreClassVisibility(); try (ScanResult scanResult = classGraph.scan()) { Collection<ClassResource> classes = Util.toList(scanResult.getAllClasses(), ClassResource::new); Collection<URL> nonClasses = scanResult.getAllResources().nonClassFilesOnly().getURLs(); return new Resources(classes, nonClasses); } } private  ReflectionUtils(); static T readStaticFieldOrNull(String classname, String fieldName); @Nonnull @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", justification = "False positive on try-with-resources as of JDK11") static Collection<Class<?>> nestedClassesOf(Class<?>... classes); @Nonnull @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", justification = "False positive on try-with-resources as of JDK11") static Resources resourcesOf(String... packages); static String toClassResourceId(String name); static Class<?> loadClass(ClassLoader classLoader, String name); static T newInstance(ClassLoader classLoader, String name);  }
@Test public void when_resourcesOf_then_returnsAllResources() throws ClassNotFoundException { Resources resources = ReflectionUtils.resourcesOf(OuterClass.class.getPackage().getName()); Collection<ClassResource> classes = resources.classes().collect(toList()); assertThat(classes, hasSize(greaterThan(5))); assertThat(classes, hasItem(classResource(OuterClass.class))); assertThat(classes, hasItem(classResource(OuterClass.NestedClass.class))); assertThat(classes, hasItem(classResource(OuterClass.NestedClass.DeeplyNestedClass.class))); assertThat(classes, hasItem(classResource(Class.forName(OuterClass.class.getName() + "$1")))); assertThat(classes, hasItem( classResource(Class.forName(OuterClass.NestedClass.DeeplyNestedClass.class.getName() + "$1")) )); List<URL> nonClasses = resources.nonClasses().collect(toList()); assertThat(nonClasses, hasSize(5)); assertThat(nonClasses, hasItem(hasToString(containsString("file.json")))); assertThat(nonClasses, hasItem(hasToString(containsString("file_list.json")))); assertThat(nonClasses, hasItem(hasToString(containsString("file_pretty_printed.json")))); assertThat(nonClasses, hasItem(hasToString(containsString("file_list_pretty_printed.json")))); assertThat(nonClasses, hasItem(hasToString(containsString("package.properties")))); }