@Test public void annotatedClassesAreFound() throws Exception { Class[] annotations = {Scannable.class}; String[] packages = {"com.comcast.redirector.common.util.annotation.scanner.sample"}; Set<Class<?>> result = AnnotationScanner.getAnnotatedClasses(annotations, packages); Assert.assertTrue(result.contains(Annotated.class)); Assert.assertTrue(result.contains(OneMoreAnnotated.class)); Assert.assertFalse(result.contains(NotAnnotated.class)); Assert.assertFalse(result.contains(AnnotadedByIgnoredAnnotation.class)); }
public static Set<Class<?>> getAnnotatedClasses(final Class[] annotations, final String... packages) { Set<String> annotationsToScan = Stream.of(annotations).map(Class::getSimpleName).collect(toSet()); ClassLoader cl = AnnotationScanner.class.getClassLoader(); try { ClassPath cp = ClassPath.from(cl); return Stream.of(packages) .flatMap(packageName -> cp.getTopLevelClassesRecursive(packageName).stream()) .filter(isClassAnnotatedByScannedAnnotations(annotationsToScan)) .map(ClassPath.ClassInfo::load) .collect(toSet()); } catch (IOException e) { log.error("Failed to get annotated classes", e); return Collections.emptySet(); } }
AnnotationScanner { public static Set<Class<?>> getAnnotatedClasses(final Class[] annotations, final String... packages) { Set<String> annotationsToScan = Stream.of(annotations).map(Class::getSimpleName).collect(toSet()); ClassLoader cl = AnnotationScanner.class.getClassLoader(); try { ClassPath cp = ClassPath.from(cl); return Stream.of(packages) .flatMap(packageName -> cp.getTopLevelClassesRecursive(packageName).stream()) .filter(isClassAnnotatedByScannedAnnotations(annotationsToScan)) .map(ClassPath.ClassInfo::load) .collect(toSet()); } catch (IOException e) { log.error("Failed to get annotated classes", e); return Collections.emptySet(); } } }
AnnotationScanner { public static Set<Class<?>> getAnnotatedClasses(final Class[] annotations, final String... packages) { Set<String> annotationsToScan = Stream.of(annotations).map(Class::getSimpleName).collect(toSet()); ClassLoader cl = AnnotationScanner.class.getClassLoader(); try { ClassPath cp = ClassPath.from(cl); return Stream.of(packages) .flatMap(packageName -> cp.getTopLevelClassesRecursive(packageName).stream()) .filter(isClassAnnotatedByScannedAnnotations(annotationsToScan)) .map(ClassPath.ClassInfo::load) .collect(toSet()); } catch (IOException e) { log.error("Failed to get annotated classes", e); return Collections.emptySet(); } }  }
AnnotationScanner { public static Set<Class<?>> getAnnotatedClasses(final Class[] annotations, final String... packages) { Set<String> annotationsToScan = Stream.of(annotations).map(Class::getSimpleName).collect(toSet()); ClassLoader cl = AnnotationScanner.class.getClassLoader(); try { ClassPath cp = ClassPath.from(cl); return Stream.of(packages) .flatMap(packageName -> cp.getTopLevelClassesRecursive(packageName).stream()) .filter(isClassAnnotatedByScannedAnnotations(annotationsToScan)) .map(ClassPath.ClassInfo::load) .collect(toSet()); } catch (IOException e) { log.error("Failed to get annotated classes", e); return Collections.emptySet(); } }  static Set<Class<?>> getAnnotatedClasses(final Class[] annotations, final String... packages); }
AnnotationScanner { public static Set<Class<?>> getAnnotatedClasses(final Class[] annotations, final String... packages) { Set<String> annotationsToScan = Stream.of(annotations).map(Class::getSimpleName).collect(toSet()); ClassLoader cl = AnnotationScanner.class.getClassLoader(); try { ClassPath cp = ClassPath.from(cl); return Stream.of(packages) .flatMap(packageName -> cp.getTopLevelClassesRecursive(packageName).stream()) .filter(isClassAnnotatedByScannedAnnotations(annotationsToScan)) .map(ClassPath.ClassInfo::load) .collect(toSet()); } catch (IOException e) { log.error("Failed to get annotated classes", e); return Collections.emptySet(); } }  static Set<Class<?>> getAnnotatedClasses(final Class[] annotations, final String... packages);  }