/** * Method scanning given directory for classes containing Hibernate @Entity annotation * * @param pckgs string array with packages containing Hibernate entities (classes annotated with @Entity annotation) * e.g. com.codahale.fake.db.directory.entities * @return ImmutableList with classes from given directory annotated with Hibernate @Entity annotation */ public static List<Class<?>> findEntityClassesFromDirectory(String[] pckgs) { @SuppressWarnings("unchecked") final AnnotationAcceptingListener asl = new AnnotationAcceptingListener(Entity.class); try (final PackageNamesScanner scanner = new PackageNamesScanner(pckgs, 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); } } } } return new ArrayList<>(asl.getAnnotatedClasses()); } }
@Override public InputStream getFileStream() { if (in == null) { in = scanner.open(); } return in; }
/** * Method scanning given directory for classes containing Hibernate @Entity annotation * * @param pckgs string array with packages containing Hibernate entities (classes annotated with @Entity annotation) * e.g. com.codahale.fake.db.directory.entities * @return ImmutableList with classes from given directory annotated with Hibernate @Entity annotation */ public static ImmutableList<Class<?>> findEntityClassesFromDirectory(String[] pckgs) { @SuppressWarnings("unchecked") final AnnotationAcceptingListener asl = new AnnotationAcceptingListener(Entity.class); final PackageNamesScanner scanner = new PackageNamesScanner(pckgs, 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(); for (Class<?> clazz : asl.getAnnotatedClasses()) { builder.add(clazz); } return builder.build(); } }
final String next = scanner.next(); if (asl.accept(next)) { try (final InputStream in = scanner.open()) { asl.process(next, in); } catch (IOException e) {
/** * 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)); } }