/** * Scans all classes accessible from the context class loader which belong to the given package and subpackages. * @param pckg package name * @param annotation Annotation to search * @return The classes */ static public List<Class<?>> getAnnotatedClasses(Package pckg, Class<? extends Annotation> annotation) { final AnnotationAcceptingListener annotationAcceptingListener = new AnnotationAcceptingListener(annotation); try (final PackageNamesScanner scanner = new PackageNamesScanner(new String[] { pckg.getName() }, true)) { while (scanner.hasNext()) { final String next = scanner.next(); if (annotationAcceptingListener.accept(next)) { try (final InputStream in = scanner.open()) { annotationAcceptingListener.process(next, in); } catch (IOException e) { throw new RuntimeException("AnnotationAcceptingListener failed to process scanned resource: " + next); } } } } return annotationAcceptingListener.getAnnotatedClasses() .stream().collect(Collectors.toCollection(ArrayList::new)); }
private static ImmutableList<Class<?>> findEntityClassesFromDirectory(String path, String... paths) { @SuppressWarnings("unchecked") final AnnotationAcceptingListener asl = new AnnotationAcceptingListener(Entity.class); final PackageNamesScanner scanner = new PackageNamesScanner(merge(path, paths), true); while (scanner.hasNext()) { final String next = scanner.next(); if (asl.accept(next)) { try (final InputStream in = scanner.open()) { asl.process(next, in); } catch (IOException e) { throw new RuntimeException("AnnotationAcceptingListener failed to process scanned resource: " + next); } } } final ImmutableList.Builder<Class<?>> builder = ImmutableList.builder(); asl.getAnnotatedClasses().forEach(builder::add); return builder.build(); }
/** * <p>scan.</p> */ public void scan() { final PackageNamesScanner scanner = new PackageNamesScanner( scanPkgs.toArray(new String[scanPkgs.size()]), true); while (scanner.hasNext()) { String fileName = scanner.next(); if (!fileName.endsWith(".class")) continue; ClassInfo info = new ClassInfo(fileName) { InputStream in; @Override public InputStream getFileStream() { if (in == null) { in = scanner.open(); } return in; } @Override public void closeFileStream() { closeQuietly(in); } }; String className = info.getCtClass().getName(); if (!foundClasses.contains(className)) { ClassFoundEvent event = new ClassFoundEvent(info); SystemEventBus.publish(event); info.closeFileStream(); if (event.accept) { acceptClasses.add(className); } } foundClasses.add(className); } }
protected AbstractRestApplication(Class<? extends AbstractRestApplication> applicationClass) { super(applicationClass); register(JacksonJsonProvider.class); registerFinder(new PackageNamesScanner(new String[] { applicationClass.getPackage().getName() }, true)); }