Refine search
/** * 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); }
@Override public Object scan(Vfs.File file, Object classObject) { getStore().put(file.getName(), file.getRelativePath()); return classObject; }
config.setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner()); final Reflections reflections = new Reflections(config); Set<Class<?>> classes = reflections.getTypesAnnotatedWith(OpenAPIDefinition.class);
public ReflectionsExtensionFinder(URL[] classpath, ClassLoader classLoader) { this.classpath = classpath; this.classLoader = classLoader; this.reflections = new Reflections(classLoader, classpath, new ResourcesScanner(), new SubTypesScanner(), new TypeAnnotationsScanner()); }
public List<Class<?>> annotatedClassesInPackage(String packageName) { Reflections reflections = new Reflections(packageName, new SubTypesScanner(), new TypeAnnotationsScanner(), new MethodAnnotationsScanner(), new ResourcesScanner(), getClassLoader()); Set<Class<?>> matchingClasses = new HashSet<>(); for (Class<? extends Annotation> expectedAnnotation : expectedAnnotations) { matchingClasses.addAll(reflections.getTypesAnnotatedWith(expectedAnnotation)); matchingClasses.addAll(classesFrom(reflections.getMethodsAnnotatedWith(expectedAnnotation))); } return ImmutableList.copyOf(matchingClasses); }
classLoadersList.add(ClasspathHelper.staticClassLoader()); classLoadersList.add(new URLClassLoader(urls.toArray(new URL[0]))); Reflections reflections = new Reflections(new ConfigurationBuilder().addClassLoaders(classLoadersList).setUrls(urls).setScanners(new SubTypesScanner(), new TypeAnnotationsScanner().filterResultsBy(new Predicate<String>() return false; }), new ResourcesScanner())); System.out.println("Finished Scanning. Getting subtypes and initializing classes."); int crunchers = 0, scripters = 0; Set<Class<? extends Cruncher>> csubTypesOf = reflections.getSubTypesOf(Cruncher.class); cruncherSpecs = new EwMap<String, Class<? extends Cruncher>>(); Set<Class<? extends Scripter>> ssubTypesOf = reflections.getSubTypesOf(Scripter.class); scripterSpecs = new EwMap<String, Class<? extends Scripter>>();
Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(urls).setScanners( new ResourcesScanner().filterResultsBy(new Predicate<String>() Set<String> resources = reflections.getResources(Pattern.compile(".*")); for (String s : resources)
.get())); final Reflections reflections = new Reflections( new ConfigurationBuilder().addUrls(filterURLs(classloaders)) .addScanners(new ResourcesScanner())); final Set<String> files = reflections.getStore().getResources( new Predicate<String>() {
@Test public void ensureAllClassesInPackageTempHaveAZeroArgConstructor() throws Exception { List<ClassLoader> classLoadersList = new LinkedList<>(); classLoadersList.add(ClasspathHelper.contextClassLoader()); classLoadersList.add(ClasspathHelper.staticClassLoader()); Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false), new ResourcesScanner()) .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0]))) .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("temp")))); for (Class<?> clazz : reflections.getSubTypesOf(Object.class)) { boolean hasZeroArgConstructor = Arrays.asList(clazz.getConstructors()).stream().anyMatch(c -> c.getParameterCount() == 0); if (! hasZeroArgConstructor) { Assert.fail(String.format("%s doesn't have a zero arg constructor", clazz.getCanonicalName())); } } }
Reflections reflections = new Reflections(null, new ResourcesScanner()); Set<String> resourceList = reflections.getResources(x -> true);
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>(); classLoadersList.add(ClasspathHelper.contextClassLoader()); classLoadersList.add(ClasspathHelper.staticClassLoader()); Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0]))) .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("org.your.package"))));
Predicate<String> filter = new FilterBuilder().include(".*\\.properties"); Reflections reflections = new Reflections(new ConfigurationBuilder() .filterInputsBy(filter) .setScanners(new ResourcesScanner()) .setUrls(asList(ClasspathHelper.forJavaClassPath()))); System.out.println(reflections.getStore().get(ResourcesScanner.class).keySet());
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));
/** * Creates an instance of the scanner that scans only in the given packages. * * @param packages * Packages to limit scanning to. */ private DeploymentScanner(final String... packages) { final ConfigurationBuilder builder = ConfigurationBuilder.build((Object[]) packages); removeSysLibUrls(builder); builder.addScanners(new ResourcesScanner()); reflections = new Reflections(builder); }
public List<Class<?>> annotatedClassesInPackage(String packageName) { Reflections reflections = new Reflections(packageName, new SubTypesScanner(), new TypeAnnotationsScanner(), new MethodAnnotationsScanner(), new ResourcesScanner(), getClassLoader()); Set<Class<?>> matchingClasses = Sets.newHashSet(); for (Class<? extends Annotation> expectedAnnotation : expectedAnnotations) { matchingClasses.addAll(reflections.getTypesAnnotatedWith(expectedAnnotation)); matchingClasses.addAll(classesFrom(reflections.getMethodsAnnotatedWith(expectedAnnotation))); } return ImmutableList.copyOf(matchingClasses); }
/** * Looks for use of javax.inject.Provider (should be com.google.inject.Provider). For the scope classes, however, * the Provider has to be of the Google type, and are therefore excluded from the check */ @Test public void testForGoogleClasses() { // given List<Class<?>> targetTypes = new ArrayList<>(); targetTypes.add(javax.inject.Provider.class); Reflections reflections = new Reflections(new ConfigurationBuilder().setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) .setUrls(ClasspathHelper.forPackage("uk" + ".co.q3c"))); Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class); // when boolean failed = false; for (Class<? extends Object> clazz : allClasses) { Field[] declaredFields = clazz.getDeclaredFields(); for (Field field : declaredFields) { if (targetTypes.contains(field.getType())) { System.out.println("Found target type " + field.getType() + " in " + clazz.getName()); failed = true; } ; } } // then assertThat(failed).isFalse() .overridingErrorMessage("See console output if this fails"); }
Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(urls).setScanners( new ResourcesScanner().filterResultsBy(new Predicate<String>() Set<String> resources = reflections.getResources(Pattern.compile(".*")); for (String s : resources)
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]))); }
/** * Finds all package names * @return Set of package names */ public Set<String> findAllPackages() { List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>(); classLoadersList.add(ClasspathHelper.contextClassLoader()); classLoadersList.add(ClasspathHelper.staticClassLoader()); Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false), new ResourcesScanner()) .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0]))) .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("my.base.package")))); Set<Class<? extends Object>> classes = reflections.getSubTypesOf(Object.class); Set<String> packageNameSet = new TreeSet<String>(); for (Class classInstance : classes) { packageNameSet.add(classInstance.getPackage().getName()); } return packageNameSet; }
Reflections reflections = new Reflections("com.example.package", new ResourcesScanner()); Set<String> paths = reflections.getResources(Pattern.compile(".*\\.template$")); Map<String, String> templates = new LinkedHashMap<>(); for (String path : paths) { log.info("Found " + path); String templateName = Files.getNameWithoutExtension(path); URL resource = getClass().getClassLoader().getResource(path); String text = Resources.toString(resource, StandardCharsets.UTF_8); templates.put(templateName, text); }