/** * gets all sub types in hierarchy of a given type * <p/>depends on SubTypesScanner configured */ public <T> Set<Class<? extends T>> getSubTypesOf(final Class<T> type) { return Sets.newHashSet(ReflectionUtils.<T>forNames( store.getAll(index(SubTypesScanner.class), Arrays.asList(type.getName())), loaders())); }
/** * get types annotated with a given annotation, both classes and annotations, including annotation member values matching * <p>{@link java.lang.annotation.Inherited} is honored according to given honorInherited * <p/>depends on TypeAnnotationsScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Annotation annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.annotationType().getName()); Iterable<Class<?>> filter = filter(forNames(annotated, loaders()), withAnnotation(annotation)); Iterable<String> classes = getAllAnnotated(names(filter), annotation.annotationType().isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(filter, forNames(filter(classes, not(in(Sets.newHashSet(annotated)))), loaders()))); }
/** * gets all sub types in hierarchy of a given type * <p/>depends on SubTypesScanner configured */ public <T> Set<Class<? extends T>> getSubTypesOf(final Class<T> type) { return Sets.newHashSet(ReflectionUtils.<T>forNames( store.getAll(index(SubTypesScanner.class), Arrays.asList(type.getName())), loaders())); }
/** * get types annotated with a given annotation, both classes and annotations * <p>{@link java.lang.annotation.Inherited} is honored according to given honorInherited. * <p>when honoring @Inherited, meta-annotation should only effect annotated super classes and it's sub types * <p>when not honoring @Inherited, meta annotation effects all subtypes, including annotations interfaces and classes * <p><i>Note that this (@Inherited) meta-annotation type has no effect if the annotated type is used for anything other then a class. * Also, this meta-annotation causes annotations to be inherited only from superclasses; annotations on implemented interfaces have no effect.</i> * <p/>depends on TypeAnnotationsScanner and SubTypesScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Class<? extends Annotation> annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.getName()); Iterable<String> classes = getAllAnnotated(annotated, annotation.isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(forNames(annotated, loaders()), forNames(classes, loaders()))); }
/** * get types annotated with a given annotation, both classes and annotations * <p>{@link java.lang.annotation.Inherited} is honored according to given honorInherited. * <p>when honoring @Inherited, meta-annotation should only effect annotated super classes and it's sub types * <p>when not honoring @Inherited, meta annotation effects all subtypes, including annotations interfaces and classes * <p><i>Note that this (@Inherited) meta-annotation type has no effect if the annotated type is used for anything other then a class. * Also, this meta-annotation causes annotations to be inherited only from superclasses; annotations on implemented interfaces have no effect.</i> * <p/>depends on TypeAnnotationsScanner and SubTypesScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Class<? extends Annotation> annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.getName()); Iterable<String> classes = getAllAnnotated(annotated, annotation.isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(forNames(annotated, loaders()), forNames(classes, loaders()))); }
/** * get types annotated with a given annotation, both classes and annotations, including annotation member values matching * <p>{@link java.lang.annotation.Inherited} is honored according to given honorInherited * <p/>depends on TypeAnnotationsScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Annotation annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.annotationType().getName()); Iterable<Class<?>> filter = filter(forNames(annotated, loaders()), withAnnotation(annotation)); Iterable<String> classes = getAllAnnotated(names(filter), annotation.annotationType().isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(filter, forNames(filter(classes, not(in(Sets.newHashSet(annotated)))), loaders()))); }
types.addAll(ReflectionUtils.forNames(allTypesScanner.types, classLoader));
public Set<Class<?>> getTypesAnnotatedWith(final Class<? extends Annotation> annotation , boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class) , annotation.getName()); Iterable<String> classes = getAllAnnotated(annotated , annotation.isAnnotationPresent(Inherited.class) , honorInherited); final List<Class<?>> classes1 = forNames(annotated , loaders()); final List<Class<?>> classes2 = forNames(classes , loaders()); final Iterable<Class<?>> concat = concat(classes1 , classes2); return StreamSupport .stream(concat.spliterator() , false) .collect(Collectors.toSet()); }
/** * gets all sub types in hierarchy of a given type * <p/>depends on SubTypesScanner configured */ public <T> Set<Class<? extends T>> getSubTypesOf(final Class<T> type) { return Sets.newHashSet(ReflectionUtils.<T>forNames( store.getAll(index(SubTypesScanner.class), Arrays.asList(type.getName())), loaders())); }
/** * gets all sub types in hierarchy of a given type * <p/>depends on SubTypesScanner configured */ public <T> Set<Class<? extends T>> getSubTypesOf(final Class<T> type) { return Sets.newHashSet(ReflectionUtils.<T>forNames( store.getAll(index(SubTypesScanner.class), Arrays.asList(type.getName())), loaders())); }
Set<Class<? extends T>> scan(Class<? extends T> aClass) { Configuration configuration = ConfigurationBuilder.build((Object[]) packages).addClassLoaders(classLoaders) .addScanners(new AssignableScanner(aClass)); Reflections reflections = new Reflections(configuration); Predicate<Class<? extends T>> classPredicate = klass -> Modifier.isPublic(klass.getModifiers()) && (!klass.isMemberClass() || (klass.isMemberClass() && Modifier .isStatic(klass.getModifiers()))) && !Modifier.isInterface(klass.getModifiers()) && !Modifier.isAbstract(klass.getModifiers()); HashSet<Class<? extends T>> subtypes = Sets.newHashSet( ReflectionUtils.forNames( reflections.getStore() .getAll(AssignableScanner.class.getSimpleName(), Collections.singletonList(aClass.getName())), classLoaders)); return subtypes.stream().filter(classPredicate).collect(Collectors.toSet()); }
private List<UIModule> getCustomUIModules(List<Class<?>> excludeClasses) { //Scan classpath for UI module instances, but ignore the 'excludeClasses' classes List<String> classNames = Collections.singletonList(UIModule.class.getName()); Reflections reflections = new Reflections(); org.reflections.Store store = reflections.getStore(); Iterable<String> subtypesByName = store.getAll(org.reflections.scanners.SubTypesScanner.class.getSimpleName(), classNames); Set<? extends Class<?>> subtypeClasses = Sets.newHashSet(ReflectionUtils.forNames(subtypesByName)); List<Class<?>> toCreate = new ArrayList<>(); for (Class<?> c : subtypeClasses) { if (excludeClasses.contains(c)) continue;; toCreate.add(c); } List<UIModule> ret = new ArrayList<>(toCreate.size()); for (Class<?> c : toCreate) { UIModule m; try { m = (UIModule) c.newInstance(); } catch (Exception e) { log.warn("Could not create instance of custom UIModule of type {}; skipping", c, e); continue; } log.debug("Created instance of custom UI module: {}", c); ret.add(m); } return ret; }
/** * get types annotated with a given annotation, both classes and annotations * <p>{@link java.lang.annotation.Inherited} is honored according to given honorInherited. * <p>when honoring @Inherited, meta-annotation should only effect annotated super classes and it's sub types * <p>when not honoring @Inherited, meta annotation effects all subtypes, including annotations interfaces and classes * <p><i>Note that this (@Inherited) meta-annotation type has no effect if the annotated type is used for anything other then a class. * Also, this meta-annotation causes annotations to be inherited only from superclasses; annotations on implemented interfaces have no effect.</i> * <p/>depends on TypeAnnotationsScanner and SubTypesScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Class<? extends Annotation> annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.getName()); Iterable<String> classes = getAllAnnotated(annotated, annotation.isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(forNames(annotated, loaders()), forNames(classes, loaders()))); }
public <T> Set<Class<? extends T>> getSubTypesOf(final Class<T> type) { final Iterable<String> all = store.getAll(index(SubTypesScanner.class) , Arrays.asList(type.getName())); return new HashSet<>(ReflectionUtils.<T>forNames(all , loaders())); }
private List<UIModule> getCustomUIModules(List<Class<?>> excludeClasses) { //Scan classpath for UI module instances, but ignore the 'excludeClasses' classes List<String> classNames = Collections.singletonList(UIModule.class.getName()); Reflections reflections = new Reflections(); org.reflections.Store store = reflections.getStore(); Iterable<String> subtypesByName = store.getAll(org.reflections.scanners.SubTypesScanner.class.getSimpleName(), classNames); Set<? extends Class<?>> subtypeClasses = Sets.newHashSet(ReflectionUtils.forNames(subtypesByName)); List<Class<?>> toCreate = new ArrayList<>(); for (Class<?> c : subtypeClasses) { if (excludeClasses.contains(c)) continue;; toCreate.add(c); } List<UIModule> ret = new ArrayList<>(toCreate.size()); for (Class<?> c : toCreate) { UIModule m; try { m = (UIModule) c.newInstance(); } catch (Exception e) { log.warn("Could not create instance of custom UIModule of type {}; skipping", c, e); continue; } log.debug("Created instance of custom UI module: {}", c); ret.add(m); } return ret; }
/** * get types annotated with a given annotation, both classes and annotations * <p>{@link java.lang.annotation.Inherited} is honored according to given honorInherited. * <p>when honoring @Inherited, meta-annotation should only effect annotated super classes and it's sub types * <p>when not honoring @Inherited, meta annotation effects all subtypes, including annotations interfaces and classes * <p><i>Note that this (@Inherited) meta-annotation type has no effect if the annotated type is used for anything other then a class. * Also, this meta-annotation causes annotations to be inherited only from superclasses; annotations on implemented interfaces have no effect.</i> * <p/>depends on TypeAnnotationsScanner and SubTypesScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Class<? extends Annotation> annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.getName()); Iterable<String> classes = getAllAnnotated(annotated, annotation.isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(forNames(annotated, loaders()), forNames(classes, loaders()))); }
/** * get types annotated with a given annotation, both classes and annotations, including annotation member values matching * <p>{@link java.lang.annotation.Inherited} is honored according to given honorInherited * <p/>depends on TypeAnnotationsScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Annotation annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.annotationType().getName()); Iterable<Class<?>> filter = filter(forNames(annotated, loaders()), withAnnotation(annotation)); Iterable<String> classes = getAllAnnotated(names(filter), annotation.annotationType().isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(filter, forNames(filter(classes, not(in(Sets.newHashSet(annotated)))), loaders()))); }
/** * get types annotated with a given annotation, both classes and annotations, including annotation member values matching * <p>{@link java.lang.annotation.Inherited} is honored according to given honorInherited * <p/>depends on TypeAnnotationsScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Annotation annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.annotationType().getName()); Iterable<Class<?>> filter = filter(forNames(annotated, loaders()), withAnnotation(annotation)); Iterable<String> classes = getAllAnnotated(names(filter), annotation.annotationType().isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(filter, forNames(filter(classes, not(in(Sets.newHashSet(annotated)))), loaders()))); }
public Set<Class<?>> getTypesAnnotatedWith(final Annotation annotation , boolean honorInherited) { final Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class) , annotation.annotationType().getName()); final Iterable<Class<?>> filter = filter(forNames(annotated , loaders()) , withAnnotation(annotation)); final Iterable<String> classes = getAllAnnotated(names(filter) , annotation.annotationType().isAnnotationPresent(Inherited.class) , honorInherited); final Iterable<Class<?>> concat = concat( filter , forNames( filter(classes , not( in( StreamSupport .stream(annotated.spliterator() , false) .collect(Collectors.toSet()) ))) , loaders())); return StreamSupport .stream(concat.spliterator() , false) .collect(Collectors.toSet()); }
private static Set<Class<?>> getClasses(ClassLoader classLoader, String packageName) { List<ClassLoader> classLoadersList = new LinkedList<>(); classLoadersList.add(ClasspathHelper.contextClassLoader()); classLoadersList.add(ClasspathHelper.staticClassLoader()); if (classLoader != null) { classLoadersList.add(classLoader); } classLoaders = classLoadersList.toArray(new ClassLoader[0]); FilterBuilder filter = new FilterBuilder() .include(FilterBuilder.prefix(packageName)); if (!isAllowFindingInternalClasses()) { filter.exclude(FilterBuilder.prefix(URM_PACKAGE)); } Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) .setUrls(ClasspathHelper.forPackage(packageName, classLoaders)) .filterInputsBy(filter) .addClassLoaders(classLoadersList) ); Multimap<String, String> mmap = reflections.getStore().get(SubTypesScanner.class.getSimpleName()); return Sets.newHashSet(ReflectionUtils.forNames(mmap.values(), classLoadersList.toArray(new ClassLoader[0]))); }