private Map<String, String> getAllDocStrings0(ClassLoader classLoader) { final Configuration configuration = new ConfigurationBuilder() .filterInputsBy(new FilterBuilder().includePackage(path)) .setUrls(ClasspathHelper.forPackage(path, classLoader)) .addClassLoader(classLoader) .setScanners(new ResourcesScanner()); if (configuration.getUrls() == null || configuration.getUrls().isEmpty()) { return Collections.emptyMap(); } final Map<String, byte[]> files = new Reflections(configuration) .getResources(this::acceptFile).stream() .map(f -> { try { URL url = classLoader.getResource(f); if (url == null) { throw new IllegalStateException("not found: " + f); } return new SimpleImmutableEntry<>(f, Resources.toByteArray(url)); } catch (IOException e) { throw new UncheckedIOException(e); } }) .collect(toImmutableMap(Entry::getKey, Entry::getValue)); return getDocStringsFromFiles(files); }
/** * 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; }
public AnnotationTypeLoader(TypeLoader<T> parent, ClassLoader classLoader) { this.parent = parent; builder = new ConfigurationBuilder(); builder.addClassLoader(classLoader); }
public void setClasspathUrls(ConfigurationBuilder configurationBuilder) { List<String> classpathElements = null; try { classpathElements = project.getCompileClasspathElements(); List<URL> projectClasspathList = new ArrayList<URL>(); for (String element : classpathElements) { projectClasspathList.add(new File(element).toURI().toURL()); } configurationBuilder.addUrls(projectClasspathList); configurationBuilder.addClassLoader(new URLClassLoader(projectClasspathList.toArray(new URL[0]), Thread.currentThread().getContextClassLoader())); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } }
private static Reflections buildReflections(final Collection<URL> urls) { final ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread .currentThread().getContextClassLoader()); final ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setScanners(SCANNER).addClassLoader(classLoader).addUrls(urls); final Reflections reflections = new Reflections(builder); return reflections; }
private static Reflections buildReflections(final Collection<URL> urls) { final ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread .currentThread().getContextClassLoader()); final ConfigurationBuilder builder = new ConfigurationBuilder(); builder.addScanners(SCANNER).addClassLoader(classLoader).addUrls(urls); final Reflections reflections = new Reflections(builder); return reflections; }
private static <T> Set<Class<? extends T>> getSubclassesFromJarEntry(URL[] urls, Class<T> targetCls) { Reflections refl = new Reflections(new ConfigurationBuilder(). setUrls(urls). addClassLoader(new URLClassLoader(urls))); return refl.getSubTypesOf(targetCls); }
public Map<String, Field> find() throws MalformedURLException { Reflections reflections = new Reflections( new ConfigurationBuilder() .setUrls(classpathUrls) .addClassLoader(new URLClassLoader(classpathUrls.toArray(new URL[0]), getClass().getClassLoader())) .setScanners(new SubTypesScanner())); Map<String, Field> metricFields = new HashMap<>(); for (Class<? extends LambdaMetricSet> type : reflections.getSubTypesOf(LambdaMetricSet.class)) { metricFields.putAll(MetricsUtils.findAnnotatedFields(type)); } return metricFields; }
/** * Creates a new instance of class path introspector using the * {@code projectClassesLocations}. * * @param projectClassesLocations * urls to project class locations (directories, jars etc.) */ protected ClassPathIntrospector(URL... projectClassesLocations) { projectClassLoader = new URLClassLoader(projectClassesLocations, null); reflections = new Reflections( new ConfigurationBuilder().addClassLoader(projectClassLoader) .addUrls(projectClassesLocations)); }
private String getKeywordClassList(URLClassLoader cl) throws Exception { URL url = cl.getURLs()[0]; try { Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(url) .addClassLoader(cl).setScanners(new MethodAnnotationsScanner())); Set<Method> methods = reflections.getMethodsAnnotatedWith(Keyword.class); Set<String> kwClasses = new HashSet<>(); for(Method method:methods) { kwClasses.add(method.getDeclaringClass().getName()); } StringBuilder kwClassnamesBuilder = new StringBuilder(); kwClasses.forEach(kwClassname->kwClassnamesBuilder.append(kwClassname+";")); return kwClassnamesBuilder.toString(); } catch (Exception e) { String errorMsg = "Error while looking for methods annotated with @Keyword in "+url.toString(); throw new Exception(errorMsg, e); } } }
public static <T extends BrooklynObject> Set<Class<? extends T>> findClasses(Collection<URL> urls, Class<T> clazz) { ClassLoader classLoader = new UrlClassLoader(urls.toArray(new URL[urls.size()])); Reflections reflections = new ConfigurationBuilder() .addClassLoader(classLoader) .addScanners(new SubTypesScanner(), new TypeAnnotationsScanner(), new FieldAnnotationsScanner()) .addUrls(urls) .build(); Set<Class<? extends T>> types = reflections.getSubTypesOf(clazz); return types; } }
/** * Builds a {@link Reflections} instance that will scan for classes in, and * load them from, the given class loader. * * @param classLoader The class loader to scan and load from. * @return A new {@link Reflections} instance. */ static Reflections createReflections(ClassLoader classLoader, String packagePrefix) { // We set up reflections to use the classLoader for loading classes // and also to use the classLoader to determine the list of URLs: ConfigurationBuilder configurationBuilder = new ConfigurationBuilder().addClassLoader(classLoader); if (StringUtils.isNotBlank(packagePrefix)) { configurationBuilder.addUrls(ClasspathHelper.forPackage(packagePrefix, classLoader)); } else { configurationBuilder.addUrls(ClasspathHelper.forClassLoader(classLoader)); } Reflections reflections = new Reflections(configurationBuilder); log.info("Reflections URLs: {}", reflections.getConfiguration().getUrls()); if (Main.configuration.classesReloadable && reflections.getConfiguration().getUrls().size() == 0 && StringUtils.isNotEmpty(Main.configuration.packagePrefix)) { log.info("It looks like no reloadable classes were found. Is '{}' the correct package prefix for your app?", Main.configuration.packagePrefix); } return reflections; } }
@Override public Collection<Class<? extends T>> lookup(Class<?> clazz, ClassLoader classLoader) { Reflections reflections = new Reflections(new ConfigurationBuilder() .addClassLoader(classLoader) .setUrls(ClasspathHelper.forClass(clazz, classLoader)) .setScanners(new SubTypesScanner(true), new TypeAnnotationsScanner()) .filterInputsBy(new FilterBuilder().includePackage(clazz.getPackage().getName()))); return reflections.getSubTypesOf(subType); } }
@Override public Set<Method> findFunctions(final List<URL> urls) { return new Reflections( new ConfigurationBuilder() .addUrls(urls) .setScanners(new MethodAnnotationsScanner()) .addClassLoader(getClassLoader(urls))) .getMethodsAnnotatedWith(FunctionName.class); }
private List<ScanResult> scanForSources(ClassLoader classLoader, URL[] downloadedUrls) throws Exception { Reflections reflections = new Reflections(new ConfigurationBuilder().addClassLoader(classLoader) .addScanners(new SubTypesScanner(), new TypeAnnotationsScanner()).addUrls(downloadedUrls)); Set<Class<?>> sources = Sets.intersection(reflections.getTypesAnnotatedWith(Source.class), reflections.getSubTypesOf(PipelineSource.class)); return sources.stream().map(source -> { Source sourceInfo = source.getAnnotation(Source.class); ComponentMetadata metadata = ComponentMetadata.builder().type(ComponentType.SOURCE).namespace(sourceInfo.namespace()) .name(sourceInfo.name()).version(sourceInfo.version()).description(sourceInfo.description()) .cpu(sourceInfo.cpu()).memory(sourceInfo.memory()) .requiredProperties(ImmutableList.copyOf(sourceInfo.requiredProperties())) .optionalProperties(ImmutableList.copyOf(sourceInfo.optionalProperties())).build(); return ScanResult.builder().metadata(metadata).componentClass(source).build(); }).collect(Collectors.toCollection(ArrayList::new)); }
private List<ScanResult> scanForProcessors(ClassLoader classLoader, URL[] downloadedUrls) throws Exception { Reflections reflections = new Reflections(new ConfigurationBuilder().addClassLoader(classLoader) .addScanners(new SubTypesScanner(), new TypeAnnotationsScanner()).addUrls(downloadedUrls)); Set<Class<?>> processors = Sets.intersection(reflections.getTypesAnnotatedWith(Processor.class), reflections.getSubTypesOf(ProcessorBase.class)); return processors.stream().map(processor -> { Processor processorInfo = processor.getAnnotation(Processor.class); ComponentMetadata metadata = ComponentMetadata.builder().type(ComponentType.PROCESSOR).namespace(processorInfo.namespace()) .name(processorInfo.name()).version(processorInfo.version()) .description(processorInfo.description()).cpu(processorInfo.cpu()) .memory(processorInfo.memory()).processorType(processorInfo.processorType()) .requiredProperties(ImmutableList.copyOf(processorInfo.requiredProperties())) .optionalProperties(ImmutableList.copyOf(processorInfo.optionalProperties())).build(); return ScanResult.builder().metadata(metadata).componentClass(processor).build(); }).collect(Collectors.toCollection(ArrayList::new)); }
private static void init() { final Reflections ref = new Reflections(new ConfigurationBuilder() .setUrls(ClasspathHelper.forPackage("org.intellivim")) .setScanners( new SubTypesScanner(), new TypeAnnotationsScanner()) .addClassLoader(ICommand.class.getClassLoader()) .filterInputsBy(new FilterBuilder().include("org.intellivim.*")) ); commandMap = initCommands(ref); injectors = initInjectors(ref); polymorphManager = new PolymorphManager(ref); }
builder.addClassLoader(classLoader);
builder.addClassLoader(classLoader);
public void addLoader(ClassLoader loader) { if (builder == null) { this.builder = new ConfigurationBuilder(); builder.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner()); } builder.addClassLoader(loader); builder.addUrls(ClasspathHelper.forClassLoader(loader)); if(loader instanceof JoinClassLoader) { // When the object "reflections" is created in the method scanClassPath(), are scanned the URLs so // it is necessary to add the URLs from the enclosing class loader in the JoinClassLoader that it // contains the fixture classpath (see org.reflections.Reflections.scan()). builder.addUrls(ClasspathHelper.forClassLoader(((JoinClassLoader) loader).getEnclosingClassLoader())); } scanClassPath(builder); }