public Reflections read(InputStream inputStream) { Reflections reflections; try { Constructor<Reflections> constructor = Reflections.class.getDeclaredConstructor(); constructor.setAccessible(true); reflections = constructor.newInstance(); } catch (Exception e) { reflections = new Reflections(new ConfigurationBuilder()); } try { Document document = new SAXReader().read(inputStream); for (Object e1 : document.getRootElement().elements()) { Element index = (Element) e1; for (Object e2 : index.elements()) { Element entry = (Element) e2; Element key = entry.element("key"); Element values = entry.element("values"); for (Object o3 : values.elements()) { Element value = (Element) o3; reflections.getStore().getOrCreate(index.getName()).put(key.getText(), value.getText()); } } } } catch (DocumentException e) { throw new ReflectionsException("could not read.", e); } catch (Throwable e) { throw new RuntimeException("Could not read. Make sure relevant dependencies exist on classpath.", e); } return reflections; }
Reflections reflections = new Reflections("my.project.prefix"); Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class);
@Override public Set<Class<?>> classes() { ConfigurationBuilder config = new ConfigurationBuilder(); Set<String> acceptablePackages = new HashSet<String>(); Set<String> resourceClasses = new HashSet<String>(); if (!isIgnored(pkg)) { acceptablePackages.add(pkg); config.addUrls(ClasspathHelper.forPackage(pkg)); config.setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner()); final Reflections reflections = new Reflections(config); Set<Class<?>> classes = reflections.getTypesAnnotatedWith(OpenAPIDefinition.class);
Reflections reflections = new Reflections("my.project.prefix"); Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class); Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);
/** * Searches for Methods that have either a Path Annotation or a HTTP-Method Annotation */ @SuppressWarnings("unchecked") private Set<Method> findControllerMethods() { Set<Method> methods = Sets.newLinkedHashSet(); methods.addAll(reflections.getMethodsAnnotatedWith(Path.class)); Reflections annotationReflections = new Reflections("", new TypeAnnotationsScanner(), new SubTypesScanner()); for (Class<?> httpMethod : annotationReflections.getTypesAnnotatedWith(HttpMethod.class)) { if (httpMethod.isAnnotation()) { methods.addAll(reflections.getMethodsAnnotatedWith((Class<? extends Annotation>) httpMethod)); } } return methods; }
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; }
List<Class<?>> classesList = Arrays.<Class<?>>asList(); Collection<URL> urls = ClasspathHelper.forClassLoader(); List<URL> scanUrls = new ArrayList<>(); for (URL u : urls) { Reflections reflections = new Reflections( new ConfigurationBuilder().filterInputsBy(new FilterBuilder().exclude("^(?!.*\\.class$).*$") //Consider only .class files (to avoid debug messages etc. on .dlls, etc .exclude("^org.reflections.*").exclude("^ch.qos.*") //Logback ).addUrls(scanUrls).setScanners(new DataVecSubTypesScanner(interfaces, classesList))); org.reflections.Store store = reflections.getStore(); Iterable<String> subtypesByName = store.getAll(DataVecSubTypesScanner.class.getSimpleName(), classNames); Set<? extends Class<?>> subtypeClasses = Sets.newHashSet(ReflectionUtils.forNames(subtypesByName)); subtypesClassCache = new HashSet<>(); for (Class<?> c : subtypeClasses) {
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]))); }
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()); }
ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setScanners(new SubTypesScanner(false)); builder.setUrls(ClasspathHelper.forPackage(packageName, LOGGER.getClass().getClassLoader())); builder.setInputsFilter(new FilterBuilder().includePackage(packageName)); Reflections reflections = new Reflections(builder); Multimap<String, String> map = reflections.getStore().get(SubTypesScanner.class.getSimpleName()); Set<String> types = new HashSet<>(); for (String key : map.keySet()) { Collection<String> col = map.get(key); if (col == null) { continue;
Serializer serializer = optionalSerializer != null && optionalSerializer.length == 1 ? optionalSerializer[0] : new XmlSerializer(); Collection<URL> urls = ClasspathHelper.forPackage(packagePrefix); if (urls.isEmpty()) return null; long start = System.currentTimeMillis(); final Reflections reflections = new Reflections(); Iterable<Vfs.File> files = Vfs.findFiles(urls, packagePrefix, resourceNameFilter); for (final Vfs.File file : files) { try { inputStream = file.openInputStream(); reflections.merge(serializer.read(inputStream)); } catch (IOException e) { throw new ReflectionsException("could not merge " + file, e); Store store = reflections.getStore(); int keys = 0; int values = 0; for (String index : store.keySet()) { keys += store.get(index).keySet().size(); values += store.get(index).size();
org.reflections.Configuration rc = new org.reflections.util.ConfigurationBuilder() .setUrls(scanUrls) .setScanners(new TypeAnnotationsScanner(), new SubTypesScanner()); Reflections reflections = new Reflections(rc); for (Class<?> c : reflections.getTypesAnnotatedWith(PreInitializeConfigOptions.class)) { try { loadCount += loadSingleClassUnsafe(c);
/** * Scan the classpath for {@link #classpathRootName} and return all resources under it. * {@inheritDoc} * @see org.apache.gobblin.config.store.deploy.DeployableConfigSource#getDeployableConfigPaths() */ private Set<String> getDeployableConfigPaths() { ConfigurationBuilder cb = new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader()).setScanners(new ResourcesScanner()) .filterInputsBy(new FilterBuilder().include(String.format(".*%s.*", this.classpathRootName))); Reflections reflections = new Reflections(cb); Pattern pattern = Pattern.compile(".*"); return reflections.getResources(pattern); }
public Collection<TestConfiguration> generateData(String testPackage, String pattern) { Reflections reflections = new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()) .setUrls(ClasspathHelper.forPackage("")) .filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix(testPackage)))); Collection<TestConfiguration> data = new ArrayList<TestConfiguration>(); for (String testResourceName : reflections.getResources(Pattern.compile(pattern))) { data.addAll(parseSingleResource(testResourceName)); } return data; }
private static Collection<? extends Class<?>> getPackageTypes(String packageToPase, Collection<URL> urls) { Set<Class<?>> classes = new HashSet<>(); Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false /* exclude Object.class */), new ResourcesScanner(), new TypeElementsScanner()) .setUrls(urls) .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageToPase)).exclude("java.*"))); Set<String> types; types = reflections.getStore().get("TypeElementsScanner").keySet(); for (String type: types) { Class<?> aClass = TypesHelper.loadClass(type, CLASS_LOADER); boolean wantedElement = StringUtils.startsWith(type, packageToPase); if (null != aClass && wantedElement) { logger.log(Level.INFO, "looking up for type: " + type); classes.add(aClass); } } return classes; }
ConfigurationBuilder config = new ConfigurationBuilder(); Set<String> acceptablePackages = new HashSet<String>(); Set<Class<?>> output = new HashSet<Class<?>>(); if (!isIgnored(pkg)) { acceptablePackages.add(pkg); config.addUrls(ClasspathHelper.forPackage(pkg)); allowAllPackages = true; config.filterInputsBy(new FilterBuilder().exclude(".*json").exclude(".*yaml")); config.setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner()); final Reflections reflections; reflections = new Reflections(config); Set<Class<?>> classes = reflections.getTypesAnnotatedWith(javax.ws.rs.Path.class); classes.addAll(reflections.getTypesAnnotatedWith(OpenAPIDefinition.class));
/** * Return the classes from the given package and subpackages using the supplied classloader * * @param classLoader classloader to be used * @param pkg package to scan * @return set of found classes * @throws IOException */ public static Set<Class<?>> scanPackage(ClassLoader classLoader, String pkg) throws IOException { Reflections reflections = new Reflections(new ConfigurationBuilder() .addUrls(ClasspathHelper.forPackage(pkg, classLoader)) .addClassLoader(classLoader) .setScanners(new SubTypesScanner(false))); Set<Class<?>> classes = new HashSet<Class<?>>(); for (String typeNames : reflections.getStore().get(SubTypesScanner.class.getSimpleName()).values()) { Class<?> clazz = safeClassForName(classLoader, typeNames); if (clazz != null) { classes.add(clazz); } } return classes; }
final ConfigurationBuilder conf = new ConfigurationBuilder(); conf.setScanners(new TypeElementsScanner(), new TypeAnnotationsScanner()); s.addAll(ClasspathHelper.forClassLoader()); s.addAll(ClasspathHelper.forJavaClassPath()); final Iterator<URL> iterator = s.iterator(); while (iterator.hasNext()) { conf.setUrls(new ArrayList<URL>(s)); conf.filterInputsBy(localPredicate); conf.addScanners(new SubTypesScanner()); final Reflections r = new Reflections(conf); final Set<Class<?>> entities = r.getTypesAnnotatedWith(Entity.class); for (final Class<?> c : entities) { m.map(c);
FilterBuilder filterBuilder = new FilterBuilder().includePackage(Constants.DISCONF_PACK_NAME); filterBuilder = filterBuilder.includePackage(packName); Set<URL> urls = ClasspathHelper.forPackage(packName); urlTotals.addAll(urls); Reflections reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(filter) .setScanners(new SubTypesScanner().filterResultsBy(filter), new TypeAnnotationsScanner() .filterResultsBy(filter), new FieldAnnotationsScanner() .filterResultsBy(filter),
/** * Get all the entities in a package. * * @param packageName Package name * @return All entities found in package. */ public static HashSet<Class> getAllEntities(String packageName) { return new HashSet<>(new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false), new TypeAnnotationsScanner()) .setUrls(ClasspathHelper.forClassLoader(ClassLoader.getSystemClassLoader())) .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageName)))) .getTypesAnnotatedWith(Entity.class)); } }