ClasspathScanUtil { static Set<Class<?>> getClasses(Stream<String> classNames, ClassLoader classLoader) { AtomicInteger missingClasses = new AtomicInteger(); AtomicInteger missingDependentClasses = new AtomicInteger(); Set<Class<?>> collect = classNames .map(className -> { try { return classLoader.loadClass(className); } catch (ClassNotFoundException e) { missingClasses.incrementAndGet(); log.debug("Failed to load class {} although it's listed in the prepared scan result.", className); log.trace("Stacktrace", e); } catch (NoClassDefFoundError e) { missingDependentClasses.incrementAndGet(); log.debug("Failed to load class {} because it's dependency {} is missing.", className, e.getMessage()); log.trace("Stacktrace", e); } catch (LinkageError e) { log.warn("Failed to load class {} from prepared scan result", className, e); } return null; }) .filter(Objects::nonNull) .collect(Collectors.toSet()); if (missingClasses.get() > 0) { log.warn("{} classes listed in the prepared scan result could not be found. Set the log-level to debug for more information.", missingClasses.get()); } if (missingDependentClasses.get() > 0) { log.info("{} classes failed to load, because some of their dependencies are missing. Set the log-level to debug for more information.", missingDependentClasses.get()); } return collect; }  static Optional<Set<Class<?>>> readClassSet(String resourceName, ClassLoader classLoader); static Optional<Map<Class<? extends Annotation>, Set<Class<?>>>> readClassMap(String resourceName, ClassLoader classLoader);  }
@Test void getClasses_empty() { Set<Class<?>> classes = ClasspathScanUtil.getClasses(Stream.empty(), getClass().getClassLoader()); assertThat(classes).isEmpty(); }
@Test void getClasses_notFound() { Set<Class<?>> classes = ClasspathScanUtil.getClasses(Stream.of("foo.Bar"), getClass().getClassLoader()); assertThat(classes).isEmpty(); }
@Test void getClasses_notLoadable() { Set<Class<?>> classes = ClasspathScanUtil.getClasses(Stream.of("org.springframework.boot.autoconfigure.mustache.MustacheEnvironmentCollector"), getClass().getClassLoader()); assertThat(classes).isEmpty(); }
@Test void getClasses_one() { Set<Class<?>> classes = ClasspathScanUtil.getClasses(Stream.of(this.getClass().getName()), getClass().getClassLoader()); assertThat(classes).contains(this.getClass()); }